lundi 4 mars 2019

Write tests that rely on `IntersectionObserver` calls

I'm trying to write a test that asserts something when element is in viewport.

I'm using the IntersectionObserver API, and it turns out that the calls to the observers is done when the browser can make them.

So the question is, how should I approach such assertion? Should I just wait some time? How many milliseconds is enough? Is there an event to know when the browser finished calling observers?

Currently I'm doing it like this:

// ... do whatever to make the code observe some elements
document.scrollingElement.scrollTop = window.innerHeight;

// Wait for observer to kick in (tried 1, 5, millis, 10 seems to work)
await new Promise(resolve => setTimeout(resolve, 10));

sinon.assert.calledWith(spy, dom.querySelector(selector));

This works, but I'm wondering if there's something better to wait for than just some time.

Aucun commentaire:

Enregistrer un commentaire