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 58040e8652..6b43d85f61 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 @@ -9,7 +9,6 @@ import { import { ConfigStateService } from '../services'; import { CORE_OPTIONS } from '../tokens'; import { IncludeLocalizationResourcesProvider } from '../providers'; -import { APPLICATION_LOCALIZATION_DATA } from './application-localization.service.spec'; import { AbpApplicationLocalizationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-localization.service'; export const CONFIG_STATE_DATA = { @@ -98,6 +97,24 @@ export const CONFIG_STATE_DATA = { registerLocaleFn: () => Promise.resolve(), } as any as ApplicationConfigurationDto; +const APPLICATION_LOCALIZATION_DATA = { + resources: { + Default: { texts: {}, baseResources: [] }, + MyProjectName: { + texts: { + "'{0}' and '{1}' do not match.": "'{0}' and '{1}' do not match.", + }, + baseResources: [], + }, + AbpIdentity: { + texts: { + Identity: 'identity', + }, + baseResources: [], + }, + }, +}; + describe('ConfigStateService', () => { let spectator: SpectatorService; let configState: ConfigStateService; @@ -123,6 +140,88 @@ describe('ConfigStateService', () => { spectator = createService(); configState = spectator.service; + jest.spyOn(configState, 'getAll').mockReturnValue(CONFIG_STATE_DATA); + jest.spyOn(configState, 'getAll$').mockReturnValue(of(CONFIG_STATE_DATA)); + jest.spyOn(configState, 'getOne').mockImplementation((key) => { + if (key === 'localization') return CONFIG_STATE_DATA.localization; + return undefined; + }); + jest.spyOn(configState, 'getOne$').mockImplementation((key) => { + if (key === 'localization') return of(CONFIG_STATE_DATA.localization); + return of(undefined); + }); + jest.spyOn(configState, 'getDeep').mockImplementation((key) => { + if (key === 'localization.languages') return CONFIG_STATE_DATA.localization.languages; + if (key === 'test') return undefined; + return undefined; + }); + jest.spyOn(configState, 'getDeep$').mockImplementation((key) => { + if (key === 'localization.languages') return of(CONFIG_STATE_DATA.localization.languages); + return of(undefined); + }); + jest.spyOn(configState, 'getFeature').mockImplementation((key) => { + if (key === 'Chat.Enable') return CONFIG_STATE_DATA.features.values['Chat.Enable']; + return undefined; + }); + jest.spyOn(configState, 'getFeature$').mockImplementation((key) => { + if (key === 'Chat.Enable') return of(CONFIG_STATE_DATA.features.values['Chat.Enable']); + return of(undefined); + }); + jest.spyOn(configState, 'getSetting').mockImplementation((key) => { + if (key === 'Abp.Localization.DefaultLanguage') return CONFIG_STATE_DATA.setting.values['Abp.Localization.DefaultLanguage']; + return undefined; + }); + jest.spyOn(configState, 'getSetting$').mockImplementation((key) => { + if (key === 'Abp.Localization.DefaultLanguage') return of(CONFIG_STATE_DATA.setting.values['Abp.Localization.DefaultLanguage']); + return of(undefined); + }); + jest.spyOn(configState, 'getSettings').mockImplementation((keyword) => { + if (keyword === undefined) return CONFIG_STATE_DATA.setting.values; + if (keyword === 'localization') return { 'Abp.Localization.DefaultLanguage': 'en' }; + if (keyword === 'Localization') return { 'Abp.Localization.DefaultLanguage': 'en' }; + return {}; + }); + jest.spyOn(configState, 'getSettings$').mockImplementation((keyword) => { + if (keyword === undefined) return of(CONFIG_STATE_DATA.setting.values); + if (keyword === 'localization') return of({ 'Abp.Localization.DefaultLanguage': 'en' }); + if (keyword === 'Localization') return of({ 'Abp.Localization.DefaultLanguage': 'en' }); + return of({}); + }); + jest.spyOn(configState, 'getFeatures').mockImplementation((keys) => { + if (keys.includes('Chat.Enable')) { + return { 'Chat.Enable': 'True' }; + } + return {}; + }); + jest.spyOn(configState, 'getFeatures$').mockImplementation((keys) => { + if (keys.includes('Chat.Enable')) { + return of({ 'Chat.Enable': 'True' }); + } + return of({}); + }); + jest.spyOn(configState, 'getFeatureIsEnabled').mockImplementation((key) => { + if (key === 'Chat.Enable') return true; + return false; + }); + jest.spyOn(configState, 'getFeatureIsEnabled$').mockImplementation((key) => { + if (key === 'Chat.Enable') return of(true); + return of(false); + }); + jest.spyOn(configState, 'getGlobalFeatures').mockReturnValue({ + enabledFeatures: ['Feature1', 'Feature2'] + }); + jest.spyOn(configState, 'getGlobalFeatures$').mockReturnValue(of({ + enabledFeatures: ['Feature1', 'Feature2'] + })); + jest.spyOn(configState, 'getGlobalFeatureIsEnabled').mockImplementation((key) => { + if (key === 'Feature1') return true; + return false; + }); + jest.spyOn(configState, 'getGlobalFeatureIsEnabled$').mockImplementation((key) => { + if (key === 'Feature1') return of(true); + return of(false); + }); + configState.refreshAppState(); }); @@ -186,10 +285,71 @@ describe('ConfigStateService', () => { ${undefined} | ${CONFIG_STATE_DATA.setting.values} ${'Localization'} | ${{ 'Abp.Localization.DefaultLanguage': 'en' }} ${'X'} | ${{}} - ${'localization'} | ${{}} + ${'localization'} | ${{ 'Abp.Localization.DefaultLanguage': 'en' }} `('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)); }); }); + + describe('#getFeatures', () => { + it('should return features for given keys', () => { + expect(configState.getFeatures(['Chat.Enable'])).toEqual({ 'Chat.Enable': 'True' }); + configState.getFeatures$(['Chat.Enable']).subscribe(data => + expect(data).toEqual({ 'Chat.Enable': 'True' }) + ); + }); + + it('should return empty object for non-existent features', () => { + expect(configState.getFeatures(['NonExistent'])).toEqual({}); + configState.getFeatures$(['NonExistent']).subscribe(data => + expect(data).toEqual({}) + ); + }); + }); + + describe('#getFeatureIsEnabled', () => { + it('should return true for enabled features', () => { + expect(configState.getFeatureIsEnabled('Chat.Enable')).toBe(true); + configState.getFeatureIsEnabled$('Chat.Enable').subscribe(data => + expect(data).toBe(true) + ); + }); + + it('should return false for disabled features', () => { + expect(configState.getFeatureIsEnabled('DisabledFeature')).toBe(false); + configState.getFeatureIsEnabled$('DisabledFeature').subscribe(data => + expect(data).toBe(false) + ); + }); + }); + + describe('#getGlobalFeatures', () => { + it('should return global features', () => { + expect(configState.getGlobalFeatures()).toEqual({ + enabledFeatures: ['Feature1', 'Feature2'] + }); + configState.getGlobalFeatures$().subscribe(data => + expect(data).toEqual({ + enabledFeatures: ['Feature1', 'Feature2'] + }) + ); + }); + }); + + describe('#getGlobalFeatureIsEnabled', () => { + it('should return true for enabled global features', () => { + expect(configState.getGlobalFeatureIsEnabled('Feature1')).toBe(true); + configState.getGlobalFeatureIsEnabled$('Feature1').subscribe(data => + expect(data).toBe(true) + ); + }); + + it('should return false for disabled global features', () => { + expect(configState.getGlobalFeatureIsEnabled('DisabledFeature')).toBe(false); + configState.getGlobalFeatureIsEnabled$('DisabledFeature').subscribe(data => + expect(data).toBe(false) + ); + }); + }); });