4

I want to create a test that checks that there is binding between background color and component property.

Component template snippet

<div
    class="colored-div"
    [style.background-color]="color"
</div>

Test

it('background should have correct color', () => {
    component.color = '#000000';
    fixture.detectChanges();
    const colorEl: HTMLElement = fixture.debugElement.query(By.css('.colored-div')).nativeElement;
    expect(colorEl.style.backgroundColor).toBe('#000000');
});

I expect to pass this test, but angular converts background color to rgb and test fails with error: "Expected 'rgb(0, 0, 0)' to be '#000000'". So the question is: is there a way to stop angular from converting value to rgb or should I change my test assertion to

expect(colorEl.style.backgroundColor).toBe('rgb(0, 0, 0)');
orlyohreally
  • 410
  • 6
  • 19

2 Answers2

1

Actually it is not Angular but the browsers: they return RGB when using backgroundColor. You can either change your assertion to be RGB (as you wrote) or just use an additional function which converts RGB into HEX (pick any you like function example)

Tatsiana
  • 49
  • 5
0

As browser returns rgb, you can provide the input as rgb and test the assertion as below:

it('background should have correct color', () => {
let customColor = 'rgb(0,0,0)';
    component.color = customColor;
    fixture.detectChanges();
    const colorEl: HTMLElement = fixture.debugElement.query(By.css('.colored-div')).nativeElement;
    expect(colorEl.style.backgroundColor).toBe(customColor);
});