-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (61 loc) · 1.62 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
import * as promiseLib from './src/mockPromise'
import * as fetchLib from './src/mockFetch'
import timetable from './src/timetable'
import Snapshot from './src/snapshot'
import snapActionCore from './src/snapAction'
import config from './src/config'
/**
* Resets config and timetable
*/
const reset = () => {
config.reset()
timetable.reset()
promiseLib.useReal()
fetchLib.useReal()
}
/**
* @typedef {{name:string, type: ("resolve" | "reject"), payload}} Resolution
*/
/**
* Takes snapshot of action's evaluation
* @param {Function} action action to test
* @param {{state, getters, commit: Function, dispatch: Function, payload}} mocks arguments passed to the action, payload is the second argument
* @param {[(string | Resolution)]} resolutions
* @returns {(Array | Promise<Array>)}
*/
const snapAction = (action, mocks={}, resolutions=[], snapshot=new Snapshot()) => {
if(Array.isArray(mocks)) {
resolutions = mocks
mocks = {}
}
const commit = mocks.commit || (() => {})
const dispatch = mocks.dispatch || (() => {})
return snapActionCore(
action,
{
payload: mocks.payload,
state: mocks.state,
getters: mocks.getters,
commit,
dispatch
},
resolutions,
config.options,
snapshot
)
}
export default {
snapAction,
reset,
timetable,
resetTimetable: timetable.reset,
config: config.options,
resetConfig: config.reset,
Snapshot,
mockFetch: fetchLib.mockFetch,
useMockFetch: fetchLib.useMock,
useRealFetch: fetchLib.useReal,
MockPromise: promiseLib.MockPromise,
useMockPromise: promiseLib.useMock,
useRealPromise: promiseLib.useReal,
}