Browse Source

tests(core): fix tests in the config.state.spec

pull/2447/head
mehmet-erim 6 years ago
parent
commit
a7e44b739c
  1. 154
      npm/ng-packs/packages/core/src/lib/tests/config.state.spec.ts

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

@ -1,17 +1,12 @@
import {
createServiceFactory,
SpectatorService,
SpyObject,
} from '@ngneat/spectator/jest';
import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest';
import { Store } from '@ngxs/store';
import { ReplaySubject, timer, Subject, of } from 'rxjs';
import { Config } from '../models/config';
import {
ApplicationConfigurationService,
ConfigStateService,
} from '../services';
import { ApplicationConfigurationService, ConfigStateService } from '../services';
import { ConfigState } from '../states';
import { SetLanguage, PatchRouteByName } from '../actions';
import { SetLanguage, PatchRouteByName, AddRoute } from '../actions';
import clone from 'just-clone';
import { ABP } from '../models';
export const CONFIG_STATE_DATA = {
environment: {
@ -55,6 +50,7 @@ export const CONFIG_STATE_DATA = {
name: 'AbpAccount::Login',
order: 1,
url: '/account/login',
parentName: 'AbpAccount::Menu:Account',
},
],
url: '/account',
@ -68,10 +64,27 @@ export const CONFIG_STATE_DATA = {
url: '/',
},
{
name: '::Menu:Identity',
path: 'identity',
children: [],
url: '/identity',
name: 'AbpAccount::Menu:Account',
path: 'account',
invisible: true,
layout: 'application',
children: [
{
path: 'login',
name: 'AbpAccount::Login',
order: 1,
url: '/account/login',
parentName: 'AbpAccount::Menu:Account',
},
],
url: '/account',
},
{
path: 'login',
name: 'AbpAccount::Login',
order: 1,
url: '/account/login',
parentName: 'AbpAccount::Menu:Account',
},
],
localization: {
@ -134,10 +147,7 @@ describe('ConfigState', () => {
store = spectator.get(Store);
service = spectator.service;
appConfigService = spectator.get(ApplicationConfigurationService);
state = new ConfigState(
spectator.get(ApplicationConfigurationService),
store,
);
state = new ConfigState(spectator.get(ApplicationConfigurationService), store);
});
describe('#getAll', () => {
@ -165,16 +175,12 @@ describe('ConfigState', () => {
describe('#getDeep', () => {
it('should return deeper', () => {
expect(
ConfigState.getDeep('environment.localization.defaultResourceName')(
CONFIG_STATE_DATA,
),
ConfigState.getDeep('environment.localization.defaultResourceName')(CONFIG_STATE_DATA),
).toEqual(CONFIG_STATE_DATA.environment.localization.defaultResourceName);
expect(
ConfigState.getDeep([
'environment',
'localization',
'defaultResourceName',
])(CONFIG_STATE_DATA),
ConfigState.getDeep(['environment', 'localization', 'defaultResourceName'])(
CONFIG_STATE_DATA,
),
).toEqual(CONFIG_STATE_DATA.environment.localization.defaultResourceName);
expect(ConfigState.getDeep('test')(null)).toBeFalsy();
@ -183,10 +189,10 @@ describe('ConfigState', () => {
describe('#getRoute', () => {
it('should return route', () => {
expect(
ConfigState.getRoute(null, '::Menu:Home')(CONFIG_STATE_DATA),
).toEqual(CONFIG_STATE_DATA.flattedRoutes[0]);
expect(ConfigState.getRoute('identity')(CONFIG_STATE_DATA)).toEqual(
expect(ConfigState.getRoute(null, '::Menu:Home')(CONFIG_STATE_DATA)).toEqual(
CONFIG_STATE_DATA.flattedRoutes[0],
);
expect(ConfigState.getRoute('account')(CONFIG_STATE_DATA)).toEqual(
CONFIG_STATE_DATA.flattedRoutes[1],
);
});
@ -205,11 +211,7 @@ describe('ConfigState', () => {
describe('#getSetting', () => {
it('should return a setting', () => {
expect(
ConfigState.getSetting('Abp.Localization.DefaultLanguage')(
CONFIG_STATE_DATA,
),
).toEqual(
expect(ConfigState.getSetting('Abp.Localization.DefaultLanguage')(CONFIG_STATE_DATA)).toEqual(
CONFIG_STATE_DATA.setting.values['Abp.Localization.DefaultLanguage'],
);
});
@ -217,9 +219,7 @@ describe('ConfigState', () => {
describe('#getSettings', () => {
it('should return settings', () => {
expect(
ConfigState.getSettings('Localization')(CONFIG_STATE_DATA),
).toEqual({
expect(ConfigState.getSettings('Localization')(CONFIG_STATE_DATA)).toEqual({
'Abp.Localization.DefaultLanguage': 'en',
});
@ -231,45 +231,31 @@ describe('ConfigState', () => {
describe('#getGrantedPolicy', () => {
it('should return a granted policy', () => {
expect(
ConfigState.getGrantedPolicy('Abp.Identity')(CONFIG_STATE_DATA),
).toBe(false);
expect(
ConfigState.getGrantedPolicy('Abp.Identity || Abp.Account')(
CONFIG_STATE_DATA,
),
).toBe(true);
expect(
ConfigState.getGrantedPolicy('Abp.Account && Abp.Identity')(
CONFIG_STATE_DATA,
),
).toBe(false);
expect(
ConfigState.getGrantedPolicy('Abp.Account &&')(CONFIG_STATE_DATA),
).toBe(false);
expect(
ConfigState.getGrantedPolicy('|| Abp.Account')(CONFIG_STATE_DATA),
).toBe(false);
expect(ConfigState.getGrantedPolicy('Abp.Identity')(CONFIG_STATE_DATA)).toBe(false);
expect(ConfigState.getGrantedPolicy('Abp.Identity || Abp.Account')(CONFIG_STATE_DATA)).toBe(
true,
);
expect(ConfigState.getGrantedPolicy('Abp.Account && Abp.Identity')(CONFIG_STATE_DATA)).toBe(
false,
);
expect(ConfigState.getGrantedPolicy('Abp.Account &&')(CONFIG_STATE_DATA)).toBe(false);
expect(ConfigState.getGrantedPolicy('|| Abp.Account')(CONFIG_STATE_DATA)).toBe(false);
expect(ConfigState.getGrantedPolicy('')(CONFIG_STATE_DATA)).toBe(true);
});
});
describe('#getLocalization', () => {
it('should return a localization', () => {
expect(
ConfigState.getLocalization('AbpIdentity::Identity')(CONFIG_STATE_DATA),
).toBe('identity');
expect(ConfigState.getLocalization('AbpIdentity::Identity')(CONFIG_STATE_DATA)).toBe(
'identity',
);
expect(
ConfigState.getLocalization('AbpIdentity::NoIdentity')(
CONFIG_STATE_DATA,
),
).toBe('AbpIdentity::NoIdentity');
expect(ConfigState.getLocalization('AbpIdentity::NoIdentity')(CONFIG_STATE_DATA)).toBe(
'AbpIdentity::NoIdentity',
);
expect(
ConfigState.getLocalization({ key: '', defaultValue: 'default' })(
CONFIG_STATE_DATA,
),
ConfigState.getLocalization({ key: '', defaultValue: 'default' })(CONFIG_STATE_DATA),
).toBe('default');
expect(
@ -290,9 +276,7 @@ describe('ConfigState', () => {
});
expect(false).toBeTruthy(); // fail
} catch (error) {
expect((error as Error).message).toContain(
'Please check your environment',
);
expect((error as Error).message).toContain('Please check your environment');
}
});
});
@ -328,11 +312,11 @@ describe('ConfigState', () => {
});
describe('#PatchRouteByName', () => {
it('should should patch the route', () => {
it('should patch the route', () => {
let patchStateArg;
const patchState = jest.fn(s => (patchStateArg = s));
const getState = jest.fn(() => CONFIG_STATE_DATA);
const getState = jest.fn(() => clone(CONFIG_STATE_DATA));
state.patchRoute(
{ patchState, getState } as any,
@ -347,17 +331,21 @@ describe('ConfigState', () => {
name: 'Home',
path: 'home',
url: '/home',
children: [
{ path: 'dashboard', name: 'Dashboard', url: '/home/dashboard' },
],
children: [{ path: 'dashboard', name: 'Dashboard', url: '/home/dashboard' }],
});
expect(patchStateArg.flattedRoutes[0]).toEqual({
name: 'Home',
path: 'home',
url: '/home',
children: [{ path: 'dashboard', name: 'Dashboard', url: '/home/dashboard' }],
});
});
it('should should patch the route without path', () => {
it('should patch the route without path', () => {
let patchStateArg;
const patchState = jest.fn(s => (patchStateArg = s));
const getState = jest.fn(() => CONFIG_STATE_DATA);
const getState = jest.fn(() => clone(CONFIG_STATE_DATA));
state.patchRoute(
{ patchState, getState } as any,
@ -373,6 +361,16 @@ describe('ConfigState', () => {
url: '/',
children: [{ path: 'dashboard', name: 'Dashboard', url: '/dashboard' }],
});
expect(patchStateArg.flattedRoutes[0]).toEqual({
name: 'Main',
path: '',
url: '/',
children: [{ path: 'dashboard', name: 'Dashboard', url: '/dashboard' }],
});
});
});
describe('#AddRoute', () => {
const newRoute = {
name: 'My new page',

Loading…
Cancel
Save