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
// Otherwise wait for DOMContentLoaded
if (loadedStates.includes(doc.readyState)) {
done();
setTimeout(done, 0);
} else {
doc.addEventListener('DOMContentLoaded', done);
}

16
test/unit.js

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

Loading…
Cancel
Save