Luke Childs
7 years ago
4 changed files with 30 additions and 59 deletions
@ -1 +0,0 @@ |
|||
require('browser-env')(['window']); |
@ -1,72 +1,49 @@ |
|||
import EventEmitter from 'events'; |
|||
import test from 'ava'; |
|||
import jsdom from 'jsdom'; |
|||
import Window from 'window'; |
|||
import whenDomReady from '../'; |
|||
|
|||
test.cb('callback fires with global document', t => { |
|||
test.cb('callback fires', t => { |
|||
t.plan(1); |
|||
const { document } = new Window(); |
|||
whenDomReady(() => { |
|||
t.pass(); |
|||
t.end(); |
|||
}); |
|||
}, document); |
|||
}); |
|||
|
|||
test('Promise resolves with global document', async t => { |
|||
test('Promise resolves', async t => { |
|||
const { document } = new Window(); |
|||
t.plan(1); |
|||
await whenDomReady().then(() => t.pass()); |
|||
await whenDomReady(document).then(() => t.pass()); |
|||
}); |
|||
|
|||
test.cb('callback fires with local document', t => { |
|||
t.plan(1); |
|||
const config = { |
|||
html: '', |
|||
onload: window => whenDomReady(() => { |
|||
t.pass(); |
|||
t.end(); |
|||
}, window.document) |
|||
}; |
|||
jsdom.env(config); |
|||
}); |
|||
|
|||
test.cb('Promise resolves with local document', t => { |
|||
test('Promise chain helper passes value through', async t => { |
|||
const { document } = new Window(); |
|||
t.plan(1); |
|||
const config = { |
|||
html: '', |
|||
onload: window => whenDomReady(window.document).then(() => { |
|||
t.pass(); |
|||
t.end(); |
|||
}) |
|||
}; |
|||
jsdom.env(config); |
|||
await Promise |
|||
.resolve('foo') |
|||
.then(whenDomReady.resume(document)) |
|||
.then(val => t.is(val, 'foo')); |
|||
}); |
|||
|
|||
test.cb('callback fires if we run after DOMContentLoaded', t => { |
|||
test('If document.readyState is already "interactive" run cb', async t => { |
|||
const document = { readyState: 'interactive' }; |
|||
t.plan(1); |
|||
const config = { |
|||
html: '', |
|||
created: (err, window) => whenDomReady(() => { |
|||
t.pass(); |
|||
t.end(); |
|||
}, window.document) |
|||
}; |
|||
jsdom.env(config); |
|||
await whenDomReady(document).then(() => t.pass()); |
|||
}); |
|||
|
|||
test.cb('Promise resolves if we run after DOMContentLoaded', t => { |
|||
test('If document.readyState is already "complete" run cb', async t => { |
|||
const document = { readyState: 'complete' }; |
|||
t.plan(1); |
|||
const config = { |
|||
html: '', |
|||
created: (err, window) => whenDomReady(window.document).then(() => { |
|||
t.pass(); |
|||
t.end(); |
|||
}) |
|||
}; |
|||
jsdom.env(config); |
|||
await whenDomReady(document).then(() => t.pass()); |
|||
}); |
|||
|
|||
test('Promise chain helper passes value through', async t => { |
|||
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); |
|||
await Promise |
|||
.resolve('foo') |
|||
.then(whenDomReady.resume()) |
|||
.then(val => t.is(val, 'foo')); |
|||
setTimeout(() => document.emit('DOMContentLoaded'), 500); |
|||
await whenDomReady(document).then(() => t.pass()); |
|||
}); |
|||
|
Loading…
Reference in new issue