1
+ /* @flow strict-local */
2
+
1
3
import deepFreeze from 'deep-freeze' ;
2
4
3
- import {
4
- REGISTER_COMPLETE ,
5
- PRESENCE_RESPONSE ,
6
- EVENT_PRESENCE ,
7
- ACCOUNT_SWITCH ,
8
- } from '../../actionConstants' ;
5
+ import * as eg from '../../__tests__/lib/exampleData' ;
6
+ import { PRESENCE_RESPONSE , EVENT_PRESENCE } from '../../actionConstants' ;
9
7
import presenceReducer from '../presenceReducer' ;
10
8
11
9
const currentTimestamp = Date . now ( ) / 1000 ;
@@ -19,13 +17,8 @@ describe('presenceReducer', () => {
19
17
website : { client : 'website' , status : 'active' , timestamp : 123 } ,
20
18
} ,
21
19
} ;
22
- const prevState = deepFreeze ( { } ) ;
23
- const action = deepFreeze ( {
24
- type : REGISTER_COMPLETE ,
25
- data : {
26
- presences : presenceData ,
27
- } ,
28
- } ) ;
20
+ const prevState = deepFreeze ( eg . baseReduxState . presence ) ;
21
+ const action = eg . mkActionRegisterComplete ( { presences : presenceData } ) ;
29
22
30
23
const actualState = presenceReducer ( prevState , action ) ;
31
24
@@ -40,9 +33,15 @@ describe('presenceReducer', () => {
40
33
website : { client : 'website' , status : 'active' , timestamp : 123 } ,
41
34
} ,
42
35
} ) ;
43
- const action = deepFreeze ( {
44
- type : REGISTER_COMPLETE ,
45
- data : { } ,
36
+ const action = eg . mkActionRegisterComplete ( {
37
+ // Hmm, we should need a Flow suppression here. This property is
38
+ // marked required in InitialData, and this explicit undefined is
39
+ // meant to defy that; see TODO(#5102) above.
40
+ // mkActionRegisterComplete is designed to accept input with this or
41
+ // any property *omitted*… and I think, as a side effect of handling
42
+ // that, Flow mistakenly accepts an explicit undefined here, so it
43
+ // doesn't catch the resulting malformed InitialData.
44
+ presences : undefined ,
46
45
} ) ;
47
46
const expectedState = { } ;
48
47
@@ -56,8 +55,8 @@ describe('presenceReducer', () => {
56
55
test ( 'merges a single user in presence response' , ( ) => {
57
56
const presence = {
58
57
59
- aggregated : { status : 'active' , timestamp : 123 } ,
60
- website : { status : 'active' , timestamp : 123 } ,
58
+ aggregated : { status : 'active' , timestamp : 123 , client : 'website' } ,
59
+ website : { status : 'active' , timestamp : 123 , client : 'website' } ,
61
60
} ,
62
61
} ;
63
62
const action = deepFreeze ( {
@@ -67,15 +66,13 @@ describe('presenceReducer', () => {
67
66
} ) ;
68
67
69
68
const prevState = deepFreeze ( {
70
-
71
- } ) ;
72
-
73
- const expectedState = {
74
69
75
- aggregated : { status : 'active' , timestamp : 123 } ,
76
- website : { status : 'active' , timestamp : 123 } ,
70
+ aggregated : { status : 'active' , timestamp : 8 , client : 'zulipMobile' } ,
71
+ zulipMobile : { status : 'active' , timestamp : 8 , client : 'zulipMobile' } ,
77
72
} ,
78
- } ;
73
+ } ) ;
74
+
75
+ const expectedState = { ...prevState , ...presence } ;
79
76
80
77
const newState = presenceReducer ( prevState , action ) ;
81
78
@@ -84,8 +81,14 @@ describe('presenceReducer', () => {
84
81
85
82
test ( 'merges multiple users in presence response' , ( ) => {
86
83
const prevState = deepFreeze ( {
87
-
88
-
84
+
85
+ aggregated : { status : 'active' , timestamp : 8 , client : 'zulipMobile' } ,
86
+ zulipMobile : { status : 'active' , timestamp : 8 , client : 'zulipMobile' } ,
87
+ } ,
88
+
89
+ aggregated : { status : 'active' , timestamp : 8 , client : 'zulipMobile' } ,
90
+ zulipMobile : { status : 'active' , timestamp : 8 , client : 'zulipMobile' } ,
91
+ } ,
89
92
} ) ;
90
93
91
94
const presence = {
@@ -94,11 +97,8 @@ describe('presenceReducer', () => {
94
97
website : { client : 'website' , status : 'active' , timestamp : 123 } ,
95
98
} ,
96
99
97
- website : {
98
- status : 'active' ,
99
- timestamp : 345 ,
100
- client : 'website' ,
101
- } ,
100
+ aggregated : { status : 'active' , timestamp : 345 , client : 'website' } ,
101
+ website : { status : 'active' , timestamp : 345 , client : 'website' } ,
102
102
} ,
103
103
} ;
104
104
const action = deepFreeze ( {
@@ -107,20 +107,7 @@ describe('presenceReducer', () => {
107
107
serverTimestamp : 12345 ,
108
108
} ) ;
109
109
110
- const expectedState = {
111
-
112
- aggregated : { client : 'website' , status : 'active' , timestamp : 123 } ,
113
- website : { client : 'website' , status : 'active' , timestamp : 123 } ,
114
- } ,
115
-
116
- website : {
117
- status : 'active' ,
118
- timestamp : 345 ,
119
- client : 'website' ,
120
- } ,
121
- } ,
122
-
123
- } ;
110
+ const expectedState = { ...prevState , ...presence } ;
124
111
125
112
const newState = presenceReducer ( prevState , action ) ;
126
113
@@ -138,13 +125,15 @@ describe('presenceReducer', () => {
138
125
timestamp : currentTimestamp - 20 ,
139
126
} ,
140
127
website : {
128
+ client : 'website' ,
141
129
status : 'idle' ,
142
130
timestamp : currentTimestamp - 20 ,
143
131
} ,
144
132
} ,
145
133
} ) ;
146
134
147
135
const action = deepFreeze ( {
136
+ id : 1 ,
148
137
type : EVENT_PRESENCE ,
149
138
150
139
server_timestamp : 200 ,
@@ -165,6 +154,7 @@ describe('presenceReducer', () => {
165
154
timestamp : currentTimestamp - 10 ,
166
155
} ,
167
156
website : {
157
+ client : 'website' ,
168
158
status : 'idle' ,
169
159
timestamp : currentTimestamp - 20 ,
170
160
} ,
@@ -184,18 +174,15 @@ describe('presenceReducer', () => {
184
174
185
175
describe ( 'ACCOUNT_SWITCH' , ( ) => {
186
176
test ( 'resets state to initial state' , ( ) => {
187
- const prevState = deepFreeze ( [
188
- {
189
- full_name : 'Some Guy' ,
190
-
191
- status : 'offline' ,
177
+ const prevState = deepFreeze ( {
178
+
179
+ aggregated : { status : 'active' , timestamp : 8 , client : 'zulipMobile' } ,
180
+ zulipMobile : { status : 'active' , timestamp : 8 , client : 'zulipMobile' } ,
192
181
} ,
193
- ] ) ;
194
-
195
- const action = deepFreeze ( {
196
- type : ACCOUNT_SWITCH ,
197
182
} ) ;
198
183
184
+ const action = eg . action . account_switch ;
185
+
199
186
const expectedState = { } ;
200
187
201
188
const actualState = presenceReducer ( prevState , action ) ;
0 commit comments