mercredi 12 août 2020

In Jest, is it correct to use setTimeout to wait for DOM manipulation to happen?

I have a function which registers a click handler on a button. Once the button is clicked, the variable NAME is set to an input element's value.

When I try to click the button and read the NAME variable the expect() call returns the sam NAME value before the click event was dispatched.

This is my code (that doesn't work):

test("Input name and receive correctly", () => {
    document.body.innerHTML = `
    <input id="new-account-name">
    <button id="create-button">Create</button>
    `
    boot.boot(null);
    document.getElementById("new-account-name").value = "name";
    document.getElementById("create-button").click();
    expect(boot.NAME).toBe("name");
});

However, if I add a setTimeout()

setTimeout(()=>{
    expect(boot.NAME).toBe("name");
}, 50);

It works and the test passes. Is this how I should be doing this? Or is there an issue with my code?

Aucun commentaire:

Enregistrer un commentaire