Skip to content

Commit

Permalink
feat(module: i18n): add test for locale zh_TW
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Mar 27, 2019
1 parent 6583036 commit 14f1399
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
26 changes: 25 additions & 1 deletion components/i18n/date-helper.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Injector } from '@angular/core';
import { TestBed } from '@angular/core/testing';

import ngZhHantHK from '@angular/common/locales/zh-Hant-HK';

import enDateLocale from 'date-fns/locale/en';
import { NZ_DATE_CONFIG } from './date-config';
import { DateHelperByDatePipe, DateHelperService } from './date-helper.service';
import en_US from './languages/en_US';
import zh_TW from './languages/zh_TW';
import { NzI18nModule } from './nz-i18n.module';
import { NZ_DATE_LOCALE, NZ_I18N } from './nz-i18n.token';
import { NZ_DATE_LOCALE, NZ_I18N, NZ_I18N_LOCALEID } from './nz-i18n.token';

describe('DateHelperService', () => {
let injector: Injector;
Expand Down Expand Up @@ -37,6 +40,27 @@ describe('DateHelperService', () => {
});
});

describe('Formatting with DatePipe (use zh_TW)', () => {
beforeEach(() => {
injector = TestBed.configureTestingModule({
imports: [NzI18nModule],
providers: [{ provide: NZ_I18N, useValue: zh_TW }, { provide: NZ_I18N_LOCALEID, useValue: ngZhHantHK[0] }]
});

dateHelper = injector.get(DateHelperService);
});

it('should do formatting by DatePipe', () => {
expect(dateHelper instanceof DateHelperByDatePipe).toBeTruthy();
});

it('should do formatting correctly', () => {
const date = new Date('2018-12-31 12:11:10');
expect(dateHelper.format(date, 'yyyy-MM-dd')).toBe('2018-12-31');
expect(dateHelper.format(date, 'yyyy-ww')).toBe('2018-53');
});
});

describe('Formatting with Data-fns', () => {
beforeEach(() => {
injector = TestBed.configureTestingModule({
Expand Down
13 changes: 11 additions & 2 deletions components/i18n/nz-i18n.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { Injector } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import en_US from './languages/en_US';
import zh_CN from './languages/zh_CN';
import zh_TW from './languages/zh_TW';
import { NzI18nModule } from './nz-i18n.module';
import { NzI18nService } from './nz-i18n.service';

import zhHantHK from '@angular/common/locales/zh-Hant-HK';

describe('nz-i18n.service', () => {
let injector: Injector;
let srv: NzI18nService;
Expand All @@ -22,10 +25,16 @@ describe('nz-i18n.service', () => {
// tslint:disable:no-string-literal
it('should be auto default zh_CN', () => {
expect(srv.getLocale().locale).toBe(DEFAULT_LAN.locale);
});
it('ng locale id should equal default locale', () => {
expect(srv.getLocaleId()).toBe(DEFAULT_LAN.locale);
expect(srv.getNgLocaleId()).toBe(DEFAULT_LAN.locale);
});
it('should set ngLocaleId to zh-hant-hk locale', () => {
spyOn(srv['_change'], 'next');
expect(srv['_change'].next).not.toHaveBeenCalled();
srv.setLocale(zh_TW, zhHantHK[0] + '');
expect(srv.getNgLocaleId()).toBe(zhHantHK[0]);
expect(srv.getLocaleId()).toBe(zh_TW.locale);
});
it('should trigger changed when set different lang', () => {
spyOn(srv['_change'], 'next');
expect(srv['_change'].next).not.toHaveBeenCalled();
Expand Down
2 changes: 1 addition & 1 deletion components/i18n/nz-i18n.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class NzI18nService {
if (this._locale && this._locale.locale === locale.locale) {
return;
}
this._ngLocaleId = ngLocaleId || locale.locale;
this._ngLocaleId = ngLocaleId;
this._locale = locale;
this._change.next(locale);
}
Expand Down

0 comments on commit 14f1399

Please sign in to comment.