I'm completely new to unit testing, so I may just be doing everything wrong here. I'm using *ngIf to display a data grid from DevExpress once my GET request completes, and I'm trying to verify with the Jasmine test that it only is displayed once my *ngIf condition is set to true.
A truncated version of my grid:
<dx-data-grid #grid *ngIf="!loading">
...
</dx-data-grid>
and my .spec file:
import { ApplicationsComponent } from "./applications.component";
import { ComponentFixture, async, TestBed } from "@angular/core/testing";
import { RouterTestingModule } from "@angular/router/testing";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { DxDataGridModule } from "devextreme-angular";
import { DebugElement } from "@angular/core";
import { By } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
describe("ApplicationPocComponent", () => {
let component: ApplicationsComponent;
let fixture: ComponentFixture<ApplicationsComponent>;
let el: DebugElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ApplicationsComponent],
imports: [RouterTestingModule, HttpClientTestingModule, DxDataGridModule, CommonModule ],
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(ApplicationsComponent);
component = fixture.componentInstance;
el = fixture.debugElement;
});
}));
it("should create applications component", () => {
expect(component).toBeTruthy();
});
it("should display the data grid", () => {
component.loading = true;
fixture.detectChanges();
const dataGrid = el.queryAll(By.css("#grid"));
expect(dataGrid).toBeTruthy("Datagrid not created");
expect(dataGrid).toBeNull("Datagrid is created");
})
});
My first assertion, expect(dataGrid).toBeTruthy()
succeeds, whereas the assertion .toBeNull()
fails. This is the opposite of what I expected, what am I missing here?