-
Notifications
You must be signed in to change notification settings - Fork 210
/
util.js
46 lines (40 loc) · 1.21 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
jest.setTimeout(1000)
const fs = require('fs')
const path = require('path')
const { JSDOM } = require('jsdom')
function getFilename (file) {
const dir = path.dirname(file)
const base = path.basename(file)
const files = fs.readdirSync(path.resolve(dir, '../'))
const id = base.match(/^\d+\.\d+/)[0]
return files.filter(f => f.startsWith(id) && f.endsWith('.html'))[0]
}
exports.createTestCase = (file, fn, extra) => {
const fileToTest = getFilename(file)
it(fileToTest.replace(/\.html$/, ''), done => {
JSDOM.fromFile(
path.resolve(file, `../../${fileToTest}`),
{
resources: 'usable',
runScripts: "dangerously"
}
).then(({ window }) => {
// test helper
window.$click = function (target) {
var evt = window.document.createEvent('HTMLEvents')
evt.initEvent('click', false, true)
window.document.querySelector(target).dispatchEvent(evt)
}
if (extra) {
extra(window)
}
window.addEventListener('load', () => {
const log = window.console.log = jest.fn(() => {})
if (window.Vue) {
window.Vue.config.productionTip = false
}
fn(window, log.mock.calls, done)
})
})
})
}