vendredi 24 juin 2016

Angular2, testing with Jasmin, router (deprecated)

I've got some problems testing components containing a router.

In my AppComponent I've got the RouteConfig:

@RouteConfig([
  {
    path: '/',
    name: 'SelectVisitorId',
    component:  SelectVisitorIdComponent,
  },
  ...
]}

My spec-class looks like:

describe('Testing router', () => {
  let fixture;
  let location;
  let router;  

  beforeEachProviders(() => [
    ROUTER_PROVIDERS,
    TestComponentBuilder,
    RouteRegistry,
    {provide: Router, useClass: RootRouter},
    {provide: Location, useClass: SpyLocation},
    {provide: ROUTER_PRIMARY_COMPONENT, useValue: AppComponent},
    {provide: LocationStrategy, useClass: HashLocationStrategy},
  ]);

  beforeEach( inject( [TestComponentBuilder, Router, Location] , (_tcb: TestComponentBuilder, router : Router, location: Location) => {
    this.router = router;
    this.location = location;
    return _tcb
      .createAsync(TestSearchDirective)
      .then( (fixture) => {
         this.fixture = fixture;
      });
  }));

  it('Trying to test router', (done) => {
    this.fixture.detectChanges();
    this.router.navigate(['SelectVisitorId', {search: 'suc'}]);
    return new Promise((resolve, reject) => {
      expect(this.location.path()).toBe('/');
      done();
      resolve();
    });
  });

The result will be:

Expected '' to be '/'.

If I log this.router in the Promise-section, it says:

lastNavigationAttempt:""

I've got the same result, using 'then' on the navigate:

  this.router.navigate(['SelectVisitorId', {search: 'suc'}])
    .then( () => {
      expect(this.location.path()).toBe('/');
      done();
    });

The TestSearchDirective looks like:

@Component({
  selector: '[testSearch]',
  directives: [SearchDirective],
  template: '<div searchDirective></div>',
}) class TestSearchDirective {}

Does somebody know a way to get the router-injection working? Or how to mock it?

Thanks!

Aucun commentaire:

Enregistrer un commentaire