Merge pull request #1836 from abpframework/feature/tests

Feature/tests
pull/1838/head
Yasin Aydın 6 years ago committed by GitHub
commit 00fc215a7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,13 +23,12 @@ export class LocalizationService {
if (otherInstance) throw new Error('LocaleService should have only one instance.');
}
private setRouteReuse(reuse: ShouldReuseRoute) {
setRouteReuse(reuse: ShouldReuseRoute) {
this.router.routeReuseStrategy.shouldReuseRoute = reuse;
}
registerLocale(locale: string) {
const { shouldReuseRoute } = this.router.routeReuseStrategy;
this.setRouteReuse(() => false);
this.router.navigated = false;
@ -41,11 +40,11 @@ export class LocalizationService {
});
}
get(keys: string, ...interpolateParams: string[]): Observable<string> {
return this.store.select(state => state.ConfigState.getCopy(keys, ...interpolateParams));
get(key: string, ...interpolateParams: string[]): Observable<string> {
return this.store.select(state => state.ConfigState.getLocalization(key, ...interpolateParams));
}
instant(keys: string, ...interpolateParams: string[]): string {
return this.store.selectSnapshot(state => state.ConfigState.getCopy(keys, ...interpolateParams));
instant(key: string, ...interpolateParams: string[]): string {
return this.store.selectSnapshot(state => state.ConfigState.getLocalization(key, ...interpolateParams));
}
}

@ -113,6 +113,11 @@ export class ConfigState {
return selector;
}
/**
*
* @param deprecated, Use getLocalization instead. To be delete in v1
*
*/
static getCopy(key: string, ...interpolateParams: string[]) {
if (!key) key = '';
@ -161,6 +166,54 @@ export class ConfigState {
return selector;
}
static getLocalization(key: string, ...interpolateParams: string[]) {
if (!key) key = '';
const keys = key.split('::') as string[];
const selector = createSelector(
[ConfigState],
(state: Config.State) => {
if (!state.localization) return key;
const { defaultResourceName } = state.environment.localization;
if (keys[0] === '') {
if (!defaultResourceName) {
throw new Error(
`Please check your environment. May you forget set defaultResourceName?
Here is the example:
{ production: false,
localization: {
defaultResourceName: 'MyProjectName'
}
}`,
);
}
keys[0] = snq(() => defaultResourceName);
}
let localization = (keys as any).reduce((acc, val) => {
if (acc) {
return acc[val];
}
return undefined;
}, state.localization.values);
interpolateParams = interpolateParams.filter(params => params != null);
if (localization && interpolateParams && interpolateParams.length) {
interpolateParams.forEach(param => {
localization = localization.replace(/[\'\"]?\{[\d]+\}[\'\"]?/, param);
});
}
return localization || key;
},
);
return selector;
}
constructor(private appConfigurationService: ApplicationConfigurationService, private store: Store) {}
@Action(GetAppConfiguration)

@ -0,0 +1,81 @@
import { Router } from '@angular/router';
import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator';
import { Store } from '@ngxs/store';
import { Observable, of } from 'rxjs';
import { LocalizationService } from '../services/localization.service';
describe('LocalizationService', () => {
let spectator: SpectatorService<LocalizationService>;
let store: SpyObject<Store>;
let service: LocalizationService;
const createService = createServiceFactory({
service: LocalizationService,
entryComponents: [],
mocks: [Store, Router],
});
beforeEach(() => {
spectator = createService();
store = spectator.get(Store);
service = spectator.service;
});
describe('#currentLang', () => {
it('should be tr', () => {
store.selectSnapshot.andCallFake((selector: (state: any, ...states: any[]) => string) => {
return selector({ SessionState: { getLanguage: 'tr' } });
});
expect(service.currentLang).toBe('tr');
});
});
describe('#get', () => {
it('should be return an observable localization', async () => {
store.select.andCallFake((selector: (state: any, ...states: any[]) => Observable<string>) => {
return selector({ ConfigState: { getLocalization: (keys, ...interpolateParams) => of(keys) } });
});
const localization = await service.get('AbpTest').toPromise();
expect(localization).toBe('AbpTest');
});
});
describe('#instant', () => {
it('should be return a localization', () => {
store.selectSnapshot.andCallFake((selector: (state: any, ...states: any[]) => Observable<string>) => {
return selector({ ConfigState: { getLocalization: (keys, ...interpolateParams) => keys } });
});
expect(service.instant('AbpTest')).toBe('AbpTest');
});
});
describe('#registerLocale', () => {
it('should return registerLocale and then call setRouteReuse', () => {
const router = spectator.get(Router);
const shouldReuseRoute = () => true;
router.routeReuseStrategy = { shouldReuseRoute } as any;
router.navigateByUrl.andCallFake(url => {
return new Promise(resolve => resolve({ catch: () => null }));
});
service.registerLocale('tr');
expect(router.navigated).toBe(false);
expect(router.routeReuseStrategy.shouldReuseRoute).not.toEqual(shouldReuseRoute);
});
it('should throw an error message when service have an otherInstance', async () => {
try {
const instance = new LocalizationService(null, null, null, {} as any);
} catch (error) {
expect((error as Error).message).toBe('LocaleService should have only one instance.');
}
});
});
});

@ -19,11 +19,10 @@ export class OverwritePlugin implements NgxsPlugin {
if (action instanceof InitState && !this.initialized) {
state = { ...state, ...this.options };
console.log(state);
this.initialized = true;
}
if (action instanceof StateOverwrite) {
if (type === StateOverwrite.type) {
state = setValue(state, action.payload.stateName, action.payload.value);
}

@ -47,7 +47,7 @@
"no-arg": true,
"no-bitwise": true,
"no-conflicting-lifecycle": true,
"no-console": [true, "debug", "info", "time", "timeEnd", "trace"],
"no-console": [true, "debug", "info", "time", "timeEnd", "trace", "log"],
"no-construct": true,
"no-debugger": true,
"no-duplicate-super": true,

Loading…
Cancel
Save