Browse Source

test(core): config.state.spec

pull/1918/head
mehmet-erim 7 years ago
parent
commit
bdd5d9ab23
  1. 8
      npm/ng-packs/packages/core/src/lib/states/config.state.ts
  2. 88
      npm/ng-packs/packages/core/src/lib/tests/config.state.spec.ts

8
npm/ng-packs/packages/core/src/lib/states/config.state.ts

@ -278,18 +278,16 @@ function patchRouteDeep(
routes: ABP.FullRoute[],
name: string,
newValue: Partial<ABP.FullRoute>,
parentUrl: string = null,
parentUrl: string = '',
): ABP.FullRoute[] {
routes = routes.map(route => {
if (route.name === name) {
if (newValue.path) {
newValue.url = `${parentUrl}/${newValue.path}`;
}
newValue.url = `${parentUrl}/${(!newValue.path && newValue.path === '' ? route.path : newValue.path) || ''}`;
if (newValue.children && newValue.children.length) {
newValue.children = newValue.children.map(child => ({
...child,
url: `${parentUrl}/${route.path}/${child.path}`,
url: `${newValue.url}/${child.path}`.replace('//', '/'),
}));
}

88
npm/ng-packs/packages/core/src/lib/tests/config.state.spec.ts

@ -1,11 +1,10 @@
import { Router } from '@angular/router';
import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest';
import { Store, NgxsModule } from '@ngxs/store';
import { Observable, of } from 'rxjs';
import { ConfigService, ApplicationConfigurationService, RestService } from '../services';
import { ConfigState } from '../states';
import { HttpClient } from '@angular/common/http';
import { Store } from '@ngxs/store';
import { ReplaySubject, timer, Subject, of } from 'rxjs';
import { Config } from '../models/config';
import { ApplicationConfigurationService, ConfigService } from '../services';
import { ConfigState } from '../states';
import { SetLanguage, PatchRouteByName } from '../actions';
export const CONFIG_STATE_DATA = {
environment: {
@ -115,6 +114,7 @@ describe('ConfigService', () => {
let store: SpyObject<Store>;
let service: ConfigService;
let state: ConfigState;
let appConfigService: SpyObject<ApplicationConfigurationService>;
const createService = createServiceFactory({
service: ConfigService,
@ -125,6 +125,7 @@ describe('ConfigService', () => {
spectator = createService();
store = spectator.get(Store);
service = spectator.service;
appConfigService = spectator.get(ApplicationConfigurationService);
state = new ConfigState(spectator.get(ApplicationConfigurationService), store);
});
@ -223,8 +224,79 @@ describe('ConfigService', () => {
});
describe('#GetAppConfiguration', () => {
it('should call the getConfiguration of ApplicationConfigurationService and patch the state', () => {
// state.addData()
it('should call the getConfiguration of ApplicationConfigurationService and patch the state', done => {
let patchStateArg;
let dispatchArg;
const configuration = {
setting: { values: { 'Abp.Localization.DefaultLanguage': 'tr;TR' } },
};
const res$ = new ReplaySubject(1);
res$.next(configuration);
const patchState = jest.fn(s => (patchStateArg = s));
const dispatch = jest.fn(a => {
dispatchArg = a;
return of(a);
});
appConfigService.getConfiguration.andReturn(res$);
state.addData({ patchState, dispatch } as any).subscribe();
timer(0).subscribe(() => {
expect(patchStateArg).toEqual(configuration);
expect(dispatchArg instanceof SetLanguage).toBeTruthy();
expect(dispatchArg).toEqual({ payload: 'tr' });
done();
});
});
});
describe('#PatchRouteByName', () => {
it('should should patch the route', () => {
let patchStateArg;
const patchState = jest.fn(s => (patchStateArg = s));
const getState = jest.fn(() => CONFIG_STATE_DATA);
state.patchRoute(
{ patchState, getState } as any,
new PatchRouteByName('::Menu:Home', {
name: 'Home',
path: 'home',
children: [{ path: 'dashboard', name: 'Dashboard' }],
}),
);
expect(patchStateArg.routes[0]).toEqual({
name: 'Home',
path: 'home',
url: '/home',
children: [{ path: 'dashboard', name: 'Dashboard', url: '/home/dashboard' }],
});
});
it('should should patch the route without path', () => {
let patchStateArg;
const patchState = jest.fn(s => (patchStateArg = s));
const getState = jest.fn(() => CONFIG_STATE_DATA);
state.patchRoute(
{ patchState, getState } as any,
new PatchRouteByName('::Menu:Home', {
name: 'Main',
children: [{ path: 'dashboard', name: 'Dashboard' }],
}),
);
expect(patchStateArg.routes[0]).toEqual({
name: 'Main',
path: '',
url: '/',
children: [{ path: 'dashboard', name: 'Dashboard', url: '/dashboard' }],
});
});
});
});

Loading…
Cancel
Save