Skip to content

Commit 5b9d51d

Browse files
committed
presence tests: Start type-checking; use example data
1 parent 522f49b commit 5b9d51d

File tree

1 file changed

+42
-55
lines changed

1 file changed

+42
-55
lines changed

src/presence/__tests__/presenceReducer-test.js

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
/* @flow strict-local */
2+
13
import deepFreeze from 'deep-freeze';
24

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';
97
import presenceReducer from '../presenceReducer';
108

119
const currentTimestamp = Date.now() / 1000;
@@ -19,13 +17,8 @@ describe('presenceReducer', () => {
1917
website: { client: 'website', status: 'active', timestamp: 123 },
2018
},
2119
};
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 });
2922

3023
const actualState = presenceReducer(prevState, action);
3124

@@ -40,9 +33,15 @@ describe('presenceReducer', () => {
4033
website: { client: 'website', status: 'active', timestamp: 123 },
4134
},
4235
});
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,
4645
});
4746
const expectedState = {};
4847

@@ -56,8 +55,8 @@ describe('presenceReducer', () => {
5655
test('merges a single user in presence response', () => {
5756
const presence = {
5857
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' },
6160
},
6261
};
6362
const action = deepFreeze({
@@ -67,15 +66,13 @@ describe('presenceReducer', () => {
6766
});
6867

6968
const prevState = deepFreeze({
70-
71-
});
72-
73-
const expectedState = {
7469
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' },
7772
},
78-
};
73+
});
74+
75+
const expectedState = { ...prevState, ...presence };
7976

8077
const newState = presenceReducer(prevState, action);
8178

@@ -84,8 +81,14 @@ describe('presenceReducer', () => {
8481

8582
test('merges multiple users in presence response', () => {
8683
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+
},
8992
});
9093

9194
const presence = {
@@ -94,11 +97,8 @@ describe('presenceReducer', () => {
9497
website: { client: 'website', status: 'active', timestamp: 123 },
9598
},
9699
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' },
102102
},
103103
};
104104
const action = deepFreeze({
@@ -107,20 +107,7 @@ describe('presenceReducer', () => {
107107
serverTimestamp: 12345,
108108
});
109109

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 };
124111

125112
const newState = presenceReducer(prevState, action);
126113

@@ -138,13 +125,15 @@ describe('presenceReducer', () => {
138125
timestamp: currentTimestamp - 20,
139126
},
140127
website: {
128+
client: 'website',
141129
status: 'idle',
142130
timestamp: currentTimestamp - 20,
143131
},
144132
},
145133
});
146134

147135
const action = deepFreeze({
136+
id: 1,
148137
type: EVENT_PRESENCE,
149138
150139
server_timestamp: 200,
@@ -165,6 +154,7 @@ describe('presenceReducer', () => {
165154
timestamp: currentTimestamp - 10,
166155
},
167156
website: {
157+
client: 'website',
168158
status: 'idle',
169159
timestamp: currentTimestamp - 20,
170160
},
@@ -184,18 +174,15 @@ describe('presenceReducer', () => {
184174

185175
describe('ACCOUNT_SWITCH', () => {
186176
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' },
192181
},
193-
]);
194-
195-
const action = deepFreeze({
196-
type: ACCOUNT_SWITCH,
197182
});
198183

184+
const action = eg.action.account_switch;
185+
199186
const expectedState = {};
200187

201188
const actualState = presenceReducer(prevState, action);

0 commit comments

Comments
 (0)