|
11 | 11 | import '@angular/compiler'; |
12 | 12 | /* eslint-enable import/no-unassigned-import */ |
13 | 13 |
|
14 | | -import { Component, InjectionToken, Injector, inject } from '@angular/core'; |
| 14 | +import { |
| 15 | + Component, |
| 16 | + DestroyRef, |
| 17 | + InjectionToken, |
| 18 | + Injector, |
| 19 | + PlatformRef, |
| 20 | + inject, |
| 21 | + provideAppInitializer, |
| 22 | +} from '@angular/core'; |
15 | 23 | import { |
16 | 24 | Route, |
17 | 25 | Routes, |
@@ -1335,4 +1343,85 @@ describe('extractRoutesAndCreateRouteTree', () => { |
1335 | 1343 | }, |
1336 | 1344 | ]); |
1337 | 1345 | }); |
| 1346 | + |
| 1347 | + it('should log destroy errors without failing route extraction', async () => { |
| 1348 | + const destroyError = new Error('Destroy hook failed'); |
| 1349 | + const consoleErrorSpy = spyOn(console, 'error'); |
| 1350 | + |
| 1351 | + setAngularAppTestingManifest( |
| 1352 | + [{ path: 'home', component: DummyComponent }], |
| 1353 | + [{ path: '**', renderMode: RenderMode.Server }], |
| 1354 | + undefined, |
| 1355 | + undefined, |
| 1356 | + undefined, |
| 1357 | + undefined, |
| 1358 | + [ |
| 1359 | + provideAppInitializer(() => { |
| 1360 | + inject(DestroyRef).onDestroy(() => { |
| 1361 | + throw destroyError; |
| 1362 | + }); |
| 1363 | + }), |
| 1364 | + ], |
| 1365 | + ); |
| 1366 | + |
| 1367 | + const { routeTree, errors } = await extractRoutesAndCreateRouteTree({ url }); |
| 1368 | + expect(errors).toHaveSize(0); |
| 1369 | + expect(routeTree.toObject()).toEqual([{ route: '/home', renderMode: RenderMode.Server }]); |
| 1370 | + expect(consoleErrorSpy).toHaveBeenCalledOnceWith( |
| 1371 | + 'An error occurred while destroying the platform.', |
| 1372 | + destroyError, |
| 1373 | + ); |
| 1374 | + }); |
| 1375 | + |
| 1376 | + it('should not throw when the platform is already destroyed during route extraction', async () => { |
| 1377 | + const consoleErrorSpy = spyOn(console, 'error'); |
| 1378 | + |
| 1379 | + setAngularAppTestingManifest( |
| 1380 | + [ |
| 1381 | + { |
| 1382 | + path: 'lazy', |
| 1383 | + loadChildren: () => { |
| 1384 | + inject(PlatformRef).destroy(); |
| 1385 | + |
| 1386 | + return []; |
| 1387 | + }, |
| 1388 | + }, |
| 1389 | + ], |
| 1390 | + [{ path: '**', renderMode: RenderMode.Server }], |
| 1391 | + ); |
| 1392 | + |
| 1393 | + const { routeTree, errors } = await extractRoutesAndCreateRouteTree({ url }); |
| 1394 | + expect(errors).toHaveSize(0); |
| 1395 | + expect(routeTree.toObject()).toEqual([{ route: '/lazy', renderMode: RenderMode.Server }]); |
| 1396 | + expect(consoleErrorSpy).not.toHaveBeenCalled(); |
| 1397 | + }); |
| 1398 | + |
| 1399 | + it('should not mask route extraction errors when platform destruction also throws', async () => { |
| 1400 | + const initError = new Error('Initializer failed'); |
| 1401 | + const destroyError = new Error('Destroy hook failed'); |
| 1402 | + const consoleErrorSpy = spyOn(console, 'error'); |
| 1403 | + |
| 1404 | + setAngularAppTestingManifest( |
| 1405 | + [{ path: 'home', component: DummyComponent }], |
| 1406 | + [{ path: '**', renderMode: RenderMode.Server }], |
| 1407 | + undefined, |
| 1408 | + undefined, |
| 1409 | + undefined, |
| 1410 | + undefined, |
| 1411 | + [ |
| 1412 | + provideAppInitializer(() => { |
| 1413 | + inject(DestroyRef).onDestroy(() => { |
| 1414 | + throw destroyError; |
| 1415 | + }); |
| 1416 | + throw initError; |
| 1417 | + }), |
| 1418 | + ], |
| 1419 | + ); |
| 1420 | + |
| 1421 | + await expectAsync(extractRoutesAndCreateRouteTree({ url })).toBeRejectedWith(initError); |
| 1422 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 1423 | + 'An error occurred while destroying the platform.', |
| 1424 | + destroyError, |
| 1425 | + ); |
| 1426 | + }); |
1338 | 1427 | }); |
0 commit comments