diff --git a/package.json b/package.json index b117618..7caa5a2 100644 --- a/package.json +++ b/package.json @@ -26,11 +26,6 @@ "array-includes" ] }, - "ava": { - "require": [ - "./test/helpers/setup-browser-env.js" - ] - }, "xo": { "env": "browser", "extends": "xo-lukechilds" @@ -60,11 +55,9 @@ "ava": "^0.19.1", "babel-plugin-array-includes": "^2.0.3", "babel-preset-es2015": "^6.24.1", - "browser-env": "^2.0.23", "camelcase": "^4.0.0", "coveralls": "^2.11.15", "eslint-config-xo-lukechilds": "^1.0.0", - "jsdom": "^11.0.0", "nyc": "^11.0.2", "rollup": "^0.42.0", "rollup-plugin-babel": "^2.7.1", diff --git a/test/helpers/setup-browser-env.js b/test/helpers/setup-browser-env.js deleted file mode 100644 index b81bad5..0000000 --- a/test/helpers/setup-browser-env.js +++ /dev/null @@ -1 +0,0 @@ -require('browser-env')(['window']); diff --git a/test/types.js b/test/types.js index d1ec86c..4b00539 100644 --- a/test/types.js +++ b/test/types.js @@ -7,7 +7,8 @@ test('whenDomReady is a function', t => { }); test('whenDomReady returns a Promise', t => { - t.true(whenDomReady() instanceof Promise); + const { document } = new Window(); + t.true(whenDomReady(document) instanceof Promise); }); test('whenDomReady.resume is a function', t => { @@ -15,7 +16,8 @@ test('whenDomReady.resume is a function', t => { }); test('whenDomReady.resume returns a function that returns a promise', t => { - const returnValue = whenDomReady.resume(); + const { document } = new Window(); + const returnValue = whenDomReady.resume(document); t.is(typeof returnValue, 'function'); t.true(returnValue() instanceof Promise); }); diff --git a/test/unit.js b/test/unit.js index b4b26a4..a2d62f6 100644 --- a/test/unit.js +++ b/test/unit.js @@ -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()); });