Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 tests - get DOM element styles

I want to test the functionality of my hide-show button in my Angular 2 app(Tests are written in Jasmine), so I need to check the value of the display property of the relevant element. How can I get this property using Angular's debugElement? Test code:

let input = fixture.debugElement.query(By.css('input'));
expect(input.styles['visibility']).toBe('false');

I get the error: Expected undefined to be 'false'.

like image 886
liorblob Avatar asked Feb 01 '17 12:02

liorblob


2 Answers

I was having the same issue. The DebugElement.styles is always an empty object even if I set some style to that element explicitly(maybe bug in angular code?). So I would rather get that from the browser native element directly:

let input = fixture.debugElement.query(By.css('input'));
expect(input.nativeElement.style.visibility).toBe('false');
like image 89
LeOn - Han Li Avatar answered Sep 28 '22 01:09

LeOn - Han Li


For anyone stumbling upon this example, the solution for this specific issue with display is the hidden property on the debugElement. It will contain true if the element is hidden and false otherwise.

like image 36
liorblob Avatar answered Sep 28 '22 02:09

liorblob