gpt4 book ai didi

angular - 在 Angular 7 中使用 Jest 进行测试时缺少 Kendo Intl Service 的语言环境信息

转载 作者:太空狗 更新时间:2023-10-29 17:21:09 24 4
gpt4 key购买 nike

我对处理区域设置格式的非常具体的数字组件进行了多次测试。 Karma 的测试没问题,但是一旦我们将其更改为 Jest,这个错误就开始出现:

NoLocale:缺少“de-DE”的语言环境信息解决方案:http://www.telerik.com/kendo-angular-ui/components/internationalization/troubleshooting/#toc-no-locale

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

IntlService 是用 en-US locale id 初始化的,所以我为每个测试更改了它,具体取决于我想要断言的格式(de- DEen-GB).

这些是 component.spec.ts 中的测试示例:

数字文本框.component.spec.ts

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');
});

很少见的是“en-GB”语言环境被 Intl Service 识别(因此测试运行良好),但“de-DE”则不然。

我的问题是:如何将de-DE locale信息导入测试环境,让Intl Service动态识别?

2019 年 5 月 10 日更新

fixture.whenStable().then(()=> {}) 导致测试错误处理出现问题,因此已将其删除。


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

数字文本框.component.ts

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 元素时,显示值的格式取决于语言环境。

最佳答案

我建议您尝试按照您在章节 loading additional locale data 中链接到的文档中的描述导入它.由于您没有提到加载其他语言环境,我假设您根本没有这样做。否则,请提供您如何加载它们的其他信息。

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

// Load all required data for the de locale
import '@progress/kendo-angular-intl/locales/de/all';

关于angular - 在 Angular 7 中使用 Jest 进行测试时缺少 Kendo Intl Service 的语言环境信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56021722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com