Skip to content

Commit 48bf436

Browse files
committed
test(ilc/client): add missing cleanup to registerApps()
1 parent 4127cba commit 48bf436

File tree

5 files changed

+53
-41
lines changed

5 files changed

+53
-41
lines changed

ilc/client/Client.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class Client {
8585

8686
#canonicalTagHandler;
8787

88-
#asyncBootUp;
88+
#registeredApps;
8989

9090
#navigationHooks = [
9191
(url) => (this.#transitionHooksExecutor.shouldNavigate(url) ? url : null),
@@ -296,7 +296,7 @@ export class Client {
296296

297297
// TODO: window.ILC.importLibrary - calls bootstrap function with props (if supported), and returns exposed API
298298
// TODO: window.ILC.importParcelFromLibrary - same as importParcelFromApp, but for libs
299-
this.#asyncBootUp = registerApplications(
299+
this.#registeredApps = registerApplications(
300300
this.#configRoot,
301301
this.#router,
302302
this.#errorHandlerFor.bind(this),
@@ -479,9 +479,9 @@ export class Client {
479479
this.#i18n.destroy();
480480
}
481481

482-
// Destroy AsyncBootUp
483-
if (this.#asyncBootUp) {
484-
this.#asyncBootUp.destroy();
482+
// Unregister all single-spa applications
483+
if (this.#registeredApps) {
484+
this.#registeredApps.destroy();
485485
}
486486
}
487487
}

ilc/client/registerSpaApps.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,13 @@ export function registerApplications(
148148

149149
singleSpa.registerApplication(appId, loadingFn, activityFn, customProps);
150150
});
151+
152+
return {
153+
destroy() {
154+
asyncBootUp.destroy();
155+
// TODO: change to unregisterApplication when single-spa is updated to 5.8+
156+
const registeredApps = singleSpa.getAppNames();
157+
return Promise.all(registeredApps.map((appName) => singleSpa.unloadApplication(appName)));
158+
},
159+
};
151160
}

ilc/client/registerSpaApps.spec.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('registerSpaApps', () => {
1616
let composeAppSlotPairsStub: SinonStub;
1717
let mockSlot: any;
1818
let asyncBootUpWaitForSlotStub: SinonStub;
19+
let registeredAppsResult: { destroy: () => Promise<void[]> } | null = null;
1920
let testCounter = 0;
2021

2122
beforeEach(() => {
@@ -103,8 +104,10 @@ describe('registerSpaApps', () => {
103104

104105
afterEach(async () => {
105106
// Unload all registered apps from single-spa to avoid conflicts with other tests
106-
const registeredApps = singleSpa.getAppNames();
107-
await Promise.all(registeredApps.map((appName) => singleSpa.unloadApplication(appName)));
107+
if (registeredAppsResult) {
108+
await registeredAppsResult.destroy();
109+
registeredAppsResult = null;
110+
}
108111

109112
// Restore stubs
110113
composeAppSlotPairsStub.restore();
@@ -122,7 +125,7 @@ describe('registerSpaApps', () => {
122125

123126
describe('registerApplications', () => {
124127
it('should register applications with single-spa', () => {
125-
registerApplications(
128+
registeredAppsResult = registerApplications(
126129
mockIlcConfigRoot,
127130
mockRouter,
128131
mockAppErrorHandlerFactory,
@@ -139,7 +142,7 @@ describe('registerSpaApps', () => {
139142
});
140143

141144
it('should create custom props for each application', () => {
142-
registerApplications(
145+
registeredAppsResult = registerApplications(
143146
mockIlcConfigRoot,
144147
mockRouter,
145148
mockAppErrorHandlerFactory,
@@ -177,7 +180,7 @@ describe('registerSpaApps', () => {
177180
slotElement2.id = `ilc-slot-${secondSlotName}`;
178181
document.body.appendChild(slotElement2);
179182

180-
registerApplications(
183+
registeredAppsResult = registerApplications(
181184
mockIlcConfigRoot,
182185
mockRouter,
183186
mockAppErrorHandlerFactory,
@@ -193,7 +196,7 @@ describe('registerSpaApps', () => {
193196
});
194197

195198
it('should call composeAppSlotPairsToRegister with ilcConfigRoot', () => {
196-
registerApplications(
199+
registeredAppsResult = registerApplications(
197200
mockIlcConfigRoot,
198201
mockRouter,
199202
mockAppErrorHandlerFactory,
@@ -211,7 +214,7 @@ describe('registerSpaApps', () => {
211214
// Make getSdkFactoryByApplicationName throw an error
212215
mockSdkFactoryBuilder.getSdkFactoryByApplicationName.throws(new Error('SDK factory error'));
213216

214-
registerApplications(
217+
registeredAppsResult = registerApplications(
215218
mockIlcConfigRoot,
216219
mockRouter,
217220
mockAppErrorHandlerFactory,
@@ -233,7 +236,7 @@ describe('registerSpaApps', () => {
233236
it('should register app with correct activity function', async () => {
234237
mockRouter.isAppWithinSlotActive.returns(false);
235238

236-
registerApplications(
239+
registeredAppsResult = registerApplications(
237240
mockIlcConfigRoot,
238241
mockRouter,
239242
mockAppErrorHandlerFactory,
@@ -254,7 +257,7 @@ describe('registerSpaApps', () => {
254257
});
255258

256259
it('should preload app bundle', () => {
257-
registerApplications(
260+
registeredAppsResult = registerApplications(
258261
mockIlcConfigRoot,
259262
mockRouter,
260263
mockAppErrorHandlerFactory,
@@ -278,7 +281,7 @@ describe('registerSpaApps', () => {
278281
wrappedWith: 'wrapperApp',
279282
});
280283

281-
registerApplications(
284+
registeredAppsResult = registerApplications(
282285
mockIlcConfigRoot,
283286
mockRouter,
284287
mockAppErrorHandlerFactory,
@@ -294,7 +297,7 @@ describe('registerSpaApps', () => {
294297
});
295298

296299
it('should register with custom props containing appId', () => {
297-
registerApplications(
300+
registeredAppsResult = registerApplications(
298301
mockIlcConfigRoot,
299302
mockRouter,
300303
mockAppErrorHandlerFactory,
@@ -315,7 +318,7 @@ describe('registerSpaApps', () => {
315318
});
316319

317320
it('should register with custom props containing domElementGetter', () => {
318-
registerApplications(
321+
registeredAppsResult = registerApplications(
319322
mockIlcConfigRoot,
320323
mockRouter,
321324
mockAppErrorHandlerFactory,
@@ -342,7 +345,7 @@ describe('registerSpaApps', () => {
342345
},
343346
});
344347

345-
registerApplications(
348+
registeredAppsResult = registerApplications(
346349
mockIlcConfigRoot,
347350
mockRouter,
348351
mockAppErrorHandlerFactory,
@@ -361,7 +364,7 @@ describe('registerSpaApps', () => {
361364
const mockPathProps = { testProp: 'testValue' };
362365
mockRouter.getCurrentRouteProps.returns(mockPathProps);
363366

364-
registerApplications(
367+
registeredAppsResult = registerApplications(
365368
mockIlcConfigRoot,
366369
mockRouter,
367370
mockAppErrorHandlerFactory,
@@ -379,7 +382,7 @@ describe('registerSpaApps', () => {
379382
it('should register apps with getCurrentBasePath function', () => {
380383
mockRouter.getCurrentRoute.returns({ basePath: '/test-base' });
381384

382-
registerApplications(
385+
registeredAppsResult = registerApplications(
383386
mockIlcConfigRoot,
384387
mockRouter,
385388
mockAppErrorHandlerFactory,

ilc/server/tailor/request-fragment.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = (filterHeaders, processFragmentResponse, logger) =>
4343
if (attributes.wrapperConf) {
4444
const wrapperConf = attributes.wrapperConf;
4545
const reqUrl = makeFragmentUrl({
46-
domain: request.hostname,
46+
domain: request.host,
4747
route: currRoute,
4848
baseUrl: wrapperConf.src,
4949
appId: wrapperConf.appId,
@@ -56,7 +56,7 @@ module.exports = (filterHeaders, processFragmentResponse, logger) =>
5656
{
5757
url: currRoute.route,
5858
id: request.id,
59-
domain: request.hostname,
59+
domain: request.host,
6060
detailsJSON: JSON.stringify({
6161
attributes,
6262
}),
@@ -77,7 +77,7 @@ module.exports = (filterHeaders, processFragmentResponse, logger) =>
7777
{
7878
url: currRoute.route,
7979
id: request.id,
80-
domain: request.hostname,
80+
domain: request.host,
8181
detailsJSON: JSON.stringify({
8282
statusCode: response.statusCode,
8383
'x-props-override': response.headers['x-props-override'],
@@ -105,7 +105,7 @@ module.exports = (filterHeaders, processFragmentResponse, logger) =>
105105
{
106106
url: currRoute.route,
107107
id: request.id,
108-
domain: request.hostname,
108+
domain: request.host,
109109
detailsJSON: JSON.stringify({
110110
attributes,
111111
}),
@@ -136,7 +136,7 @@ module.exports = (filterHeaders, processFragmentResponse, logger) =>
136136
{
137137
url: currRoute.route,
138138
id: request.id,
139-
domain: request.hostname,
139+
domain: request.host,
140140
},
141141
'Request Fragment. Wrapper Fragment Processing. Fragment Response Processing Error',
142142
);
@@ -148,7 +148,7 @@ module.exports = (filterHeaders, processFragmentResponse, logger) =>
148148
{
149149
url: currRoute.route,
150150
id: request.id,
151-
domain: request.hostname,
151+
domain: request.host,
152152
},
153153
'Request Fragment. Wrapper Fragment Processing. Fragment Request Error',
154154
);
@@ -170,7 +170,7 @@ module.exports = (filterHeaders, processFragmentResponse, logger) =>
170170
});
171171

172172
const reqUrl = makeFragmentUrl({
173-
domain: request.hostname,
173+
domain: request.host,
174174
route: currRoute,
175175
baseUrl: fragmentUrl,
176176
appId: attributes.id,
@@ -182,7 +182,7 @@ module.exports = (filterHeaders, processFragmentResponse, logger) =>
182182
{
183183
url: currRoute.route,
184184
id: request.id,
185-
domain: request.hostname,
185+
domain: request.host,
186186
detailsJSON: JSON.stringify({
187187
route: currRoute,
188188
baseUrl: fragmentUrl,
@@ -211,7 +211,7 @@ module.exports = (filterHeaders, processFragmentResponse, logger) =>
211211
}),
212212
);
213213
logger.debug(
214-
{ url: currRoute.route, id: request.id, domain: request.hostname },
214+
{ url: currRoute.route, id: request.id, domain: request.host },
215215
'Fragment Processing. Finished',
216216
);
217217
} catch (e) {

ilc/server/tailor/request-fragment.spec.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('request-fragment', () => {
5757
const request = {
5858
registryConfig,
5959
ilcState: {},
60-
hostname: 'apps.test',
60+
host: 'apps.test',
6161
};
6262
request.router = new ServerRouter(logger, request, '/primary');
6363

@@ -67,7 +67,7 @@ describe('request-fragment', () => {
6767
const expectedAppProps = { publicPath: 'http://apps.test/primary' };
6868
const expectedSdkOptions = { i18n: { manifestPath: '/l10n/primary/manifest.json' } };
6969

70-
const expectedDomain = request.hostname;
70+
const expectedDomain = request.host;
7171
const expectedRouterPropsEncoded = Buffer.from(JSON.stringify(expectedRouterProps)).toString('base64');
7272
const expectedAppPropsEncoded = Buffer.from(JSON.stringify(expectedAppProps)).toString('base64');
7373
const expectedSdkEncoded = Buffer.from(JSON.stringify(expectedSdkOptions)).toString('base64');
@@ -118,7 +118,7 @@ describe('request-fragment', () => {
118118
const request = {
119119
registryConfig,
120120
ilcState: {},
121-
hostname: 'apps.test',
121+
host: 'apps.test',
122122
};
123123
request.router = new ServerRouter(logger, request, '/wrapper');
124124

@@ -128,7 +128,7 @@ describe('request-fragment', () => {
128128
const expectedAppProps = { param1: 'value1' };
129129
const wrappedAppProps = { page: 'wrapped' };
130130

131-
const expectedDomain = request.hostname;
131+
const expectedDomain = request.host;
132132
const expectedRouterPropsEncoded = Buffer.from(JSON.stringify(expectedRouterProps)).toString('base64');
133133
const expectedAppPropsEncoded = Buffer.from(JSON.stringify(expectedAppProps)).toString('base64');
134134
const expectedWrappedAppPropsEncoded = Buffer.from(JSON.stringify(wrappedAppProps)).toString('base64');
@@ -179,7 +179,7 @@ describe('request-fragment', () => {
179179
const request = {
180180
registryConfig,
181181
ilcState: {},
182-
hostname: 'apps.test',
182+
host: 'apps.test',
183183
};
184184
request.router = new ServerRouter(logger, request, '/wrapper');
185185

@@ -190,7 +190,7 @@ describe('request-fragment', () => {
190190
const wrapperPropsOverride = { param2: 'value2' };
191191
const wrappedAppProps = { page: 'wrapped' };
192192

193-
const expectedDomain = request.hostname;
193+
const expectedDomain = request.host;
194194
const expectedWrapperRouterPropsEncoded = Buffer.from(JSON.stringify(expectedWrapperRouterProps)).toString(
195195
'base64',
196196
);
@@ -265,7 +265,7 @@ describe('request-fragment', () => {
265265
const request = {
266266
registryConfig,
267267
ilcState: {},
268-
hostname: 'apps.test',
268+
host: 'apps.test',
269269
};
270270
request.router = new ServerRouter(logger, request, '/primary');
271271

@@ -275,7 +275,7 @@ describe('request-fragment', () => {
275275
const expectedAppProps = { publicPath: 'http://apps.test/primary' };
276276
const expectedSdkOptions = { i18n: { manifestPath: '/l10n/primary/manifest.json' } };
277277

278-
const expectedDomain = request.hostname;
278+
const expectedDomain = request.host;
279279
const expectedRouterPropsEncoded = Buffer.from(JSON.stringify(expectedRouterProps)).toString('base64');
280280
const expectedAppPropsEncoded = Buffer.from(JSON.stringify(expectedAppProps)).toString('base64');
281281
const expectedSdkEncoded = Buffer.from(JSON.stringify(expectedSdkOptions)).toString('base64');
@@ -321,7 +321,7 @@ describe('request-fragment', () => {
321321
const request = {
322322
registryConfig,
323323
ilcState: {},
324-
hostname: 'apps.test',
324+
host: 'apps.test',
325325
};
326326
request.router = new ServerRouter(logger, request, '/primary');
327327

@@ -366,7 +366,7 @@ describe('request-fragment', () => {
366366
const request = {
367367
registryConfig,
368368
ilcState: {},
369-
hostname: 'apps.test',
369+
host: 'apps.test',
370370
};
371371
request.router = new ServerRouter(logger, request, '/wrapper');
372372

@@ -405,7 +405,7 @@ describe('request-fragment', () => {
405405
const request = {
406406
registryConfig,
407407
ilcState: {},
408-
hostname: 'secure.test',
408+
host: 'secure.test',
409409
};
410410
request.router = new ServerRouter(logger, request, '/primary');
411411

@@ -439,7 +439,7 @@ describe('request-fragment', () => {
439439
const request = {
440440
registryConfig,
441441
ilcState: {},
442-
hostname: 'secure.test',
442+
host: 'secure.test',
443443
};
444444
request.router = new ServerRouter(logger, request, '/primary');
445445

0 commit comments

Comments
 (0)