|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { LifecycleHooksConfigService } from './lifecycle-hooks-config.service'; |
| 3 | +import { I18nService } from '../i18n.service'; |
| 4 | +import { LuigiNodesService } from '../luigi-nodes/luigi-nodes.service'; |
| 5 | +import { LuigiCoreService } from '../luigi-core.service'; |
| 6 | +import { RoutingConfigService } from './routing-config.service'; |
| 7 | +import { StaticSettingsConfigService } from './static-settings-config.service'; |
| 8 | +import { UserSettingsConfigService } from './user-settings-config.service'; |
| 9 | +import { GlobalSearchConfigService } from './global-search-config.service'; |
| 10 | +import { |
| 11 | + LUIGI_GLOBAL_SEARCH_CONFIG_SERVICE_INJECTION_TOKEN, |
| 12 | + LUIGI_STATIC_SETTINGS_CONFIG_SERVICE_INJECTION_TOKEN, |
| 13 | + LUIGI_USER_SETTINGS_CONFIG_SERVICE_INJECTION_TOKEN, |
| 14 | +} from '../../injection-tokens'; |
| 15 | + |
| 16 | +describe('LifecycleHooksConfigService', () => { |
| 17 | + let service: LifecycleHooksConfigService; |
| 18 | + let i18nServiceMock: jest.Mocked<I18nService>; |
| 19 | + let luigiNodesServiceMock: jest.Mocked<LuigiNodesService>; |
| 20 | + let luigiCoreServiceMock: jest.Mocked<LuigiCoreService>; |
| 21 | + let routingConfigServiceMock: jest.Mocked<RoutingConfigService>; |
| 22 | + let staticSettingsConfigServiceMock: jest.Mocked<StaticSettingsConfigService>; |
| 23 | + let userSettingsConfigServiceMock: jest.Mocked<UserSettingsConfigService>; |
| 24 | + let globalSearchConfigServiceMock: jest.Mocked<GlobalSearchConfigService>; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + i18nServiceMock = { afterInit: jest.fn() } as any; |
| 28 | + luigiNodesServiceMock = { retrieveChildrenByEntity: jest.fn() } as any; |
| 29 | + luigiCoreServiceMock = { |
| 30 | + getConfig: jest.fn(), |
| 31 | + setConfig: jest.fn(), |
| 32 | + ux: jest.fn().mockReturnValue({ hideAppLoadingIndicator: jest.fn() }), |
| 33 | + isFeatureToggleActive: jest.fn(), |
| 34 | + resetLuigi: jest.fn(), |
| 35 | + showAlert: jest.fn().mockReturnValue(Promise.resolve()), |
| 36 | + } as any; |
| 37 | + routingConfigServiceMock = { getRoutingConfig: jest.fn() } as any; |
| 38 | + staticSettingsConfigServiceMock = { |
| 39 | + getStaticSettingsConfig: jest.fn(), |
| 40 | + getInitialStaticSettingsConfig: jest.fn(), |
| 41 | + } as any; |
| 42 | + userSettingsConfigServiceMock = { getUserSettings: jest.fn() } as any; |
| 43 | + globalSearchConfigServiceMock = { getGlobalSearchConfig: jest.fn() } as any; |
| 44 | + |
| 45 | + TestBed.configureTestingModule({ |
| 46 | + providers: [ |
| 47 | + LifecycleHooksConfigService, |
| 48 | + { provide: I18nService, useValue: i18nServiceMock }, |
| 49 | + { provide: LuigiNodesService, useValue: luigiNodesServiceMock }, |
| 50 | + { provide: LuigiCoreService, useValue: luigiCoreServiceMock }, |
| 51 | + { provide: RoutingConfigService, useValue: routingConfigServiceMock }, |
| 52 | + { |
| 53 | + provide: LUIGI_STATIC_SETTINGS_CONFIG_SERVICE_INJECTION_TOKEN, |
| 54 | + useValue: staticSettingsConfigServiceMock, |
| 55 | + }, |
| 56 | + { |
| 57 | + provide: LUIGI_USER_SETTINGS_CONFIG_SERVICE_INJECTION_TOKEN, |
| 58 | + useValue: userSettingsConfigServiceMock, |
| 59 | + }, |
| 60 | + { |
| 61 | + provide: LUIGI_GLOBAL_SEARCH_CONFIG_SERVICE_INJECTION_TOKEN, |
| 62 | + useValue: globalSearchConfigServiceMock, |
| 63 | + }, |
| 64 | + ], |
| 65 | + }); |
| 66 | + |
| 67 | + service = TestBed.inject(LifecycleHooksConfigService); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should be created', () => { |
| 71 | + expect(service).toBeTruthy(); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('getLifecycleHooksConfig', () => { |
| 75 | + it('should return an object with luigiAfterInit function', () => { |
| 76 | + const config = service.getLifecycleHooksConfig({} as any); |
| 77 | + expect(config).toHaveProperty('luigiAfterInit'); |
| 78 | + expect(typeof config.luigiAfterInit).toBe('function'); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('luigiAfterInit', () => { |
| 82 | + it('should call i18nServiceMock.afterInit', async () => { |
| 83 | + const config = service.getLifecycleHooksConfig({} as any); |
| 84 | + await config.luigiAfterInit(); |
| 85 | + expect(i18nServiceMock.afterInit).toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should call luigiNodesServiceMock.retrieveChildrenByEntity', async () => { |
| 89 | + const config = service.getLifecycleHooksConfig({} as any); |
| 90 | + await config.luigiAfterInit(); |
| 91 | + expect( |
| 92 | + luigiNodesServiceMock.retrieveChildrenByEntity |
| 93 | + ).toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should call luigiCoreServiceMock methods', async () => { |
| 97 | + const config = service.getLifecycleHooksConfig({} as any); |
| 98 | + await config.luigiAfterInit(); |
| 99 | + expect(luigiCoreServiceMock.getConfig).toHaveBeenCalled(); |
| 100 | + expect( |
| 101 | + luigiCoreServiceMock.ux().hideAppLoadingIndicator |
| 102 | + ).toHaveBeenCalled(); |
| 103 | + expect(luigiCoreServiceMock.setConfig).toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should call resetLuigi if btpLayout feature is active', async () => { |
| 107 | + luigiCoreServiceMock.isFeatureToggleActive.mockReturnValue(true); |
| 108 | + const config = service.getLifecycleHooksConfig({} as any); |
| 109 | + await config.luigiAfterInit(); |
| 110 | + expect(luigiCoreServiceMock.resetLuigi).toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should not call resetLuigi if btpLayout feature is not active', async () => { |
| 114 | + luigiCoreServiceMock.isFeatureToggleActive.mockReturnValue(false); |
| 115 | + const config = service.getLifecycleHooksConfig({} as any); |
| 116 | + await config.luigiAfterInit(); |
| 117 | + expect(luigiCoreServiceMock.resetLuigi).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should handle error when retrieving Luigi navigation nodes', async () => { |
| 121 | + luigiNodesServiceMock.retrieveChildrenByEntity.mockRejectedValue( |
| 122 | + new Error('Test error') |
| 123 | + ); |
| 124 | + staticSettingsConfigServiceMock.getInitialStaticSettingsConfig.mockReturnValue( |
| 125 | + { header: { title: 'Test App' } } |
| 126 | + ); |
| 127 | + console.error = jest.fn(); |
| 128 | + |
| 129 | + const config = service.getLifecycleHooksConfig({} as any); |
| 130 | + await config.luigiAfterInit(); |
| 131 | + |
| 132 | + expect(console.error).toHaveBeenCalledWith( |
| 133 | + 'Error retrieving Luigi navigation nodes Error: Test error' |
| 134 | + ); |
| 135 | + expect(luigiCoreServiceMock.showAlert).toHaveBeenCalledWith({ |
| 136 | + text: 'There was an error loading the Test App', |
| 137 | + type: 'error', |
| 138 | + }); |
| 139 | + }); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments