3

I'm trying to unit test a directive but I'm having a strange issue regarding the routing setup. Despite having a redirect to foo for path: '' the route is still / when I log it inside the test case.

So my question is why is the route not foo when I log it?

describe('IsRouteDirective', () => {

  let directive: IsRouteDirective;
  let hostElement: DebugElement;

  beforeEach(() => {

    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule.withRoutes([
          {
            path: '',
            pathMatch: 'full',
            redirectTo: 'foo'
          },
          {
            path: 'foo',
            component: LibRouteComponent
          },
          {
            path: 'bar',
            component: LibRouteComponent
          }
        ])
      ],
      declarations: [
        LibTestComponent,
        LibRouteComponent,
        IsRouteDirective
      ]
    });
  });

  it('should have a class called \'is-route\'', async(() => {

    // helper function that calls TestBed.overrideComponent, TestBed.compileComponents, TestBed.createComponent and returns the ComponentFixture
    overrideAndCompileComponent(LibTestComponent, `
      <div [libIsRoute]="['foo']"></div>
      <router-outlet></router-outlet>
    `).then((fixture) => {
      hostElement = fixture.debugElement.query(By.directive(IsRouteDirective));
      directive = hostElement.injector.get(IsRouteDirective);
      fixture.detectChanges();

      const injector = getTestBed();
      const router = injector.get(Router);

      // logs '/'
      console.log(router.url);
    });
  }));
});

I tried having a {path: '**', redirectTo: 'foo'} too but it still gives /.

Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175

0 Answers0