I have a test that checks if part DOM element has been removed by an ngIf. When I check the DOM using: fixture.debugElement.query(By.css(".button-area"))
the result
is either null
or a DOM element.
If the result
is null
, then the following test works fine. But, if the test result
contains an element, it doesn't simply fail the test, it freezes up the browser.
The test looks like this:
var result = fixture.debugElement.query(By.css(".button-area"))
expect(result).toBe(null)
I have also tried expect(result).toEqual(null)
and .toBeFalsy()
which have the same result.
What is the proper way to test if a DOM element has been removed properly?
UPDATE 1/23/2017
I have found out this issue is specifc to the element returned by:
fixture.debugElement.query(By.css(".button-area"))
This might be a bug with angular 2 or jasmine. If I use document.getElementByClassName("button-area")
it is not an issue and the test works fine.
The ComponentFixture is a test harness for interacting with the created component and its corresponding element. Access the component instance through the fixture and confirm it exists with a Jasmine expectation: content_copy const component = fixture. componentInstance; expect(component).
detectChanges() tells Angular to run change-detection. Finally! Every time it is called, it updates data bindings like ng-if, and re-renders the component based on the updated data. Calling this function will cause ngOnInit to run only the first time it is called.
TestBed is the primary api for writing unit tests for Angular applications and libraries.
spec. ts file. This file contains unit tests for the main AppComponent. When running tests using the Angular CLI, all unit tests in files with the *. spec.
Since this seems to be a bug with Jasmine or Angular, here is a quick workaround if you are in a bind:
var result = fixture.debugElement.query(By.css(".button-area"))
expect(result === null).toBeTruthy()
You should expect to fix this in your own code when the issue is resolved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With