1
+ /* @flow strict-local */
2
+
1
3
import deepFreeze from 'deep-freeze' ;
2
4
5
+ import * as eg from '../../__tests__/lib/exampleData' ;
3
6
import {
4
- REGISTER_COMPLETE ,
5
- ACCOUNT_SWITCH ,
6
7
EVENT_USER_GROUP_ADD ,
7
8
EVENT_USER_GROUP_REMOVE ,
8
9
EVENT_USER_GROUP_UPDATE ,
@@ -14,36 +15,29 @@ import userGroupsReducer from '../userGroupsReducer';
14
15
describe ( 'userGroupsReducer' , ( ) => {
15
16
describe ( 'REGISTER_COMPLETE' , ( ) => {
16
17
test ( 'when data is provided init state with it' , ( ) => {
17
- const prevState = deepFreeze ( [ ] ) ;
18
- const action = deepFreeze ( {
19
- type : REGISTER_COMPLETE ,
20
- data : {
21
- realm_user_groups : [
22
- {
23
- id : 1 ,
24
- name : 'Some user group' ,
25
- members : [ ] ,
26
- } ,
27
- ] ,
28
- } ,
29
- } ) ;
18
+ const group = eg . makeUserGroup ( ) ;
19
+
20
+ const prevState = deepFreeze ( eg . plusReduxState . userGroups ) ;
21
+
22
+ const action = eg . mkActionRegisterComplete ( { realm_user_groups : [ group ] } ) ;
30
23
31
24
const actualState = userGroupsReducer ( prevState , action ) ;
32
25
33
- expect ( actualState ) . toEqual ( [
34
- {
35
- id : 1 ,
36
- name : 'Some user group' ,
37
- members : [ ] ,
38
- } ,
39
- ] ) ;
26
+ expect ( actualState ) . toEqual ( [ group ] ) ;
40
27
} ) ;
41
28
29
+ // TODO(#5102): Remove this test case, which is for pre-1.8 servers.
42
30
test ( 'when no data is given reset state' , ( ) => {
43
- const prevState = deepFreeze ( [ [ 'stream' ] , [ 'topic' ] ] ) ;
44
- const action = deepFreeze ( {
45
- type : REGISTER_COMPLETE ,
46
- data : { } ,
31
+ const prevState = deepFreeze ( [ eg . makeUserGroup ( ) ] ) ;
32
+ const action = eg . mkActionRegisterComplete ( {
33
+ // Hmm, we should need a Flow suppression here. This property is
34
+ // marked required in InitialData, and this explicit undefined is
35
+ // meant to defy that; see TODO(#5102) above.
36
+ // mkActionRegisterComplete is designed to accept input with this or
37
+ // any property *omitted*… and I think, as a side effect of handling
38
+ // that, Flow mistakenly accepts an explicit undefined here, so it
39
+ // doesn't catch the resulting malformed InitialData.
40
+ realm_user_groups : undefined ,
47
41
} ) ;
48
42
const expectedState = [ ] ;
49
43
@@ -55,18 +49,9 @@ describe('userGroupsReducer', () => {
55
49
56
50
describe ( 'ACCOUNT_SWITCH' , ( ) => {
57
51
test ( 'resets state to initial state' , ( ) => {
58
- const prevState = deepFreeze ( [
59
- {
60
- id : 1 ,
61
- name : 'Some Group' ,
62
- description : 'This is some group' ,
63
- members : [ ] ,
64
- } ,
65
- ] ) ;
52
+ const prevState = deepFreeze ( [ eg . makeUserGroup ( ) ] ) ;
66
53
67
- const action = deepFreeze ( {
68
- type : ACCOUNT_SWITCH ,
69
- } ) ;
54
+ const action = eg . action . account_switch ;
70
55
71
56
const expectedState = [ ] ;
72
57
@@ -79,13 +64,9 @@ describe('userGroupsReducer', () => {
79
64
describe ( 'EVENT_USER_GROUP_ADD' , ( ) => {
80
65
test ( 'adds a user group to the state' , ( ) => {
81
66
const prevState = deepFreeze ( [ ] ) ;
82
- const group = {
83
- id : 1 ,
84
- name : 'Some Group' ,
85
- description : 'This is some group' ,
86
- members : [ 123 ] ,
87
- } ;
67
+ const group = eg . makeUserGroup ( ) ;
88
68
const action = deepFreeze ( {
69
+ id : 1 ,
89
70
type : EVENT_USER_GROUP_ADD ,
90
71
op : 'add' ,
91
72
group,
@@ -103,6 +84,7 @@ describe('userGroupsReducer', () => {
103
84
test ( 'if user group does not exist state does not change' , ( ) => {
104
85
const prevState = deepFreeze ( [ ] ) ;
105
86
const action = deepFreeze ( {
87
+ id : 1 ,
106
88
type : EVENT_USER_GROUP_REMOVE ,
107
89
op : 'remove' ,
108
90
group_id : 1 ,
@@ -115,28 +97,18 @@ describe('userGroupsReducer', () => {
115
97
} ) ;
116
98
117
99
test ( 'adds a user group to the state' , ( ) => {
118
- const prevState = deepFreeze ( [
119
- {
120
- id : 1 ,
121
- name : 'Some group' ,
122
- } ,
123
- {
124
- id : 2 ,
125
- name : 'Another group' ,
126
- } ,
127
- ] ) ;
100
+ const group1 = eg . makeUserGroup ( ) ;
101
+ const group2 = eg . makeUserGroup ( ) ;
102
+
103
+ const prevState = deepFreeze ( [ group1 , group2 ] ) ;
128
104
const action = deepFreeze ( {
105
+ id : 1 ,
129
106
type : EVENT_USER_GROUP_REMOVE ,
130
107
op : 'remove' ,
131
- group_id : 1 ,
108
+ group_id : group1 . id ,
132
109
} ) ;
133
110
134
- const expectedState = [
135
- {
136
- id : 2 ,
137
- name : 'Another group' ,
138
- } ,
139
- ] ;
111
+ const expectedState = [ group2 ] ;
140
112
141
113
const actualState = userGroupsReducer ( prevState , action ) ;
142
114
@@ -148,6 +120,7 @@ describe('userGroupsReducer', () => {
148
120
test ( 'if user group does not exist state does not change' , ( ) => {
149
121
const prevState = deepFreeze ( [ ] ) ;
150
122
const action = deepFreeze ( {
123
+ id : 1 ,
151
124
type : EVENT_USER_GROUP_UPDATE ,
152
125
op : 'update' ,
153
126
group_id : 1 ,
@@ -161,32 +134,18 @@ describe('userGroupsReducer', () => {
161
134
} ) ;
162
135
163
136
test ( 'updates an existing user group with supplied new values' , ( ) => {
164
- const prevState = deepFreeze ( [
165
- {
166
- id : 1 ,
167
- name : 'Some group' ,
168
- } ,
169
- {
170
- id : 2 ,
171
- name : 'Another group' ,
172
- } ,
173
- ] ) ;
137
+ const group1 = eg . makeUserGroup ( ) ;
138
+ const group2 = eg . makeUserGroup ( ) ;
139
+
140
+ const prevState = deepFreeze ( [ group1 , group2 ] ) ;
174
141
const action = deepFreeze ( {
142
+ id : 1 ,
175
143
type : EVENT_USER_GROUP_UPDATE ,
176
144
op : 'update' ,
177
- group_id : 2 ,
145
+ group_id : group2 . id ,
178
146
data : { name : 'New name' } ,
179
147
} ) ;
180
- const expectedState = [
181
- {
182
- id : 1 ,
183
- name : 'Some group' ,
184
- } ,
185
- {
186
- id : 2 ,
187
- name : 'New name' ,
188
- } ,
189
- ] ;
148
+ const expectedState = [ group1 , { ...group2 , name : 'New name' } ] ;
190
149
191
150
const actualState = userGroupsReducer ( prevState , action ) ;
192
151
@@ -198,10 +157,11 @@ describe('userGroupsReducer', () => {
198
157
test ( 'if user group does not exist state does not change' , ( ) => {
199
158
const prevState = deepFreeze ( [ ] ) ;
200
159
const action = deepFreeze ( {
160
+ id : 1 ,
201
161
type : EVENT_USER_GROUP_ADD_MEMBERS ,
202
162
op : 'add_members' ,
203
163
group_id : 1 ,
204
- user_ids : [ 1 , 2 , 3 ] ,
164
+ user_ids : [ eg . makeUser ( ) . user_id , eg . makeUser ( ) . user_id , eg . makeUser ( ) . user_id ] ,
205
165
} ) ;
206
166
const expectedState = [ ] ;
207
167
@@ -211,25 +171,17 @@ describe('userGroupsReducer', () => {
211
171
} ) ;
212
172
213
173
test ( 'updates an existing user group with supplied new members' , ( ) => {
214
- const prevState = deepFreeze ( [
215
- {
216
- id : 1 ,
217
- name : 'Some group' ,
218
- members : [ 1 ] ,
219
- } ,
220
- ] ) ;
174
+ const group = eg . makeUserGroup ( { members : [ eg . selfUser . user_id ] } ) ;
175
+ const prevState = deepFreeze ( [ group ] ) ;
221
176
const action = deepFreeze ( {
177
+ id : 1 ,
222
178
type : EVENT_USER_GROUP_ADD_MEMBERS ,
223
179
op : 'add_members' ,
224
- group_id : 1 ,
225
- user_ids : [ 2 , 3 ] ,
180
+ group_id : group . id ,
181
+ user_ids : [ eg . otherUser . user_id , eg . thirdUser . user_id ] ,
226
182
} ) ;
227
183
const expectedState = [
228
- {
229
- id : 1 ,
230
- name : 'Some group' ,
231
- members : [ 1 , 2 , 3 ] ,
232
- } ,
184
+ { ...group , members : [ eg . selfUser . user_id , eg . otherUser . user_id , eg . thirdUser . user_id ] } ,
233
185
] ;
234
186
235
187
const actualState = userGroupsReducer ( prevState , action ) ;
@@ -242,10 +194,11 @@ describe('userGroupsReducer', () => {
242
194
test ( 'if user group does not exist state does not change' , ( ) => {
243
195
const prevState = deepFreeze ( [ ] ) ;
244
196
const action = deepFreeze ( {
197
+ id : 1 ,
245
198
type : EVENT_USER_GROUP_REMOVE_MEMBERS ,
246
199
op : 'remove_members' ,
247
200
group_id : 1 ,
248
- user_ids : [ 1 ] ,
201
+ user_ids : [ eg . makeUser ( ) . user_id ] ,
249
202
} ) ;
250
203
const expectedState = [ ] ;
251
204
@@ -255,36 +208,27 @@ describe('userGroupsReducer', () => {
255
208
} ) ;
256
209
257
210
test ( 'removes members from an existing user group' , ( ) => {
258
- const prevState = deepFreeze ( [
259
- {
260
- id : 1 ,
261
- name : 'Some group' ,
262
- members : [ 1 , 2 , 3 , 4 ] ,
263
- } ,
264
- {
265
- id : 2 ,
266
- name : 'Another group' ,
267
- members : [ 1 , 2 , 3 , 4 ] ,
268
- } ,
269
- ] ) ;
211
+ const user1 = eg . makeUser ( ) ;
212
+ const user2 = eg . makeUser ( ) ;
213
+ const user3 = eg . makeUser ( ) ;
214
+ const user4 = eg . makeUser ( ) ;
215
+
216
+ const group1 = eg . makeUserGroup ( {
217
+ members : [ user1 . user_id , user2 . user_id , user3 . user_id , user4 . user_id ] ,
218
+ } ) ;
219
+ const group2 = eg . makeUserGroup ( {
220
+ members : [ user1 . user_id , user2 . user_id , user3 . user_id , user4 . user_id ] ,
221
+ } ) ;
222
+
223
+ const prevState = deepFreeze ( [ group1 , group2 ] ) ;
270
224
const action = deepFreeze ( {
225
+ id : 1 ,
271
226
type : EVENT_USER_GROUP_REMOVE_MEMBERS ,
272
227
op : 'remove_members' ,
273
- group_id : 1 ,
274
- user_ids : [ 2 , 3 ] ,
228
+ group_id : group1 . id ,
229
+ user_ids : [ user2 . user_id , user3 . user_id ] ,
275
230
} ) ;
276
- const expectedState = [
277
- {
278
- id : 1 ,
279
- name : 'Some group' ,
280
- members : [ 1 , 4 ] ,
281
- } ,
282
- {
283
- id : 2 ,
284
- name : 'Another group' ,
285
- members : [ 1 , 2 , 3 , 4 ] ,
286
- } ,
287
- ] ;
231
+ const expectedState = [ { ...group1 , members : [ user1 . user_id , user4 . user_id ] } , group2 ] ;
288
232
289
233
const actualState = userGroupsReducer ( prevState , action ) ;
290
234
0 commit comments