I am not sure why the DOM element that I try to create in my unit test always return either undefined
or null
. I need the DOM element to attach the event listener to it. I am using Mocha and Chai to do my client side Javascript testing.
I have written the following test:
it('should change the isClick value when a click event occurs', function() {
let target = document.createElement('span');
let isClick = false;
testFunction(target, isClick);
target.dispatchEvent(new Event("click")); // code here does not work since target is either undefined or null
chai.assert.isTrue(isClick, "isClick should now be true");
});
The testFunction looks like this:
function testFunction(target, isClick) {
target.addEventListener("click", (e) => {
isClick = !isClick;
});
}
I am not sure if I am suppose to create a spy or stub for the DOM element, can someone give me pointers on how I should approach a test case like this?
Aucun commentaire:
Enregistrer un commentaire