Browse Source

test: resolve localization.service.sped errors

pull/6343/head
mehmet-erim 6 years ago
parent
commit
96333a0475
  1. 102
      npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts

102
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 { Router } from '@angular/router';
import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest'; import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest';
import { Actions, Store } from '@ngxs/store'; import { Store } from '@ngxs/store';
import { BehaviorSubject, of, Subject } from 'rxjs'; import { BehaviorSubject, of } from 'rxjs';
import {
ApplicationConfigurationService,
ConfigStateService,
SessionStateService,
} from '../services';
import { LocalizationService } from '../services/localization.service'; 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; const shouldReuseRoute = () => true;
describe('LocalizationService', () => { describe('LocalizationService', () => {
let spectator: SpectatorService<LocalizationService>; let spectator: SpectatorService<LocalizationService>;
let store: SpyObject<Store>; let sessionState: SpyObject<SessionStateService>;
let configState: SpyObject<ConfigStateService>;
let router: SpyObject<Router>; let router: SpyObject<Router>;
let service: LocalizationService; let service: LocalizationService;
let appConfigService: ApplicationConfigurationService;
const createService = createServiceFactory({ const createService = createServiceFactory({
service: LocalizationService, service: LocalizationService,
entryComponents: [], entryComponents: [],
mocks: [Store, Router], mocks: [ApplicationConfigurationService, Router],
providers: [ providers: [
{ provide: Actions, useValue: new Subject() },
{ {
provide: CORE_OPTIONS, provide: CORE_OPTIONS,
useValue: { registerLocaleFn: () => Promise.resolve(), cultureNameLocaleFileMap: {} }, useValue: { registerLocaleFn: () => Promise.resolve(), cultureNameLocaleFileMap: {} },
@ -29,13 +36,16 @@ describe('LocalizationService', () => {
beforeEach(() => { beforeEach(() => {
spectator = createService(); spectator = createService();
store = spectator.inject(Store); sessionState = spectator.inject(SessionStateService);
store.dispatch.mockReturnValue(new BehaviorSubject('tr')); configState = spectator.inject(ConfigStateService);
router = spectator.inject(Router); router = spectator.inject(Router);
router.routeReuseStrategy = { shouldReuseRoute } as any; router.routeReuseStrategy = { shouldReuseRoute } as any;
service = spectator.service; 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'); sessionState.setLanguage('tr');
}); });
@ -49,20 +59,19 @@ describe('LocalizationService', () => {
}); });
describe('#get', () => { describe('#get', () => {
it('should be return an observable localization', async () => { it('should be return an observable localization', done => {
store.select.andReturn(of('AbpTest')); service.get('AbpIdentity::Identity').subscribe(localization => {
expect(localization).toBe(CONFIG_STATE_DATA.localization.values.AbpIdentity.Identity);
const localization = await service.get('AbpTest').toPromise(); done();
});
expect(localization).toBe('AbpTest');
}); });
}); });
describe('#instant', () => { describe('#instant', () => {
it('should be return a localization', () => { 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 () => { it('should throw an error message when service have an otherInstance', async () => {
try { try {
const instance = new LocalizationService( const instance = new LocalizationService(
{ getLanguage: () => {} } as any, sessionState,
spectator.inject(Injector),
null, null,
null, null,
null, null,
@ -120,16 +130,16 @@ describe('LocalizationService', () => {
`( `(
'should return observable $expected when resource name is $resource and key is $key', 'should return observable $expected when resource name is $resource and key is $key',
async ({ resource, key, defaultValue, expected }) => { async ({ resource, key, defaultValue, expected }) => {
store.select.andReturn( configState.setState({
of({ localization: {
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, values: { foo: { bar: 'baz' }, x: { y: 'z' } },
defaultResourceName: 'x', 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', 'should return $expected when resource name is $resource and key is $key',
({ resource, key, defaultValue, expected }) => { ({ resource, key, defaultValue, expected }) => {
store.selectSnapshot.andReturn({ configState.setState({
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, localization: {
defaultResourceName: 'x', values: { foo: { bar: 'baz' }, x: { y: 'z' } },
defaultResourceName: 'x',
},
}); });
const result = service.localizeSync(resource, key, defaultValue); 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', 'should return observable $expected when resource names are $resources and keys are $keys',
async ({ resources, keys, defaultValue, expected }) => { async ({ resources, keys, defaultValue, expected }) => {
store.select.andReturn( configState.setState({
of({ localization: {
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, values: { foo: { bar: 'baz' }, x: { y: 'z' } },
defaultResourceName: 'x', 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', 'should return $expected when resource names are $resources and keys are $keys',
({ resources, keys, defaultValue, expected }) => { ({ resources, keys, defaultValue, expected }) => {
store.selectSnapshot.andReturn({ configState.setState({
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, localization: {
defaultResourceName: 'x', values: { foo: { bar: 'baz' }, x: { y: 'z' } },
defaultResourceName: 'x',
},
}); });
const result = service.localizeWithFallbackSync(resources, keys, defaultValue); 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.');
});
});
}); });

Loading…
Cancel
Save