首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在测试角7中的Jest时缺少Kendo Intl服务的区域设置信息

在测试角7中的Jest时缺少Kendo Intl服务的区域设置信息
EN

Stack Overflow用户
提问于 2019-05-07 11:36:23
回答 1查看 2K关注 0票数 8

我对处理区域设置格式的非常特定的数字组件进行了多次测试。对Karma的测试是正常的,但是一旦我们将它更改为Jest,这个错误就会出现:

NoLocale: Missing locale info for 'de-DE' Solution: http://www.telerik.com/kendo-angular-ui/components/internationalization/troubleshooting/#toc-no-locale

我尝试了所有的建议,从附件的链接,但没有一个成功。

IntlService是用en-US区域设置id初始化的,因此我根据我想要断言的格式(de-DEen-GB)对每个测试进行了更改。

以下是component.spec.ts中的测试示例:

numeric-textbox.component.spec.ts

代码语言:javascript
复制
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NumericTextBoxComponent } from './numeric-textbox.component';
import { FormsModule } from '@angular/forms';
import { CldrIntlService, IntlModule, load } from '@progress/kendo-angular-intl';

describe('NumericTextBoxComponent', () => {
  let component: NumericTextBoxComponent;
  let fixture: ComponentFixture<NumericTextBoxComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ FormsModule, IntlModule ],
      declarations: [ NumericTextBoxComponent ],
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(NumericTextBoxComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('User writes valid numbers with German/International notation', () => {
    component.format = 'n2';
    (<CldrIntlService>component.intlService).localeId = 'de-DE';
    fixture.detectChanges();
      const displayValueInput: HTMLInputElement = fixture.debugElement.nativeElement.querySelector('input');

  // absolute integer
  displayValueInput.value = '123456789';
  displayValueInput.dispatchEvent(new Event('input'));
  displayValueInput.dispatchEvent(new Event('focusout'));
  expect(component.inputValue).toBe('123456789');
  expect(component.displayValue).toBe('123.456.789,00');
});

it('displays the correct format when the control is created in english context', () => {
    component.format = 'n2';
    (<CldrIntlService>component.intlService).localeId = 'en-GB';
    fixture.detectChanges();
      const displayValueInput: HTMLInputElement = fixture.debugElement.nativeElement.querySelector('input');
      displayValueInput.value = '123456789.12';
      displayValueInput.dispatchEvent(new Event('input'));
      displayValueInput.dispatchEvent(new Event('focusout'));
      expect(component.inputValue).toBe('123456789.12');
      expect(component.displayValue).toBe('123,456,789.12');
  });

非常罕见的是,Intl服务承认'en-GB‘区域设置(因此测试运行良好),但'de-DE’则不然。

我的问题是:如何将de-DE 区域设置信息导入到测试环境中,以便通过Intl服务动态识别?

更新5月10日'19

fixture.whenStable().then(()=> {})在测试错误处理中引起了问题,因此被删除。

作为附加信息,focusout事件会在组件中触发以下方法:

numeric-textbox.component.ts

代码语言:javascript
复制
onFocusOut(first: boolean = false): void {
      console.log("onFocusOut");
        if (this.isReadOnly && !first && !this.isFocusIn) { return; }
        this.isFocusIn = false;
        // save the temporal the editable display value into the inputValue.
        // if the escape key was pressed, then we undo the actual displayValue to the lastInputValue.
        this.inputValue = !this.wasEscapePressed ? this.displayValue : this.lastInputValue;
        this.wasEscapePressed = false;
        // update the readable display value with the absolute value of the input value (or zero if it is null, zero or empty).
        this.displayValue = this.formatDisplayValue(this.inputValue);
    }

formatDisplayValue(input: string): string {
        // single signs, nulls, empty and zeros are shown as zero
        if (this.checkNullZeroOrEmpty(input) || this.isSignOnly(input)) {
            input = NumericTextBoxComponent.ZERO;
        } else if (this.IsPositiveValue(input) && input[0] === NumericTextBoxComponent.PLUS_SIGN) {
            // positive signs at the beginning are trim
            input = input.replace(NumericTextBoxComponent.PLUS_SIGN, '');
        }

        // display value are always the absolute value
        const numberValue = Math.abs(this.getNumberValue(input));

        // finally return the number formatted based in the locale. 
        return this.intlService.formatNumber(numberValue, this.format);
    }

因此,当我将焦点放在输入的HTML元素中时,显示值会根据地区设置格式。

EN

回答 1

Stack Overflow用户

发布于 2019-05-09 21:22:36

我建议您尝试导入它,正如您在加载附加区域设置数据章节中链接到的文档中所描述的那样。由于您没有提到加载额外的区域设置,所以我认为您根本没有这样做。否则,请提供附加信息,您如何加载它们。

对于de,您需要添加以下导入。

代码语言:javascript
复制
// Load all required data for the de locale
import '@progress/kendo-angular-intl/locales/de/all';
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56021722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档