-
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(routing): add option to disable stubs and use RouterTestingModule (
- Loading branch information
Showing
11 changed files
with
255 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 31 additions & 10 deletions
41
projects/spectator/src/lib/spectator-routing/initial-module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,56 @@ | ||
import { ActivatedRoute, Router } from '@angular/router'; | ||
import { ActivatedRoute, Event, Router } from '@angular/router'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { Subject } from 'rxjs'; | ||
|
||
import { ModuleMetadata } from '../base/initial-module'; | ||
import { initialSpectatorModule } from '../spectator/initial-module'; | ||
|
||
import { ActivatedRouteStub } from './activated-route-stub'; | ||
import { SpectatorRoutingOptions } from './options'; | ||
import { RouterLinkDirectiveStub } from './router-link-stub'; | ||
import { RouterStub } from './router-stub'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function initialRoutingModule<S>(options: Required<SpectatorRoutingOptions<S>>): ModuleMetadata { | ||
const moduleMetadata = initialSpectatorModule(options); | ||
|
||
if (options.mockRouterLinks) { | ||
if (options.mockRouterLinks && options.stubsEnabled) { | ||
moduleMetadata.declarations.push(RouterLinkDirectiveStub); | ||
} | ||
|
||
moduleMetadata.providers.push(options.mockProvider(Router)); | ||
if (options.stubsEnabled) { | ||
moduleMetadata.providers.push( | ||
options.mockProvider(RouterStub, { | ||
events: new Subject<Event>(), | ||
emitRouterEvent(event: Event): void { | ||
this.events.next(event); | ||
} | ||
}), | ||
{ | ||
provide: Router, | ||
useExisting: RouterStub | ||
} | ||
); | ||
|
||
moduleMetadata.providers.push([ | ||
{ | ||
provide: ActivatedRoute, | ||
useFactory: () => | ||
new ActivatedRouteStub({ | ||
moduleMetadata.providers.push( | ||
{ | ||
provide: ActivatedRouteStub, | ||
useValue: new ActivatedRouteStub({ | ||
params: options.params, | ||
queryParams: options.queryParams, | ||
data: options.data | ||
}) | ||
} | ||
]); | ||
}, | ||
{ | ||
provide: ActivatedRoute, | ||
useExisting: ActivatedRouteStub | ||
} | ||
); | ||
} else { | ||
moduleMetadata.imports.push(RouterTestingModule.withRoutes(options.routes)); | ||
} | ||
|
||
return moduleMetadata; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Router, Event } from '@angular/router'; | ||
|
||
export abstract class RouterStub extends Router { | ||
public abstract emitRouterEvent(event: Event): void; | ||
} | ||
|
||
export function isRouterStub(router: Router): router is RouterStub { | ||
return 'emitRouterEvent' in router; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.