I want to write a test to check if an element exists on the page after I click on it. So when I click on the element with the class "addItem", this element is hidden using an *ngIf
. I tried it like this:
it('Should handle click on .addItem button', () => {
spyOn(component, 'addItem');
addItemDebugElement = componentFixture.debugElement.query(By.css('.addItem'));
addItemDebugElement.nativeElement.click(); // click on the button
expect(addItemDebugElement).toExist();
});
but it says: Property 'toExist' does not exist on type 'Matchers<DebugElement>'.
Can you please advise how to do this? Many thanks!
JASMINE V2.0+ var dummyElement = document. createElement('div'); document. getElementById = jasmine. createSpy('HTML Element').
DebugElement is an Angular class that contains all kinds of references and methods relevant to investigate an element as well as component fixture.debugElement.query(By.css('#shan'))
I'd recommend you use
// Add Item Debug Ele comes out as null.
addItemDebugElement = componentFixture.debugElement.query(By.css('.addItem'));
expect(addItemDebugElement).toBeFalsy();
and...
// Add Item Debug Ele comes out as not null.
addItemDebugElement = componentFixture.debugElement.query(By.css('.addItem'));
expect(addItemDebugElement).toBeTruthy();
You can try this to check if the element exist on the Dom
expect($(document)).toContain(addItemDebugElement)
or
expect(addItemDebugElement).toBeInDom();
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