RouterLinkActive не работает во время модульного тестирования, но работает при внешнем модульном тестировании.
Вот HTML:
<header class="site-tab">
<nav class="c-tab l-wrap">
<div
class="c-tab__item"
routerLink="/companies"
routerLinkActive="c-tab__item--active"
>
<p class="c-tab__text">Companies</p>
</div>
<div
class="c-tab__item"
routerLink="/products"
routerLinkActive="c-tab__item--active"
>
<p class="c-tab__text">Products</p>
</div>
<div
class="c-tab__item"
routerLink="/invoices"
routerLinkActive="c-tab__item--active"
>
<p class="c-tab__text">Invoices</p>
</div>
</nav>
</header>
, а вот мой файл spe c:
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { RouterTestingModule } from '@angular/router/testing';
import { TabsComponent } from './tabs.component';
describe('TabsComponent', () => {
let component: TabsComponent;
let fixture: ComponentFixture<TabsComponent>;
let de: DebugElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TabsComponent],
imports: [RouterTestingModule]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TabsComponent);
component = fixture.componentInstance;
de = fixture.debugElement;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should set clicked tab to active', () => {
de.queryAll(By.css('.c-tab__item'))[1].nativeElement.click();
expect(
de.query(By.css('.c-tab__item--active .c-tab__text')).nativeElement
.innerHTML
).toContain('Product');
});
Я ожидаю, что вкладка будет активной после того, как я нажму на нее, но я получаю эту ошибку TypeError: de.query(...) is null in http://localhost:9876/_karma_webpack_/main.js (line 872)
, которая, как я считаю, означает, что ни одна вкладка не была установлена активной.