-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
86 lines (80 loc) · 2.22 KB
/
test.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
'use strict'
const { deepEqual } = require('assert')
const { createStore } = require('redux')
const { coalesceFunks, funkMiddleware, call, runFunks } = require('./src/index.js')
const test = require('tape')
const asyncAction = payload => new Promise((resolve, reject) => {
resolve({type: 'SECOND', payload})
})
const reducer = coalesceFunks((state = {text: 'initial'}, action) => {
switch (action.type) {
case 'FIRST':
call(action, [asyncAction, ['payload']])
return Object.assign({}, state, {text: 'foo'})
case 'SECOND':
return Object.assign({}, {text: action.payload})
default:
return state
}
})
test('integration: `call` and `coalesceFunks`', t => {
const { dispatch, getState } = createStore(reducer)
const action = {type: 'FIRST'}
dispatch(action)
t.deepEqual(
getState().funks[0],
[asyncAction, ['payload']],
'funks are added to `state`')
t.deepEqual(
Object.getOwnPropertySymbols(action),
[],
'reducer is pure (action restored to the way it was)')
t.deepEqual(
getState().text,
'foo',
'rest of state is updated correctly')
dispatch({type: 'SECOND', payload: 'new payload'})
t.deepEqual(
getState(),
{funks: [], text: 'new payload'},
'funks are reset on each action'
)
t.end()
})
test('`runFunks`', t => {
// BEGIN FIXTURES
let calledWith
const mock = arg => {
calledWith = arg
return Promise.resolve({type: 'BAR'})
}
const reducer = coalesceFunks((state={}, action) => {
switch (action.type) {
case 'FOO':
call(action, [mock, [action.payload]])
return state
case 'BAR':
return Object.assign(state, {'funksHaveRun': true})
}
})
const store = createStore(reducer)
const { dispatch, getState } = store
runFunks(store)
const action = {type: 'FOO', payload: 'hi'}
dispatch(action)
// END FIXTURES
t.deepEqual(
calledWith,
action.payload,
'calls funks'
)
// wait to next tick of event loop for promise to resolve
setTimeout(() => {
t.deepEqual(
getState().funksHaveRun,
true,
'dispatches actions'
)
t.end()
})
})