I had multiple tests for a very specific numeric component that handles locale formatting. The tests were fine with Karma, but once we changed it to Jest this error started to appear:
NoLocale: Missing locale info for 'de-DE' Solution: http://www.telerik.com/kendo-angular-ui/components/internationalization/troubleshooting/#toc-no-locale
The IntlService
is initialized with en-US
locale id, so I changed it for each test, depending the format that I wanted to assert (de-DE
or en-GB
).
These are the test examples in the component.spec.ts:
numeric-textbox.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();
fixture.whenStable().then(() => {
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();
fixture.whenStable().then(() => {
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');
});
});
The rare thing is 'en-GB' locale is recognised by the Intl Service (so the test is running fine), but 'de-DE' is not.
My question is: How do I import the de-DE
locale information to the test environment, so that it can be recognised by the Intl Service dynamically?
As an additional information, the focusout
event triggers the following method in the component:
numeric-textbox.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);
}
So, when I made focus out of the input HTML element, the display value is formatted depending the locale.
from Missing locale info for Kendo Intl Service when testing with Jest in Angular 7
No comments:
Post a Comment