Browse Source

Refactor tests

pull/14/head
Luke Childs 7 years ago
parent
commit
20e7cde99f
  1. 7
      package.json
  2. 1
      test/helpers/setup-browser-env.js
  3. 6
      test/types.js
  4. 75
      test/unit.js

7
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",

1
test/helpers/setup-browser-env.js

@ -1 +0,0 @@
require('browser-env')(['window']);

6
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);
});

75
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());
});

Loading…
Cancel
Save