1
- const Redux = require ( `redux` )
2
1
const { actions } = require ( `../actions` )
3
2
const nodeReducer = require ( `../reducers/nodes` )
4
3
const nodeTouchedReducer = require ( `../reducers/nodes-touched` )
5
4
6
5
jest . mock ( `../../db/nodes` )
7
6
jest . mock ( `../nodes` )
8
7
9
- const store = Redux . createStore (
10
- Redux . combineReducers ( { nodeReducer, nodeTouchedReducer } ) ,
11
- { }
12
- )
8
+ const dispatch = jest . fn ( )
9
+
13
10
describe ( `Create and update nodes` , ( ) => {
14
11
beforeEach ( ( ) => {
15
- store . dispatch ( {
16
- type : `DELETE_CACHE` ,
17
- } )
12
+ dispatch . mockClear ( )
18
13
} )
19
14
20
15
it ( `allows creating nodes` , ( ) => {
21
- const action = actions . createNode (
16
+ actions . createNode (
22
17
{
23
18
id : `hi` ,
24
19
children : [ ] ,
@@ -32,13 +27,14 @@ describe(`Create and update nodes`, () => {
32
27
{
33
28
name : `tests` ,
34
29
}
35
- )
30
+ ) ( dispatch )
31
+ const action = dispatch . mock . calls [ 0 ] [ 0 ]
36
32
expect ( action ) . toMatchSnapshot ( )
37
33
expect ( nodeReducer ( undefined , action ) ) . toMatchSnapshot ( )
38
34
} )
39
35
40
36
it ( `allows updating nodes` , ( ) => {
41
- const action = actions . createNode (
37
+ actions . createNode (
42
38
{
43
39
id : `hi` ,
44
40
children : [ ] ,
@@ -61,8 +57,10 @@ describe(`Create and update nodes`, () => {
61
57
{
62
58
name : `tests` ,
63
59
}
64
- )
65
- const updateAction = actions . createNode (
60
+ ) ( dispatch )
61
+ const action = dispatch . mock . calls [ 0 ] [ 0 ]
62
+
63
+ actions . createNode (
66
64
{
67
65
id : `hi` ,
68
66
children : [ ] ,
@@ -82,7 +80,9 @@ describe(`Create and update nodes`, () => {
82
80
{
83
81
name : `tests` ,
84
82
}
85
- )
83
+ ) ( dispatch )
84
+ const updateAction = dispatch . mock . calls [ 1 ] [ 0 ]
85
+
86
86
let state = nodeReducer ( undefined , action )
87
87
state = nodeReducer ( state , updateAction )
88
88
expect ( state . get ( `hi` ) . pickle ) . toEqual ( false )
@@ -91,7 +91,7 @@ describe(`Create and update nodes`, () => {
91
91
} )
92
92
93
93
it ( `nodes that are added are also "touched"` , ( ) => {
94
- const action = actions . createNode (
94
+ actions . createNode (
95
95
{
96
96
id : `hi` ,
97
97
children : [ ] ,
@@ -105,13 +105,15 @@ describe(`Create and update nodes`, () => {
105
105
{
106
106
name : `tests` ,
107
107
}
108
- )
108
+ ) ( dispatch )
109
+ const action = dispatch . mock . calls [ 0 ] [ 0 ]
110
+
109
111
let state = nodeTouchedReducer ( undefined , action )
110
112
expect ( state [ `hi` ] ) . toBe ( true )
111
113
} )
112
114
113
115
it ( `allows adding fields to nodes` , ( ) => {
114
- const action = actions . createNode (
116
+ actions . createNode (
115
117
{
116
118
id : `hi` ,
117
119
children : [ ] ,
@@ -125,7 +127,8 @@ describe(`Create and update nodes`, () => {
125
127
{
126
128
name : `tests` ,
127
129
}
128
- )
130
+ ) ( dispatch )
131
+ const action = dispatch . mock . calls [ 0 ] [ 0 ]
129
132
let state = nodeReducer ( undefined , action )
130
133
131
134
const addFieldAction = actions . createNodeField (
@@ -138,12 +141,13 @@ describe(`Create and update nodes`, () => {
138
141
name : `test` ,
139
142
}
140
143
)
144
+
141
145
state = nodeReducer ( state , addFieldAction )
142
146
expect ( state ) . toMatchSnapshot ( )
143
147
} )
144
148
145
149
it ( `throws error if a field is updated by a plugin not its owner` , ( ) => {
146
- const action = actions . createNode (
150
+ actions . createNode (
147
151
{
148
152
id : `hi` ,
149
153
children : [ ] ,
@@ -157,7 +161,8 @@ describe(`Create and update nodes`, () => {
157
161
{
158
162
name : `tests` ,
159
163
}
160
- )
164
+ ) ( dispatch )
165
+ const action = dispatch . mock . calls [ 0 ] [ 0 ]
161
166
let state = nodeReducer ( undefined , action )
162
167
163
168
const addFieldAction = actions . createNodeField (
@@ -202,7 +207,7 @@ describe(`Create and update nodes`, () => {
202
207
{
203
208
name : `pluginA` ,
204
209
}
205
- )
210
+ ) ( dispatch )
206
211
207
212
function callActionCreator ( ) {
208
213
actions . createNode (
@@ -219,7 +224,7 @@ describe(`Create and update nodes`, () => {
219
224
{
220
225
name : `pluginB` ,
221
226
}
222
- )
227
+ ) ( dispatch )
223
228
}
224
229
225
230
expect ( callActionCreator ) . toThrowErrorMatchingSnapshot ( )
@@ -244,7 +249,7 @@ describe(`Create and update nodes`, () => {
244
249
{
245
250
name : `pluginA` ,
246
251
}
247
- )
252
+ ) ( dispatch )
248
253
}
249
254
250
255
expect ( callActionCreator ) . toThrowErrorMatchingSnapshot ( )
0 commit comments