You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

7 years ago
import EventEmitter from 'events';
import test from 'ava';
7 years ago
import Window from 'window';
import whenDomReady from '../';
7 years ago
test.cb('callback fires', t => {
t.plan(1);
7 years ago
const { document } = new Window();
whenDomReady(() => {
t.pass();
t.end();
7 years ago
}, document);
});
7 years ago
test('Promise resolves', async t => {
const { document } = new Window();
t.plan(1);
7 years ago
await whenDomReady(document).then(() => t.pass());
});
7 years ago
test('Promise chain helper passes value through', async t => {
const { document } = new Window();
t.plan(1);
7 years ago
await Promise
.resolve('foo')
.then(whenDomReady.resume(document))
.then(val => t.is(val, 'foo'));
});
7 years ago
test('If document.readyState is already "interactive" run cb', async t => {
const document = { readyState: 'interactive' };
t.plan(1);
7 years ago
await whenDomReady(document).then(() => t.pass());
});
7 years ago
test('If document.readyState is already "complete" run cb', async t => {
const document = { readyState: 'complete' };
t.plan(1);
7 years ago
await whenDomReady(document).then(() => t.pass());
});
7 years ago
test('If document.readyState is "loading" run cb on DOMContentLoaded event', async t => {
const document = new EventEmitter();
document.addEventListener = document.on;
document.readyState = 'loading';
t.plan(1);
7 years ago
setTimeout(() => document.emit('DOMContentLoaded'), 500);
await whenDomReady(document).then(() => t.pass());
});