Угловой юнит-тестовый маршрут застрял в корне вместо маршрута, определенного в redirectTo - PullRequest
2 голосов
/ 03 июня 2019

Я пытаюсь выполнить модульное тестирование директивы, но у меня странная проблема, связанная с настройкой маршрутизации.Несмотря на перенаправление на foo для path: '', маршрут все еще равен /, когда я регистрирую его в тестовом примере.

Итак, мой вопрос: почему маршрут не foo, когда я его регистрирую?

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);
    });
  }));
});

Я тоже пытался получить {path: '**', redirectTo: 'foo'}, но он все равно дает /.

...