From ba559827386fb01dd7c555d02fb1f96c3d005ee6 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Tue, 24 Nov 2020 14:30:23 +0300 Subject: [PATCH] test: move config state tests to config-state.service.spec --- .../lib/tests/config-state.service.spec.ts | 209 ++++++++++++++---- 1 file changed, 166 insertions(+), 43 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/tests/config-state.service.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/config-state.service.spec.ts index d5baa8b309..2a3165a873 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/config-state.service.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/config-state.service.spec.ts @@ -1,59 +1,182 @@ +import { HttpClient } from '@angular/common/http'; import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest'; import { Store } from '@ngxs/store'; -import * as ConfigActions from '../actions'; +import { of, ReplaySubject, timer } from 'rxjs'; import { ApplicationConfiguration } from '../models/application-configuration'; -import { Config } from '../models/config'; -import { ConfigStateService } from '../services/config-state.service'; -import { ConfigState } from '../states'; +import { ApplicationConfigurationService, ConfigStateService } from '../services'; -describe('ConfigStateService', () => { - let service: ConfigStateService; +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], + }, + 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, + }, + ], + currentCulture: { + displayName: 'English', + englishName: 'English', + threeLetterIsoLanguageName: 'eng', + twoLetterIsoLanguageName: 'en', + isRightToLeft: false, + cultureName: 'en', + name: 'en', + nativeName: 'English', + dateTimeFormat: { + calendarAlgorithmType: 'SolarCalendar', + dateTimeFormatLong: 'dddd, MMMM d, yyyy', + shortDatePattern: 'M/d/yyyy', + fullDateTimePattern: 'dddd, MMMM d, yyyy h:mm:ss tt', + dateSeparator: '/', + shortTimePattern: 'h:mm tt', + longTimePattern: 'h:mm:ss tt', + }, + }, + defaultResourceName: null, + }, + auth: { + policies: { + 'AbpIdentity.Roles': true, + }, + grantedPolicies: { + 'Abp.Identity': false, + 'Abp.Account': true, + }, + }, + setting: { + values: { + 'Abp.Custom.SomeSetting': 'X', + 'Abp.Localization.DefaultLanguage': 'en', + }, + }, + currentUser: { + isAuthenticated: false, + id: null, + tenantId: null, + userName: null, + email: null, + roles: [], + } as ApplicationConfiguration.CurrentUser, + features: { + values: { + 'Chat.Enable': 'True', + }, + }, + registerLocaleFn: () => Promise.resolve(), +} as any) as ApplicationConfiguration.Response; + +describe('ConfigState', () => { let spectator: SpectatorService; - let store: SpyObject; + let configState: ConfigStateService; + + const createService = createServiceFactory({ + service: ConfigStateService, + }); - const createService = createServiceFactory({ service: ConfigStateService, mocks: [Store] }); beforeEach(() => { spectator = createService(); - service = spectator.service; - store = spectator.inject(Store); - }); - test('should have the all ConfigState static methods', () => { - const reg = /(?<=static )(.*)(?=\()/gm; - ConfigState.toString() - .match(reg) - .forEach(fnName => { - expect(service[fnName]).toBeTruthy(); - - const spy = jest.spyOn(store, 'selectSnapshot'); - spy.mockClear(); - - const isDynamicSelector = ConfigState[fnName].name !== 'memoized'; - - if (isDynamicSelector) { - ConfigState[fnName] = jest.fn((...args) => args); - service[fnName]('test', 0, {}); - expect(ConfigState[fnName]).toHaveBeenCalledWith('test', 0, {}); - } else { - service[fnName](); - expect(spy).toHaveBeenCalledWith(ConfigState[fnName]); - } - }); + configState = spectator.service; + + configState.setState(CONFIG_STATE_DATA); + }); + + describe('#getAll', () => { + it('should return CONFIG_STATE_DATA', () => { + expect(configState.getAll()).toEqual(CONFIG_STATE_DATA); + configState.getAll$().subscribe(data => expect(data).toEqual(CONFIG_STATE_DATA)); + }); }); - test('should have a dispatch method for every ConfigState action', () => { - const reg = /(?<=dispatch)(\w+)(?=\()/gm; - ConfigStateService.toString() - .match(reg) - .forEach(fnName => { - expect(ConfigActions[fnName]).toBeTruthy(); + describe('#getOne', () => { + it('should return one property', () => { + expect(configState.getOne('localization')).toEqual(CONFIG_STATE_DATA.localization); + configState + .getOne$('localization') + .subscribe(localization => expect(localization).toEqual(CONFIG_STATE_DATA.localization)); + }); + }); + + describe('#getDeep', () => { + it('should return deeper', () => { + expect(configState.getDeep('localization.languages')).toEqual( + CONFIG_STATE_DATA.localization.languages, + ); - const spy = jest.spyOn(store, 'dispatch'); - spy.mockClear(); + configState + .getDeep$('localization.languages') + .subscribe(languages => + expect(languages).toEqual(CONFIG_STATE_DATA.localization.languages), + ); - const params = Array.from(new Array(ConfigActions[fnName].length)); + expect(configState.getDeep('test')).toBeFalsy(); + }); + }); - service[`dispatch${fnName}`](...params); - expect(spy).toHaveBeenCalledWith(new ConfigActions[fnName](...params)); + describe('#getFeature', () => { + it('should return a setting', () => { + expect(configState.getFeature('Chat.Enable')).toEqual( + CONFIG_STATE_DATA.features.values['Chat.Enable'], + ); + configState + .getFeature$('Chat.Enable') + .subscribe(data => expect(data).toEqual(CONFIG_STATE_DATA.features.values['Chat.Enable'])); + }); + }); + + describe('#getSetting', () => { + it('should return a setting', () => { + expect(configState.getSetting('Abp.Localization.DefaultLanguage')).toEqual( + CONFIG_STATE_DATA.setting.values['Abp.Localization.DefaultLanguage'], + ); + configState.getSetting$('Abp.Localization.DefaultLanguage').subscribe(data => { + expect(data).toEqual(CONFIG_STATE_DATA.setting.values['Abp.Localization.DefaultLanguage']); }); + }); + }); + + describe('#getSettings', () => { + test.each` + keyword | expected + ${undefined} | ${CONFIG_STATE_DATA.setting.values} + ${'Localization'} | ${{ 'Abp.Localization.DefaultLanguage': 'en' }} + ${'X'} | ${{}} + ${'localization'} | ${{}} + `('should return $expected when keyword is given as $keyword', ({ keyword, expected }) => { + expect(configState.getSettings(keyword)).toEqual(expected); + configState.getSettings$(keyword).subscribe(data => expect(data).toEqual(expected)); + }); }); });