From 04f1ec9a5cdff5f5d3dd358a69a37ee8483a6dad Mon Sep 17 00:00:00 2001 From: pcafstockf Date: Mon, 23 Aug 2021 19:51:27 -0700 Subject: [PATCH 1/2] Set ActivatedRouteStub early in order to allow use of TestBed.inject before Component creation. --- .../lib/spectator-routing/create-factory.ts | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/projects/spectator/src/lib/spectator-routing/create-factory.ts b/projects/spectator/src/lib/spectator-routing/create-factory.ts index 66f37f4e..eff26ace 100644 --- a/projects/spectator/src/lib/spectator-routing/create-factory.ts +++ b/projects/spectator/src/lib/spectator-routing/create-factory.ts @@ -45,6 +45,12 @@ export function createRoutingFactory(typeOrOptions: Type | SpectatorRoutin overrideComponentIfProviderOverridesSpecified(options); TestBed.compileComponents(); + + // Configure an ActivatedRouteStub. + const { params, queryParams, data, fragment, url, root, parent, children, firstChild } = { ...options }; + TestBed.overrideProvider(ActivatedRoute, { + useValue: new ActivatedRouteStub({ params, queryParams, data, fragment, url, root, parent, children, firstChild }) + }); }) ); @@ -63,11 +69,17 @@ export function createRoutingFactory(typeOrOptions: Type | SpectatorRoutin }); } - const { params, queryParams, data, fragment, url, root, parent, children, firstChild } = { ...options, ...overrides }; + // If overrides contains additional router related params, overwrite the ActivatedRoute that was configured above (in beforeEach). + let params, queryParams, data, fragment, url, root, parent, children, firstChild; + const routerParamOverrides = ({ params, queryParams, data, fragment, url, root, parent, children, firstChild } = {...overrides}); + if (Object.keys(routerParamOverrides).length > 0) { + // Don't forget the initial set of options may also contain router related params. + ({ params, queryParams, data, fragment, url, root, parent, children, firstChild } = {...options, ...overrides}) + TestBed.overrideProvider(ActivatedRoute, { + useValue: new ActivatedRouteStub({params, queryParams, data, fragment, url, root, parent, children, firstChild}) + }); + } - TestBed.overrideProvider(ActivatedRoute, { - useValue: new ActivatedRouteStub({ params, queryParams, data, fragment, url, root, parent, children, firstChild }) - }); const ngZone = (TestBed).inject ? TestBed.inject(NgZone) : TestBed.get(NgZone); return ngZone.run(() => { From 2f0b43fff32e88c1518c302275a48133ee428d88 Mon Sep 17 00:00:00 2001 From: pcafstockf Date: Mon, 23 Aug 2021 21:24:51 -0700 Subject: [PATCH 2/2] Set ActivatedRouteStub early in order to allow use of TestBed.inject before Component creation. --- .../src/lib/spectator-routing/create-factory.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/projects/spectator/src/lib/spectator-routing/create-factory.ts b/projects/spectator/src/lib/spectator-routing/create-factory.ts index eff26ace..2c94e232 100644 --- a/projects/spectator/src/lib/spectator-routing/create-factory.ts +++ b/projects/spectator/src/lib/spectator-routing/create-factory.ts @@ -69,15 +69,14 @@ export function createRoutingFactory(typeOrOptions: Type | SpectatorRoutin }); } - // If overrides contains additional router related params, overwrite the ActivatedRoute that was configured above (in beforeEach). - let params, queryParams, data, fragment, url, root, parent, children, firstChild; - const routerParamOverrides = ({ params, queryParams, data, fragment, url, root, parent, children, firstChild } = {...overrides}); - if (Object.keys(routerParamOverrides).length > 0) { - // Don't forget the initial set of options may also contain router related params. - ({ params, queryParams, data, fragment, url, root, parent, children, firstChild } = {...options, ...overrides}) - TestBed.overrideProvider(ActivatedRoute, { - useValue: new ActivatedRouteStub({params, queryParams, data, fragment, url, root, parent, children, firstChild}) - }); + if (overrides) { + // If overrides contains additional router related params, overwrite the ActivatedRoute that was configured above (in beforeEach). + if ('params' in overrides || 'queryParams' in overrides || 'data' in overrides || 'fragment' in overrides || 'url' in overrides || 'root' in overrides || 'parent' in overrides || 'children' in overrides || 'firstChild' in overrides) { + const {params, queryParams, data, fragment, url, root, parent, children, firstChild} = {...options, ...overrides}; + TestBed.overrideProvider(ActivatedRoute, { + useValue: new ActivatedRouteStub({params, queryParams, data, fragment, url, root, parent, children, firstChild}) + }); + } } const ngZone = (TestBed).inject ? TestBed.inject(NgZone) : TestBed.get(NgZone);