Loading external css in Angular 2 and testing it

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?

+4
2

h1.nativeElement.style , . , backgroundColor, red

<h1 style="background-color: red;">hello world!</h1>

unit test protractor e2e. :

expect(element(by.css('h1')).getCssValue('color')).toEqual('rgba(0, 0, 255, 1)');

Protractor e2e Angular2, angular-cli. ng e2e

+3

, , . , - , :

expect(window.getComputedStyle(h1.nativeElement).color).toBe('rgb(0, 0, 255)');
0

Source: https://habr.com/ru/post/1659876/


All Articles