forked from reduxjs/react-redux
/
useActions.spec.js
121 lines (98 loc) · 3.16 KB
/
useActions.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*eslint-disable react/display-name*/
import React from 'react'
import { createStore } from 'redux'
import { renderHook } from 'react-hooks-testing-library'
import { Provider as ProviderMock, useActions } from '../../src/index.js'
describe('React', () => {
describe('hooks', () => {
describe('useActions', () => {
let store
let dispatchedActions = []
beforeEach(() => {
const reducer = (state = 0, action) => {
dispatchedActions.push(action)
if (action.type === 'inc1') {
return state + 1
}
if (action.type === 'inc') {
return state + action.amount
}
return state
}
store = createStore(reducer)
dispatchedActions = []
})
it('supports a single action creator', () => {
const { result } = renderHook(
() => useActions(() => ({ type: 'inc1' })),
{ wrapper: props => <ProviderMock {...props} store={store} /> }
)
result.current()
expect(dispatchedActions).toEqual([{ type: 'inc1' }])
})
it('supports an object of action creators', () => {
const { result } = renderHook(
() =>
useActions({
inc1: () => ({ type: 'inc1' }),
inc2: () => ({ type: 'inc', amount: 2 })
}),
{ wrapper: props => <ProviderMock {...props} store={store} /> }
)
result.current.inc1()
result.current.inc2()
expect(dispatchedActions).toEqual([
{ type: 'inc1' },
{ type: 'inc', amount: 2 }
])
})
it('supports an array of action creators', () => {
const { result } = renderHook(
() =>
useActions([
() => ({ type: 'inc1' }),
() => ({ type: 'inc', amount: 2 })
]),
{ wrapper: props => <ProviderMock {...props} store={store} /> }
)
result.current[0]()
result.current[1]()
expect(dispatchedActions).toEqual([
{ type: 'inc1' },
{ type: 'inc', amount: 2 }
])
})
it('passes through arguments', () => {
const reducer = (state = 0, action) => {
dispatchedActions.push(action)
if (action.type === 'adjust') {
return action.isAdd ? state + action.amount : state - action.amount
}
return state
}
const store = createStore(reducer)
dispatchedActions = []
const { result } = renderHook(
() =>
useActions({
adjust: (amount, isAdd = true) => ({
type: 'adjust',
amount,
isAdd
})
}),
{ wrapper: props => <ProviderMock {...props} store={store} /> }
)
result.current.adjust(1)
result.current.adjust(2)
result.current.adjust(1, false)
expect(dispatchedActions).toEqual([
{ type: 'adjust', amount: 1, isAdd: true },
{ type: 'adjust', amount: 2, isAdd: true },
{ type: 'adjust', amount: 1, isAdd: false }
])
})
// TODO: test for deps
})
})
})