-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
276 lines (243 loc) · 6.57 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'use strict'
const domino = require('domino')
ensureGlobals()
const m = require('mithril')
const Event = require('domino/lib/Event')
const code = require('yields-keycode')
const Vnode = require('mithril/render/vnode')
const formatHtml = require('pretty-html-log').highlight
function isString(thing) {
return Object.prototype.toString.call(thing) === '[object String]'
}
function isArray(thing) {
return Object.prototype.toString.call(thing) === '[object Array]'
}
function isComponent(thing) {
return !!(
(thing && (typeof thing === 'object' && thing.view)) ||
isFunction(thing) ||
isClass(thing)
)
}
function isFunction(thing) {
return typeof thing === 'function' && !isClass(thing)
}
function isBabelTranspiledClass(thing) {
const code = thing.toString().replace(/^[^{]+{/, '')
return (
// Regular Babel transpiled class
/(?:^|\s+)_classCallCheck\(/.test(code) ||
// Babel with @babel/transform-runtime and Webpack
/(?:^|\s+)_[^\s]+_classCallCheck__[^\s()]+\(/.test(code) ||
// Babel with @babel/transform-runtime (useESModules: true) and Webpack
/(?:^|\s+)Object\(_[^\s]+_classCallCheck__[^\s()]+\)\(/.test(code)
)
}
function isClass(thing) {
return (
typeof thing === 'function' &&
(/^\s*class\s/.test(thing.toString()) || // ES6 class
isBabelTranspiledClass(thing)) // Babel class
)
}
function consoleLogHtml(els) {
// eslint-disable-next-line no-console
console.log(els.map(el => formatHtml(el.outerHTML)).join('---------\n'))
}
function scan(api) {
const rootEl = api.rootEl
function find(selectorString, node) {
return Array.from(node.querySelectorAll(selectorString))
}
function first(selector) {
const node = rootEl.querySelector(selector)
if (!node) {
throw new Error('No element matches ' + selector)
}
return node
}
function has(selector) {
return find(selector, rootEl).length > 0
}
function contains(value, node) {
return !!find(':contains(' + value + ')', node).length
}
function shouldHaveAtLeast(minCount, selector) {
const actualCount = find(selector, rootEl).length
if (actualCount < minCount) {
throw new Error(
'Wrong count of elements that matches "' +
selector +
'"\n expected: >=' +
minCount +
'\n actual: ' +
actualCount
)
}
return true
}
function shouldHave(expectedCount, selector) {
if (!selector) {
return isArray(expectedCount)
? shouldHaveCollection(expectedCount)
: shouldHaveAtLeast(1, expectedCount)
}
const actualCount = find(selector, rootEl).length
if (actualCount !== expectedCount) {
throw new Error(
'Wrong count of elements that matches "' +
selector +
'"\n expected: ' +
expectedCount +
'\n actual: ' +
actualCount
)
}
return true
}
function shouldHaveCollection(selectors) {
selectors.forEach(function(selector) {
shouldHaveAtLeast(1, selector)
})
return true
}
function shouldNotHave(selector) {
shouldHave(0, selector)
return true
}
function shouldContain(string) {
if (!contains(string, rootEl)) {
throw new Error('Expected "' + string + '" not found!')
}
return true
}
function shouldNotContain(string) {
if (contains(string, rootEl)) {
throw new Error('Unexpected "' + string + '" found!')
}
return true
}
function setValue(selector, string, eventData = {}) {
const el = first(selector)
el.value = string
const inputEvent = new Event('input', eventData)
const changeEvent = new Event('change', eventData)
const keyupEvent = new Event('keyup', eventData)
el.dispatchEvent(inputEvent)
el.dispatchEvent(changeEvent)
el.dispatchEvent(keyupEvent)
if (
inputEvent.redraw !== false &&
changeEvent.redraw !== false &&
keyupEvent.redraw !== false
) {
api.redraw()
}
}
function trigger(eventName) {
return function(selector, eventData) {
const event = new Event(eventName, eventData)
const el = first(selector)
el.dispatchEvent(event)
if (event.redraw !== false) {
api.redraw()
}
}
}
function triggerKey(eventName) {
const fire = trigger(eventName)
return function handleEvent(selector, key, eventData = {}) {
const keyCode = isString(key) ? code(key) : key
const defaultEvent = {
altKey: false,
shiftKey: false,
ctrlKey: false,
type: eventName,
keyCode,
which: keyCode,
}
fire(selector, { ...defaultEvent, ...eventData })
}
}
shouldHave.at = {
least: shouldHaveAtLeast,
}
api.first = first
api.has = has
api.contains = function(value) {
return contains(value, rootEl)
}
api.find = function(selector) {
return find(selector, rootEl)
}
api.setValue = setValue
;[
'focus',
'click',
'blur',
'mousedown',
'mouseup',
'mouseover',
'mouseout',
'mouseenter',
'mouseleave',
'mousemove',
'pointerdown',
'pointerup',
'pointerover',
'pointerout',
'pointerenter',
'pointerleave',
'pointermove',
'pointercancel',
'contextmenu',
].map(function(eventName) {
api[eventName] = trigger(eventName)
})
api.keydown = triggerKey('keydown')
api.keypress = triggerKey('keypress')
api.keyup = triggerKey('keyup')
api.trigger = function(selector, eventName, event, silent) {
trigger(eventName)(selector, event, silent)
}
api.should = {
not: {
have: shouldNotHave,
contain: shouldNotContain,
},
have: shouldHave,
contain: shouldContain,
}
api.log = function(selector, logFn = consoleLogHtml) {
logFn(api.find(selector))
return api
}
return api
}
module.exports = function init(componentOrRootNode, nodeOrAttrs) {
const $window = global.window = domino.createWindow('')
const render = require('mithril/render/render')($window)
let rootNode = {
view: () => {
return isComponent(componentOrRootNode)
? m(componentOrRootNode, nodeOrAttrs)
: componentOrRootNode
},
}
const redraw = () => render($window.document.body, Vnode(rootNode))
redraw()
const onremove = () => {
componentOrRootNode = null
redraw()
}
return scan({
redraw,
onremove,
rootEl: $window.document.body,
})
}
function ensureGlobals() {
global.window = global.window || domino.createWindow('')
global.requestAnimationFrame = global.requestAnimationFrame || global.window.requestAnimationFrame
}
module.exports.ensureGlobals = ensureGlobals