I have an event listener that runs an asynchronous function, and removes some elements from the DOM after completion:
async function fetchAndRemove() {
try {
const response = await fetch('/endpoint-that-returns-json')
const result = await response.json()
if (result.status === 'Success') {
document.querySelector('.my-element').remove()
}
} catch (error) {
console.log(error)
}
}
function setupListeners () {
const button = document.querySelector('.my-button')
button.addEventListener('click', () => {
fetchAndRemove()
})
}
setupListeners()
In my test, I have:
import fetch from 'jest-fetch-mock';
test('it removes the element after clicking', () => {
fetch.mockResponse(JSON.stringify({ status: 'Success' }))
setupListeners()
document.querySelector('.my-button').click() // .click comes from JSDOM
expect(document.querySelector('.my-element')).toBeNull()
}
However that doesn't work because I believe in the test, setting up the event listener and clicking the button runs synchronously and doesn't wait for the async work to finish.
I tried this idea but no good results:
test('it removes the element after clicking', async () => {
fetch.mockResponse(JSON.stringify({ status: 'Success' }))
setupListeners()
await Promise.resolve(document.querySelector('.my-button').click())
expect(document.querySelector('.my-element')).toBeNull()
}