From 537fa0238d365b5fc447df36260742f2effd4459 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Mon, 14 Oct 2019 12:48:22 +0300 Subject: [PATCH] test(core): create config.state.spec --- .../core/src/lib/states/config.state.ts | 2 +- .../core/src/lib/tests/config.state.spec.ts | 224 ++++++++++++++++++ .../core/src/lib/tests/state-overwrite.ts | 45 ---- 3 files changed, 225 insertions(+), 46 deletions(-) create mode 100644 npm/ng-packs/packages/core/src/lib/tests/config.state.spec.ts delete mode 100644 npm/ng-packs/packages/core/src/lib/tests/state-overwrite.ts diff --git a/npm/ng-packs/packages/core/src/lib/states/config.state.ts b/npm/ng-packs/packages/core/src/lib/states/config.state.ts index 08bb79a171..1dfb454fff 100644 --- a/npm/ng-packs/packages/core/src/lib/states/config.state.ts +++ b/npm/ng-packs/packages/core/src/lib/states/config.state.ts @@ -90,7 +90,7 @@ export class ConfigState { return selector; } - static getSetting(key: string, findContain?: boolean) { + static getSetting(key: string) { const selector = createSelector( [ConfigState], (state: Config.State) => { diff --git a/npm/ng-packs/packages/core/src/lib/tests/config.state.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/config.state.spec.ts new file mode 100644 index 0000000000..1cdbb5023f --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tests/config.state.spec.ts @@ -0,0 +1,224 @@ +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 { Config } from '../models/config'; + +export const CONFIG_STATE_DATA = { + environment: { + production: false, + application: { + name: 'MyProjectName', + }, + oAuthConfig: { + issuer: 'https://localhost:44305', + }, + apis: { + default: { + url: 'https://localhost:44305', + }, + other: { + url: 'https://localhost:44306', + }, + }, + localization: { + defaultResourceName: 'MyProjectName', + }, + }, + requirements: { + layouts: [null, null, null], + }, + routes: [ + { + name: '::Menu:Home', + path: '', + children: [], + url: '/', + }, + { + name: 'AbpAccount::Menu:Account', + path: 'account', + invisible: true, + layout: 'application', + children: [ + { + path: 'login', + name: 'AbpAccount::Login', + order: 1, + url: '/account/login', + }, + ], + url: '/account', + }, + ], + flattedRoutes: [ + { + name: '::Menu:Home', + path: '', + children: [], + url: '/', + }, + { + name: '::Menu:Identity', + path: 'identity', + children: [], + url: '/identity', + }, + ], + localization: { + values: { + MyProjectName: { + "'{0}' and '{1}' do not match.": "'{0}' and '{1}' do not match.", + }, + AbpIdentity: { + Identity: 'identity', + }, + }, + languages: [ + { + cultureName: 'cs', + uiCultureName: 'cs', + displayName: 'Čeština', + flagIcon: null, + }, + ], + }, + auth: { + policies: { + 'AbpIdentity.Roles': true, + }, + grantedPolicies: { + 'Abp.Identity': false, + }, + }, + setting: { + values: { + 'Abp.Localization.DefaultLanguage': 'en', + }, + }, + currentUser: { + isAuthenticated: false, + id: null, + tenantId: null, + userName: null, + }, + features: { + values: {}, + }, +} as Config.State; + +describe('ConfigService', () => { + let spectator: SpectatorService; + let store: SpyObject; + let service: ConfigService; + let state: ConfigState; + + const createService = createServiceFactory({ + service: ConfigService, + mocks: [ApplicationConfigurationService, Store], + }); + + beforeEach(() => { + spectator = createService(); + store = spectator.get(Store); + service = spectator.service; + state = new ConfigState(spectator.get(ApplicationConfigurationService), store); + }); + + describe('#getAll', () => { + it('should return CONFIG_STATE_DATA', () => { + expect(ConfigState.getAll(CONFIG_STATE_DATA)).toEqual(CONFIG_STATE_DATA); + }); + }); + + describe('#getApplicationInfo', () => { + it('should return application property', () => { + expect(ConfigState.getApplicationInfo(CONFIG_STATE_DATA)).toEqual(CONFIG_STATE_DATA.environment.application); + }); + }); + + describe('#getOne', () => { + it('should return one property', () => { + expect(ConfigState.getOne('environment')(CONFIG_STATE_DATA)).toEqual(CONFIG_STATE_DATA.environment); + }); + }); + + describe('#getDeep', () => { + it('should return deeper', () => { + expect(ConfigState.getDeep('environment.localization.defaultResourceName')(CONFIG_STATE_DATA)).toEqual( + CONFIG_STATE_DATA.environment.localization.defaultResourceName, + ); + expect(ConfigState.getDeep(['environment', 'localization', 'defaultResourceName'])(CONFIG_STATE_DATA)).toEqual( + CONFIG_STATE_DATA.environment.localization.defaultResourceName, + ); + + expect(ConfigState.getDeep('test')(null)).toBeFalsy(); + }); + }); + + 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(CONFIG_STATE_DATA.flattedRoutes[1]); + }); + }); + + describe('#getApiUrl', () => { + it('should return api url', () => { + expect(ConfigState.getApiUrl('other')(CONFIG_STATE_DATA)).toEqual(CONFIG_STATE_DATA.environment.apis.other.url); + expect(ConfigState.getApiUrl()(CONFIG_STATE_DATA)).toEqual(CONFIG_STATE_DATA.environment.apis.default.url); + }); + }); + + describe('#getSetting', () => { + it('should return a setting', () => { + expect(ConfigState.getSetting('Abp.Localization.DefaultLanguage')(CONFIG_STATE_DATA)).toEqual( + CONFIG_STATE_DATA.setting.values['Abp.Localization.DefaultLanguage'], + ); + }); + }); + + describe('#getSettings', () => { + it('should return settings', () => { + expect(ConfigState.getSettings('Localization')(CONFIG_STATE_DATA)).toEqual({ + 'Abp.Localization.DefaultLanguage': 'en', + }); + + expect(ConfigState.getSettings('AllSettings')(CONFIG_STATE_DATA)).toEqual(CONFIG_STATE_DATA.setting.values); + }); + }); + + describe('#getGrantedPolicy', () => { + it('should return a granted policy', () => { + expect(ConfigState.getGrantedPolicy('Abp.Identity')(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::NoIdentity')(CONFIG_STATE_DATA)).toBe('AbpIdentity::NoIdentity'); + + expect(ConfigState.getLocalization({ key: '', defaultValue: 'default' })(CONFIG_STATE_DATA)).toBe('default'); + + expect(ConfigState.getLocalization("::'{0}' and '{1}' do not match.", 'first', 'second')(CONFIG_STATE_DATA)).toBe( + 'first and second do not match.', + ); + + try { + ConfigState.getLocalization('::Test')({ + ...CONFIG_STATE_DATA, + environment: { ...CONFIG_STATE_DATA.environment, localization: {} as any }, + }); + expect(false).toBeTruthy(); // fail + } catch (error) { + expect((error as Error).message).toContain('Please check your environment'); + } + }); + }); +}); diff --git a/npm/ng-packs/packages/core/src/lib/tests/state-overwrite.ts b/npm/ng-packs/packages/core/src/lib/tests/state-overwrite.ts deleted file mode 100644 index 31eaa0560f..0000000000 --- a/npm/ng-packs/packages/core/src/lib/tests/state-overwrite.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { Inject, Injectable, InjectionToken, Provider } from '@angular/core'; -import { getActionTypeFromInstance, InitState, NgxsPlugin, NGXS_PLUGINS, setValue } from '@ngxs/store'; - -export const NGXS_OVERWRITE_PLUGIN_VALUE = new InjectionToken('NGXS_OVERWRITE_PLUGIN_VALUE'); - -export class StateOverwrite { - static readonly type = '[StateOverwrite] Patch'; - constructor(public payload: { stateName: string; value: any }) {} -} - -@Injectable() -export class OverwritePlugin implements NgxsPlugin { - initialized: boolean; - - constructor(@Inject(NGXS_OVERWRITE_PLUGIN_VALUE) private options: any) {} - - handle(state, action, next) { - const type = getActionTypeFromInstance(action); - - if (action instanceof InitState && !this.initialized) { - state = { ...state, ...this.options }; - this.initialized = true; - } - - if (type === StateOverwrite.type) { - state = setValue(state, action.payload.stateName, action.payload.value); - } - - return next(state, action); - } -} - -export function stateOverwriteProviders(value = {}): Provider[] { - return [ - { - provide: NGXS_PLUGINS, - useClass: OverwritePlugin, - multi: true, - }, - { - provide: NGXS_OVERWRITE_PLUGIN_VALUE, - useValue: value, - }, - ]; -}