Commit 2afcb54a authored by Robert Knight's avatar Robert Knight

Fix cause of flakiness in `observeElementSize` test

 - Wait for a specific event, the observer callback being called, when
   possible. This avoids a possible issue where
   `ResizeObserver`/`MutationObserver` callbacks would not be delivered
   within the fixed time
 - Fix a typo in the name of the test file

Original issue report: https://hypothes-is.slack.com/archives/C1M8NH76X/p1574692275043700
parent ec1bb63a
...@@ -2,9 +2,28 @@ ...@@ -2,9 +2,28 @@
const observeElementSize = require('../observe-element-size'); const observeElementSize = require('../observe-element-size');
/**
* Wait for a condition to become true.
*
* @param {() => boolean} callback
*/
function waitFor(callback) {
return new Promise(resolve => {
const timer = setInterval(() => {
if (callback()) {
clearInterval(timer);
resolve();
}
}, 0);
});
}
/** /**
* Give MutationObserver, ResizeObserver etc. a chance to deliver their * Give MutationObserver, ResizeObserver etc. a chance to deliver their
* notifications. * notifications.
*
* This waits for a fixed amount of time. If you can wait for a specific event
* using `waitFor`, you should do so.
*/ */
function waitForObservations() { function waitForObservations() {
return new Promise(resolve => setTimeout(resolve, 1)); return new Promise(resolve => setTimeout(resolve, 1));
...@@ -40,8 +59,7 @@ describe('observeElementSize', () => { ...@@ -40,8 +59,7 @@ describe('observeElementSize', () => {
startObserving(); startObserving();
content.innerHTML = '<p>different content</p>'; content.innerHTML = '<p>different content</p>';
await waitForObservations(); await waitFor(() => sizeChanged.called);
assert.called(sizeChanged);
stopObserving(); stopObserving();
sizeChanged.reset(); sizeChanged.reset();
...@@ -84,8 +102,7 @@ describe('observeElementSize', () => { ...@@ -84,8 +102,7 @@ describe('observeElementSize', () => {
// Change the content height, which is not directly observed. // Change the content height, which is not directly observed.
content.style.minHeight = '500px'; content.style.minHeight = '500px';
triggerCheck(); triggerCheck();
await waitForObservations(); await waitFor(() => sizeChanged.called);
assert.called(sizeChanged);
sizeChanged.reset(); sizeChanged.reset();
stopObserving(); stopObserving();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment