diff --git a/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts index 3254ede573..b64093b609 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts @@ -1,25 +1,32 @@ -import { CORE_OPTIONS } from '../tokens/options.token'; +import { Injector } from '@angular/core'; import { Router } from '@angular/router'; import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest'; -import { Actions, Store } from '@ngxs/store'; -import { BehaviorSubject, of, Subject } from 'rxjs'; +import { Store } from '@ngxs/store'; +import { BehaviorSubject, of } from 'rxjs'; +import { + ApplicationConfigurationService, + ConfigStateService, + SessionStateService, +} from '../services'; import { LocalizationService } from '../services/localization.service'; -import { SessionStateService } from '../services'; +import { CORE_OPTIONS } from '../tokens/options.token'; +import { CONFIG_STATE_DATA } from './config-state.service.spec'; const shouldReuseRoute = () => true; describe('LocalizationService', () => { let spectator: SpectatorService; - let store: SpyObject; + let sessionState: SpyObject; + let configState: SpyObject; let router: SpyObject; let service: LocalizationService; + let appConfigService: ApplicationConfigurationService; const createService = createServiceFactory({ service: LocalizationService, entryComponents: [], - mocks: [Store, Router], + mocks: [ApplicationConfigurationService, Router], providers: [ - { provide: Actions, useValue: new Subject() }, { provide: CORE_OPTIONS, useValue: { registerLocaleFn: () => Promise.resolve(), cultureNameLocaleFileMap: {} }, @@ -29,13 +36,16 @@ describe('LocalizationService', () => { beforeEach(() => { spectator = createService(); - store = spectator.inject(Store); - store.dispatch.mockReturnValue(new BehaviorSubject('tr')); + sessionState = spectator.inject(SessionStateService); + configState = spectator.inject(ConfigStateService); router = spectator.inject(Router); router.routeReuseStrategy = { shouldReuseRoute } as any; service = spectator.service; + appConfigService = spectator.inject(ApplicationConfigurationService); - const sessionState = spectator.inject(SessionStateService); + const getConfigurationSpy = jest.spyOn(appConfigService, 'getConfiguration'); + getConfigurationSpy.mockReturnValue(of(CONFIG_STATE_DATA)); + configState.setState(CONFIG_STATE_DATA); sessionState.setLanguage('tr'); }); @@ -49,20 +59,19 @@ describe('LocalizationService', () => { }); describe('#get', () => { - it('should be return an observable localization', async () => { - store.select.andReturn(of('AbpTest')); - - const localization = await service.get('AbpTest').toPromise(); - - expect(localization).toBe('AbpTest'); + it('should be return an observable localization', done => { + service.get('AbpIdentity::Identity').subscribe(localization => { + expect(localization).toBe(CONFIG_STATE_DATA.localization.values.AbpIdentity.Identity); + done(); + }); }); }); describe('#instant', () => { it('should be return a localization', () => { - store.selectSnapshot.andReturn('AbpTest'); + const localization = service.instant('AbpIdentity::Identity'); - expect(service.instant('AbpTest')).toBe('AbpTest'); + expect(localization).toBe(CONFIG_STATE_DATA.localization.values.AbpIdentity.Identity); }); }); @@ -81,7 +90,8 @@ describe('LocalizationService', () => { it('should throw an error message when service have an otherInstance', async () => { try { const instance = new LocalizationService( - { getLanguage: () => {} } as any, + sessionState, + spectator.inject(Injector), null, null, null, @@ -120,16 +130,16 @@ describe('LocalizationService', () => { `( 'should return observable $expected when resource name is $resource and key is $key', async ({ resource, key, defaultValue, expected }) => { - store.select.andReturn( - of({ + configState.setState({ + localization: { values: { foo: { bar: 'baz' }, x: { y: 'z' } }, defaultResourceName: 'x', - }), - ); - - const result = await service.localize(resource, key, defaultValue).toPromise(); + }, + }); - expect(result).toBe(expected); + service.localize(resource, key, defaultValue).subscribe(result => { + expect(result).toBe(expected); + }); }, ); }); @@ -161,9 +171,11 @@ describe('LocalizationService', () => { `( 'should return $expected when resource name is $resource and key is $key', ({ resource, key, defaultValue, expected }) => { - store.selectSnapshot.andReturn({ - values: { foo: { bar: 'baz' }, x: { y: 'z' } }, - defaultResourceName: 'x', + configState.setState({ + localization: { + values: { foo: { bar: 'baz' }, x: { y: 'z' } }, + defaultResourceName: 'x', + }, }); const result = service.localizeSync(resource, key, defaultValue); @@ -205,18 +217,16 @@ describe('LocalizationService', () => { `( 'should return observable $expected when resource names are $resources and keys are $keys', async ({ resources, keys, defaultValue, expected }) => { - store.select.andReturn( - of({ + configState.setState({ + localization: { values: { foo: { bar: 'baz' }, x: { y: 'z' } }, defaultResourceName: 'x', - }), - ); - - const result = await service - .localizeWithFallback(resources, keys, defaultValue) - .toPromise(); + }, + }); - expect(result).toBe(expected); + service.localizeWithFallback(resources, keys, defaultValue).subscribe(result => { + expect(result).toBe(expected); + }); }, ); }); @@ -253,9 +263,11 @@ describe('LocalizationService', () => { `( 'should return $expected when resource names are $resources and keys are $keys', ({ resources, keys, defaultValue, expected }) => { - store.selectSnapshot.andReturn({ - values: { foo: { bar: 'baz' }, x: { y: 'z' } }, - defaultResourceName: 'x', + configState.setState({ + localization: { + values: { foo: { bar: 'baz' }, x: { y: 'z' } }, + defaultResourceName: 'x', + }, }); const result = service.localizeWithFallbackSync(resources, keys, defaultValue); @@ -264,4 +276,12 @@ describe('LocalizationService', () => { }, ); }); + + describe('#getLocalization', () => { + it('should return a localization', () => { + expect( + service.instant("MyProjectName::'{0}' and '{1}' do not match.", 'first', 'second'), + ).toBe('first and second do not match.'); + }); + }); });