This repository has been archived by the owner on Dec 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
test-helpers.js
86 lines (74 loc) · 1.81 KB
/
test-helpers.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
const deepClone = obj => {
if(obj===null || typeof obj !== "object"){
return obj;
}
if(obj instanceof Date){
return new Date(obj.getTime());
}
if(Array.isArray(obj)){
var clonedArr = [];
obj.forEach(function(element){
clonedArr.push(deepClone(element))
});
return clonedArr;
}
let clonedObj = new obj.constructor();
for(var prop in obj){
if(obj.hasOwnProperty(prop)){
clonedObj[prop] = deepClone(obj[prop]);
}
}
return clonedObj;
}
const newTag = (tagName, opts) => {
const element = document.createElement(tagName)
document.body.appendChild(element)
const tag = window.riot.mount(element, deepClone(opts))[0]
// in anticipation of riot 4
tag.$ = q => tag.root.querySelector(q)
tag.$$ = q => tag.root.querySelectorAll(q)
return tag
}
const OGXMLHttpRequest = XMLHttpRequest
const MockXMLHttpRequest = function() {
return {
open(_method, url) {
this.url = url
this.responseText = MockXMLHttpRequest[url]
if (!this.responseText) {
throw `Mocked url response missing for ${url}`
}
},
send() {
this.onload && this.onload(this.responseText)
}
}
}
MockXMLHttpRequest['yay.html'] = "Yay!"
MockXMLHttpRequest.is_mock = true
OGXMLHttpRequest.is_mock = false
const mockAjax = () => {
window.XMLHttpRequest = MockXMLHttpRequest
}
const unmockAjax = () => {
window.XMLHttpRequest = OGXMLHttpRequest
}
const mySpy = (tag, events) => {
let last = {}
const spies = {}
events.forEach( e => {
spies[e] = sinon.spy()
tag.on(e, spies[e])
})
const validate = (values) => {
Object.assign(last,values)
events.forEach(e => {
spies[e].callCount.should.equal(last[e] || 0)
})
}
validate.reset = () => {
last = {}
events.forEach(e => spies[e].resetHistory())
}
return validate
}