I'm working on setting up a specification for routing with Angular2. This is the app component:
import {Component, View} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {Search} from './search/search';
import {SearchResults} from './search-results/search-results';
@Component({
selector: 'my-app'
})
@View({
template: `<div>
<router-outlet></router-outlet>
</div>`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path: '/search', name: 'Search', component: Search, useAsDefault: true},
{path: '/search-results', name: 'SearchResults', component: SearchResults}
])
export class App {
}
This is the search component containing the navigation:
import {Component} from 'angular2/core';
import {Router} from "angular2/router";
@Component({
template: '<div></div>'
})
export class Search {
constructor(public router: Router) {}
onSelect(station:Station):void {
this.router.navigate(['SearchResults']);
}
}
The search results component: import {Component} from "angular2/core";
@Component({
template: '<div></div>'
})
export class SearchResults {
constructor() {
}
}
This is the specification:
import {
it,
inject,
describe,
beforeEachProviders,
MockApplicationRef
} from 'angular2/testing';
import {Component, provide, ApplicationRef} from 'angular2/core';
import {
APP_BASE_HREF, ROUTER_PRIMARY_COMPONENT, ROUTER_PROVIDERS, ROUTER_DIRECTIVES
} from "angular2/router";
import {Search} from "./search";
import {App} from "../app";
import {SearchResults} from "../search-results/search-results";
import {bootstrap} from 'angular2/platform/browser';
import {Http, BaseRequestOptions} from "angular2/http";
import {MockBackend} from "angular2/src/http/backends/mock_backend";
describe('Search', () => {
// provide our implementations or mocks to the dependency injector
beforeEachProviders(() => [
ROUTER_PROVIDERS,
ROUTER_DIRECTIVES,
provide(ROUTER_PRIMARY_COMPONENT, {useClass: App}),
provide(APP_BASE_HREF, {useValue : '/'}),
provide(ApplicationRef, {useClass: MockApplicationRef}),
Search
]);
it('navigates', inject([Search], (search) => {
search.onSelect(CHOICE);
expect(search.router.navigating).toBe(true);
}));
});
The production code works, but the specification doesn't. Apparently there's still something missing in the router setup, because I get the following error: "Component undefined has no route config." I debugged into the Angular2 (beta.1) code and this exception will be thrown on line 2407 of router.dev.js. It means that there's no component recognizer assigned to this component, but I don't know how to resolve it.
I use the following provider function:
import {AppComponent} from "../components/app/app-component";
import {Router, ROUTER_PRIMARY_COMPONENT, RouteRegistry} from "angular2/router";
import {RootRouter} from "angular2/src/router/router";
import {SpyLocation} from "angular2/src/mock/location_mock";
import {Location} from "angular2/src/router/location/location";
export function provideMockRouter():any[] {
return [
RouteRegistry,
provide(Location, {useClass: SpyLocation}),
provide(ROUTER_PRIMARY_COMPONENT, {useValue: AppComponent}),
provide(Router, {useClass: RootRouter}),
];
}
Which I include:
beforeEachProviders(() => [
provideMockRouter(),
...
]);
And use as follows:
it('navigates', testAsyncAwait(async() => {
spyOn(router, 'navigate').and.callThrough();
await component.call();
expect(router.navigate).toHaveBeenCalledWith(['TargetComponent']);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With