bixi
Version:
企业级中后台前端解决方案
100 lines (81 loc) • 2.74 kB
text/typescript
import { Component, OnDestroy } from '@angular/core';
import { ComponentFixture, inject, TestBed } from '@angular/core/testing';
import { Subscription } from 'rxjs';
import en_US from '../langs/en-US';
import zh_CN from '../langs/zh-CN';
import { II18nConfig } from './i18n.type';
import { BixiI18nModule } from './i18n.module';
import { BixiI18nService } from './i18n.service';
import { BIXI_I18N } from './i18n.token';
describe('i18n service', () => {
let srv: BixiI18nService;
let fixture: ComponentFixture<TestComponent>;
let testComponent: TestComponent;
const DEFAULT_LAN = zh_CN;
describe('# setLocale', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
imports: [BixiI18nModule]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
testComponent = fixture.debugElement.componentInstance;
});
beforeEach(inject([BixiI18nService], (s: BixiI18nService) => {
srv = s;
}));
it('should be auto default zh_CN', () => {
expect(testComponent.locale.locale).toBe(DEFAULT_LAN.locale);
});
it('should trigger changed when set different lang', () => {
const spy = spyOn(testComponent, 'updateLocale');
expect(spy).not.toHaveBeenCalled();
srv.setLocale(en_US);
expect(spy).toHaveBeenCalledTimes(1);
});
it('should not trigger change when set same lang', () => {
const spy = spyOn(testComponent, 'updateLocale');
expect(spy).not.toHaveBeenCalled();
srv.setLocale(zh_CN);
expect(spy).not.toHaveBeenCalled();
});
});
describe('# should be set cutomer value', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
imports: [BixiI18nModule],
providers: [{
provide: BIXI_I18N, useValue: {
locale: 'hello'
}, multi: false
}]
}).compileComponents();
fixture = TestBed.createComponent(TestComponent);
testComponent = fixture.debugElement.componentInstance;
});
it('should be set locale hello', () => {
expect(testComponent.locale.locale).toBe('hello');
});
})
});
({
template: ''
})
export class TestComponent implements OnDestroy {
locale!: II18nConfig;
private localeSubscription: Subscription;
constructor(private nzI18nService: BixiI18nService) {
this.localeSubscription = this.nzI18nService.localeChange.subscribe(locale => {
this.updateLocale(locale);
});
}
ngOnDestroy(): void {
this.localeSubscription.unsubscribe();
}
updateLocale(locale: II18nConfig): void {
this.locale = locale;
}
}