Browse Source

Make sure callback always executes async

transpile
Luke Childs 8 years ago
parent
commit
1284c58701
  1. 2
      src/index.js
  2. 16
      test/unit.js

2
src/index.js

@ -18,7 +18,7 @@ const whenDomReady = (cb, doc) => new Promise(resolve => {
// Resolve now if DOM has already loaded // Resolve now if DOM has already loaded
// Otherwise wait for DOMContentLoaded // Otherwise wait for DOMContentLoaded
if (loadedStates.includes(doc.readyState)) { if (loadedStates.includes(doc.readyState)) {
done(); setTimeout(done, 0);
} else { } else {
doc.addEventListener('DOMContentLoaded', done); doc.addEventListener('DOMContentLoaded', done);
} }

16
test/unit.js

@ -2,21 +2,27 @@ import test from 'ava';
import jsdom from 'jsdom'; import jsdom from 'jsdom';
import whenDomReady from '../'; import whenDomReady from '../';
test('callback fires with global window', async t => { test.cb('callback fires with global window', t => {
t.plan(1); t.plan(1);
whenDomReady(() => t.pass()); whenDomReady(() => {
t.pass();
t.end();
});
}); });
test('Promise resolves with global window', async t => { test('Promise resolves with global window', async t => {
t.plan(1); t.plan(1);
whenDomReady().then(() => t.pass()); await whenDomReady().then(() => t.pass());
}); });
test('callback fires with local document', async t => { test.cb('callback fires with local document', t => {
t.plan(1); t.plan(1);
const config = { const config = {
html: '', html: '',
onload: window => whenDomReady(() => t.pass(), window.document) onload: window => whenDomReady(() => {
t.pass();
t.end();
}, window.document)
}; };
jsdom.env(config); jsdom.env(config);
}); });

Loading…
Cancel
Save