forked from reduxjs/redux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbindActionCreators.spec.js
104 lines (92 loc) · 3.24 KB
/
bindActionCreators.spec.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { bindActionCreators, createStore } from '../'
import { todos } from './helpers/reducers'
import * as actionCreators from './helpers/actionCreators'
describe('bindActionCreators', () => {
let store
let actionCreatorFunctions
beforeEach(() => {
store = createStore(todos)
actionCreatorFunctions = { ...actionCreators }
Object.keys(actionCreatorFunctions).forEach(key => {
if (typeof actionCreatorFunctions[key] !== 'function') {
delete actionCreatorFunctions[key]
}
})
})
it('wraps the action creators with the dispatch function', () => {
const boundActionCreators = bindActionCreators(actionCreators, store.dispatch)
expect(
Object.keys(boundActionCreators)
).toEqual(
Object.keys(actionCreatorFunctions)
)
const action = boundActionCreators.addTodo('Hello')
expect(action).toEqual(
actionCreators.addTodo('Hello')
)
expect(store.getState()).toEqual([
{ id: 1, text: 'Hello' }
])
})
it('wraps action creators transparently', () => {
const uniqueThis = {}
const argArray = [1, 2, 3]
function actionCreator() {
return { type: 'UNKNOWN_ACTION', this: this, args: [...arguments] }
}
const boundActionCreator = bindActionCreators(actionCreator, store.dispatch)
const boundAction = boundActionCreator.apply(uniqueThis,argArray)
const action = actionCreator.apply(uniqueThis,argArray)
expect(boundAction).toEqual(action)
expect(boundAction.this).toBe(uniqueThis)
expect(action.this).toBe(uniqueThis)
})
it('skips non-function values in the passed object', () => {
const boundActionCreators = bindActionCreators({
...actionCreators,
foo: 42,
bar: 'baz',
wow: undefined,
much: {},
test: null
}, store.dispatch)
expect(
Object.keys(boundActionCreators)
).toEqual(
Object.keys(actionCreatorFunctions)
)
})
it('supports wrapping a single function only', () => {
const actionCreator = actionCreators.addTodo
const boundActionCreator = bindActionCreators(actionCreator, store.dispatch)
const action = boundActionCreator('Hello')
expect(action).toEqual(actionCreator('Hello'))
expect(store.getState()).toEqual([
{ id: 1, text: 'Hello' }
])
})
it('throws for an undefined actionCreator', () => {
expect(() => {
bindActionCreators(undefined, store.dispatch)
}).toThrow(
'bindActionCreators expected an object or a function, instead received undefined. ' +
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
)
})
it('throws for a null actionCreator', () => {
expect(() => {
bindActionCreators(null, store.dispatch)
}).toThrow(
'bindActionCreators expected an object or a function, instead received null. ' +
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
)
})
it('throws for a primitive actionCreator', () => {
expect(() => {
bindActionCreators('string', store.dispatch)
}).toThrow(
'bindActionCreators expected an object or a function, instead received string. ' +
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
)
})
})