3

Is there any way in which I could test say the height css property of an element?

I have a function that changes the height of a div and it would be nice to check in the tests that the function operates correctly...

ie:

<div #myDiv>Hello world</div>

@ViewChild('myDiv') myDiv: ElementRef;

myFunct() {
  this.myDiv.nativeElement.style.height = 500;
}

it('should increase div size', () => {
  // Here is where I get stuck...
});

Update

Implementing a test such as:

it('should return correct height of dropdown when initialised', () => {
    component.myFunct();

    expect(component.dropdown.nativeElement.style.height).toBe(34);
  });

results in a test failure with message:

Expected '' to be 500.

1 Answer 1

6

Something like this...

  1. Exposed the myDiv publicly

  2. Setup the testbed for the component (usually added by default)

  3. Add

component.myFunct();

expect(component.myDiv.nativeElement.style.height).toBe(500);
Sign up to request clarification or add additional context in comments.

2 Comments

I had done something similar to this prior to posting the question. The problem I have when doing this, is that I get a failure and returned, Expected '' to be 34....
Ah I understand what is going on now. I was mixing myself up between nativeElement > style > height and nativeElement > clientHeight

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.