I am trying to create a component with a test for it. The component has external CSS that I want to test. I think I'm doing it right, but I could not pass the test. Here is my code: app.component.spec.ts
import { AppComponent } from "./app.component";
import { async, TestBed } from "@angular/core/testing";
import { By } from "@angular/platform-browser";
describe("App Component", function () {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [AppComponent],
}).compileComponents()
}));
it("should instantiate component", () => {
let fixture = TestBed.createComponent(AppComponent);
expect(fixture.componentInstance instanceof AppComponent).toBe(true);
});
it("should have expected <h1> text", () => {
let fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
let h1 = fixture.debugElement.query(By.css("h1"));
expect(h1.nativeElement.innerText).toBe("hello world!");
expect(h1.nativeElement.style.color).toBe("blue");
});
});
app.component.ts:
import { Component } from "@angular/core";
@Component({
moduleId: module.id,
selector: "nebula-app",
styleUrls: ["./app.component.css"],
templateUrl: "./app.component.html",
})
export class AppComponent { }
app.component.html:
<h1>hello world!</h1>
app.component.css:
h1{
color: blue;
}
Only the last wait will fail. It sees h1.nativeElement.style.color empty. CSS doesn't seem to load. If I put a style like the line style in html, this test will pass. But having it as an external css will not give the expected result.
What am I doing wrong? Is my assumption wrong that compileComponents should load external CSS and put it as a nativeElement style?