-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.helpers.js
57 lines (54 loc) · 1.47 KB
/
jest.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
const helpers = {
onImplementation(bus) {
return (event, handler) => {
let handlers = bus[event]
if (handlers === undefined) {
handlers = []
}
handlers.push(handler)
bus[event] = handlers
}
},
emitImplementation(bus) {
return function() {
const args = Array.from(arguments)
const event = args.shift()
const handlers = bus[event]
if (handlers) {
handlers.forEach((handler) => {
handler.apply({}, args)
})
}
}
},
removeAllListenersImplementation(bus) {
return function(event) {
if(event) {
delete bus[event]
} else {
Object.keys(bus).forEach((event) => {
delete bus[event]
})
}
}
},
apiMember(bus, extended) {
let thisBus = {...bus}
return Object.assign({
activate: jest.fn(),
deactivate: jest.fn(),
remove: jest.fn(),
on: jest.fn(helpers.onImplementation(thisBus)),
off: jest.fn(helpers.onImplementation(thisBus)),
once: jest.fn(helpers.onImplementation(thisBus)),
emit: jest.fn(helpers.emitImplementation(thisBus)),
removeAllListeners: jest.fn(helpers.removeAllListenersImplementation(thisBus)),
mockRestore() {
['activate', 'deactivate', 'on', 'emit', 'once', 'removeAllListeners'].forEach((member) => this[member].mockRestore)
thisBus = {}
}
}, extended)
}
}
global.helpers = helpers
global.mockApiMember = helpers.apiMember