Tit*_*iva 7 jestjs angular kendo-angular-ui
我对处理语言环境格式的非常特定的数字组件进行了多次测试。使用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-DE或en-GB)。
这些是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');
});
Run Code Online (Sandbox Code Playgroud)
罕见的是国际服务可以识别“ en-GB”语言环境(因此测试运行正常),但“ de-DE”则不能。
我的问题是:如何将de-DE语言环境信息导入测试环境,以便国际服务可以动态识别它?
该fixture.whenStable().then(()=> {})是在测试错误处理造成的问题,所以这是删除。
作为附加信息,该focusout事件将触发组件中的以下方法:
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);
}
Run Code Online (Sandbox Code Playgroud)
因此,当我将焦点从输入HTML元素移开时,显示值将根据语言环境进行格式化。
我建议您尝试导入它,正如您在加载其他区域设置数据一章中链接到的文档中所描述的那样。由于您没有提到加载其他语言环境,我假设您根本没有这样做。否则,请提供如何加载它们的附加信息。
对于 de locale,您需要添加以下导入。
// Load all required data for the de locale
import '@progress/kendo-angular-intl/locales/de/all';
Run Code Online (Sandbox Code Playgroud)