Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
339 lines (298 loc) · 8.29 KB
/
Copy pathindex.js
File metadata and controls
339 lines (298 loc) · 8.29 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
'use strict'
const { JSDOM } = require('jsdom')
ensureGlobals()
const m = require('mithril')
const code = require('yields-keycode')
const Vnode = require('mithril/render/vnode')
const formatHtml = require('pretty-html-log').highlight
function createWindow() {
return new JSDOM('<!doctype html><html><body></body></html>', {
pretendToBeVisual: true,
}).window
}
function assignGlobals($window) {
global.window = $window
global.document = $window.document
global.requestAnimationFrame =
global.requestAnimationFrame || $window.requestAnimationFrame
}
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 getContainsSelectorParts(selectorString) {
const matches = selectorString.match(/^(.*?):contains\((.*)\)$/)
if (!matches) return null
const [, baseSelector, textContent] = matches
return { baseSelector: baseSelector || '*', textContent }
}
function getBooleanPropSelectorParts(selectorString) {
const matches = selectorString.match(/^(.*?)\[(checked|selected)\]$/)
if (!matches) return null
const [, baseSelector, propName] = matches
return { baseSelector: baseSelector || '*', propName }
}
function scan(api) {
const rootEl = api.rootEl
function find(selectorString, node) {
const containsSelectorParts = getContainsSelectorParts(selectorString)
if (containsSelectorParts) {
const { baseSelector, textContent } = containsSelectorParts
return Array.from(node.querySelectorAll(baseSelector)).filter(function(
el
) {
return el.textContent.includes(textContent)
})
}
const booleanPropSelectorParts = getBooleanPropSelectorParts(selectorString)
if (booleanPropSelectorParts) {
const { baseSelector, propName } = booleanPropSelectorParts
return Array.from(node.querySelectorAll(baseSelector)).filter(function(
el
) {
return el[propName] === true
})
}
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 = createEvent('input', eventData)
const changeEvent = createEvent('change', eventData)
const keyupEvent = createEvent('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 = createEvent(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 = createWindow()
assignGlobals($window)
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() {
assignGlobals(global.window || createWindow())
}
function createEvent(eventName, eventData = {}) {
const event = new global.window.Event(eventName, eventData)
for (const key in eventData) {
if (key === 'type') continue
try {
event[key] = eventData[key]
} catch (error) {
Object.defineProperty(event, key, {
configurable: true,
value: eventData[key],
})
}
}
return event
}
module.exports.ensureGlobals = ensureGlobals