Skip to content

Commit c0b3250

Browse files
committed
feat: luigi lifecycle hooks config service implementation
1 parent f711033 commit c0b3250

File tree

4 files changed

+201
-1
lines changed

4 files changed

+201
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { NoopGlobalSearchConfigService } from './global-search-config.service';
2+
3+
describe('NoopGlobalSearchConfigService', () => {
4+
let service: NoopGlobalSearchConfigService;
5+
6+
beforeEach(() => {
7+
service = new NoopGlobalSearchConfigService();
8+
});
9+
10+
describe('getGlobalSearchConfig', () => {
11+
it('should return undefined', () => {
12+
const result = service.getGlobalSearchConfig();
13+
expect(result).toBeUndefined();
14+
});
15+
16+
it('should return undefined', () => {
17+
expect(service.getGlobalSearchConfig()).toBeUndefined();
18+
});
19+
});
20+
});
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
});

src/lib/services/luigi-config/lifecycle-hooks-config.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export class LifecycleHooksConfigService {
3636
this.i18nService.afterInit();
3737

3838
let childrenByEntity: Record<string, LuigiNode[]>;
39-
4039
try {
4140
childrenByEntity =
4241
await this.luigiNodesService.retrieveChildrenByEntity();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { NoopUserSettingsConfigService } from './user-settings-config.service';
2+
import { LuigiNode } from '../../models/luigi';
3+
4+
describe('NoopUserSettingsConfigService', () => {
5+
let service: NoopUserSettingsConfigService;
6+
7+
beforeEach(() => {
8+
service = new NoopUserSettingsConfigService();
9+
});
10+
11+
describe('getUserSettings', () => {
12+
it('should return undefined', async () => {
13+
const mockLuigiNodes: Record<string, LuigiNode[]> = {
14+
category1: [{ label: 'Node 1' }, { label: 'Node 2' }],
15+
category2: [{ label: 'Node 3' }],
16+
};
17+
18+
const result = await service.getUserSettings(mockLuigiNodes);
19+
20+
expect(result).toBeUndefined();
21+
});
22+
23+
it('should return undefined even with empty input', async () => {
24+
const emptyLuigiNodes: Record<string, LuigiNode[]> = {};
25+
26+
const result = await service.getUserSettings(emptyLuigiNodes);
27+
28+
expect(result).toBeUndefined();
29+
});
30+
31+
it('should return a Promise that resolves to undefined', () => {
32+
const mockLuigiNodes: Record<string, LuigiNode[]> = {
33+
category: [{ label: 'Node' }],
34+
};
35+
36+
expect(service.getUserSettings(mockLuigiNodes)).resolves.toBeUndefined();
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)