Testing With Routing
For components which use routing, there is a special factory available that extends the default one, and provides a stubbed ActivatedRoute so that you can configure additional routing options.
describe('ProductDetailsComponent', () => {
let spectator: SpectatorRouting<ProductDetailsComponent>;
const createComponent = createRoutingFactory({
component: ProductDetailsComponent,
params: { productId: '3' },
data: { title: 'Some title' }
});
beforeEach(() => spectator = createComponent());
it('should display route data title', () => {
expect(spectator.query('.title')).toHaveText('Some title');
});
it('should react to route changes', () => {
spectator.setRouteParam('productId', '5');
// your test here...
});
});
Triggering a navigation
The SpectatorRouting API includes convenient methods for updating the current route:
interface SpectatorRouting<C> extends Spectator<C> {
/**
* Simulates a route navigation by updating the Params, QueryParams and Data observable streams.
*/
triggerNavigation(options?: RouteOptions): void;
/**
* Updates the route params and triggers a route navigation.
*/
setRouteParam(name: string, value: string): void;
/**
* Updates the route query params and triggers a route navigation.
*/
setRouteQueryParam(name: string, value: string): void;
/**
* Updates the route data and triggers a route navigation.
*/
setRouteData(name: string, value: string): void;
/**
* Updates the route fragment and triggers a route navigation.
*/
setRouteFragment(fragment: string | null): void;
}
Integration testing with RouterTestingModule
If you set the stubsEnabled option to false, you can pass a real routing configuration
and setup an integration test using the RouterTestingModule from Angular.
Note that this requires promises to resolve. One way to deal with this, is by making your test async:
describe('Routing integration test', () => {
const createComponent = createRoutingFactory({
component: MyComponent,
declarations: [OtherComponent],
stubsEnabled: false,
routes: [
{
path: '',
component: MyComponent
},
{
path: 'foo',
component: OtherComponent
}
]
});
it('should navigate away using router link', async () => {
const spectator = createComponent();
// wait for promises to resolve...
await spectator.fixture.whenStable();
// test the current route by asserting the location
expect(spectator.inject(Location).path()).toBe('/');
// click on a router link
spectator.click('.link-1');
// don't forget to wait for promises to resolve...
await spectator.fixture.whenStable();
// test the new route by asserting the location
expect(spectator.inject(Location).path()).toBe('/foo');
});
});
Routing Options
The createRoutesFactory function can take the following options, on top of the default Spectator options:
params: initial params to use inActivatedRoutestubqueryParams: initial query params to use inActivatedRoutestubdata: initial data to use inActivatedRoutestuburl: initial URL segments to use inActivatedRoutestubroot: the value forrootfor theActivatedRoutestubparent: the value forparentfor theActivatedRoutestubchildren: the value forchildrenfor theActivatedRoutestubfirstChild: the value forfirstChildfor theActivatedRoutestubfragment: initial fragment to use inActivatedRoutestubstubsEnabled(default:true): enables theActivatedRoutestub, if set tofalseit usesRouterTestingModuleinsteadroutes: ifstubsEnabledis set to false, you can pass aRoutesconfiguration forRouterTestingModule