Skip to content

Commit 5ad56cd

Browse files
committed
typing tests [nfc]: Tighten up code style a bit
1 parent 418ca19 commit 5ad56cd

File tree

1 file changed

+85
-115
lines changed

1 file changed

+85
-115
lines changed

src/typing/__tests__/typingReducer-test.js

Lines changed: 85 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -34,148 +34,118 @@ describe('typingReducer', () => {
3434

3535
describe('EVENT_TYPING_START', () => {
3636
test('adds sender as currently typing user', () => {
37-
const initialState = NULL_OBJECT;
38-
39-
const action = egTypingAction({
40-
op: 'start',
41-
sender: user1,
42-
recipients: [user1, eg.selfUser],
43-
time: 123456789,
44-
});
45-
46-
const expectedState = {
47-
'1': { time: 123456789, userIds: [user1.user_id] },
48-
};
49-
50-
const newState = typingReducer(initialState, action);
51-
52-
expect(newState).toEqual(expectedState);
37+
const prevState = NULL_OBJECT;
38+
expect(
39+
typingReducer(
40+
prevState,
41+
egTypingAction({
42+
op: 'start',
43+
sender: user1,
44+
recipients: [user1, eg.selfUser],
45+
time: 123456789,
46+
}),
47+
),
48+
).toEqual({ '1': { time: 123456789, userIds: [user1.user_id] } });
5349
});
5450

5551
test('if user is already typing, no change in userIds but update time', () => {
56-
const initialState = deepFreeze({
57-
'1': { time: 123456789, userIds: [user1.user_id] },
58-
});
59-
60-
const action = egTypingAction({
61-
op: 'start',
62-
sender: user1,
63-
recipients: [user1, eg.selfUser],
64-
time: 123456889,
65-
});
66-
67-
const expectedState = {
68-
'1': { time: 123456889, userIds: [user1.user_id] },
69-
};
70-
71-
const newState = typingReducer(initialState, action);
72-
73-
expect(newState).toEqual(expectedState);
52+
const prevState = deepFreeze({ '1': { time: 123456789, userIds: [user1.user_id] } });
53+
expect(
54+
typingReducer(
55+
prevState,
56+
egTypingAction({
57+
op: 'start',
58+
sender: user1,
59+
recipients: [user1, eg.selfUser],
60+
time: 123456889,
61+
}),
62+
),
63+
).toEqual({ '1': { time: 123456889, userIds: [user1.user_id] } });
7464
});
7565

7666
test('if other people are typing in other narrows, add, do not affect them', () => {
77-
const initialState = deepFreeze({
78-
'1': { time: 123489, userIds: [user1.user_id] },
79-
});
80-
81-
const action = egTypingAction({
82-
op: 'start',
83-
sender: user2,
84-
recipients: [user1, user2, eg.selfUser],
85-
time: 123456789,
86-
});
87-
88-
const expectedState = {
67+
const prevState = deepFreeze({ '1': { time: 123489, userIds: [user1.user_id] } });
68+
expect(
69+
typingReducer(
70+
prevState,
71+
egTypingAction({
72+
op: 'start',
73+
sender: user2,
74+
recipients: [user1, user2, eg.selfUser],
75+
time: 123456789,
76+
}),
77+
),
78+
).toEqual({
8979
'1': { time: 123489, userIds: [user1.user_id] },
9080
'1,2': { time: 123456789, userIds: [user2.user_id] },
91-
};
92-
93-
const newState = typingReducer(initialState, action);
94-
95-
expect(newState).toEqual(expectedState);
81+
});
9682
});
9783

9884
test('if another user is typing already, append new one', () => {
99-
const initialState = deepFreeze({
100-
'1,2': { time: 123489, userIds: [user1.user_id] },
101-
});
102-
103-
const action = egTypingAction({
104-
op: 'start',
105-
sender: user2,
106-
recipients: [user1, user2, eg.selfUser],
107-
time: 123456789,
108-
});
109-
110-
const expectedState = {
111-
'1,2': { time: 123456789, userIds: [user1.user_id, user2.user_id] },
112-
};
113-
114-
const newState = typingReducer(initialState, action);
115-
116-
expect(newState).toEqual(expectedState);
85+
const prevState = deepFreeze({ '1,2': { time: 123489, userIds: [user1.user_id] } });
86+
expect(
87+
typingReducer(
88+
prevState,
89+
egTypingAction({
90+
op: 'start',
91+
sender: user2,
92+
recipients: [user1, user2, eg.selfUser],
93+
time: 123456789,
94+
}),
95+
),
96+
).toEqual({ '1,2': { time: 123456789, userIds: [user1.user_id, user2.user_id] } });
11797
});
11898
});
11999

120100
describe('EVENT_TYPING_STOP', () => {
121101
test('if after removing, key is an empty list, key is removed', () => {
122-
const initialState = deepFreeze({
102+
const prevState = deepFreeze({
123103
'1': { time: 123489, userIds: [user1.user_id] },
124104
'3': { time: 123489, userIds: [eg.selfUser.user_id] },
125105
});
126-
127-
const action = egTypingAction({
128-
op: 'stop',
129-
sender: user1,
130-
recipients: [user1, eg.selfUser],
131-
time: 123456789,
132-
});
133-
134-
const expectedState = {
135-
'3': { time: 123489, userIds: [eg.selfUser.user_id] },
136-
};
137-
138-
const newState = typingReducer(initialState, action);
139-
140-
expect(newState).toEqual(expectedState);
106+
expect(
107+
typingReducer(
108+
prevState,
109+
egTypingAction({
110+
op: 'stop',
111+
sender: user1,
112+
recipients: [user1, eg.selfUser],
113+
time: 123456789,
114+
}),
115+
),
116+
).toEqual({ '3': { time: 123489, userIds: [eg.selfUser.user_id] } });
141117
});
142118

143119
test('if two people are typing, just one is removed', () => {
144-
const initialState = deepFreeze({
120+
const prevState = deepFreeze({
145121
'1': { time: 123489, userIds: [user1.user_id, eg.selfUser.user_id] },
146122
});
147-
148-
const action = egTypingAction({
149-
op: 'stop',
150-
sender: user1,
151-
recipients: [user1, eg.selfUser],
152-
time: 123456789,
153-
});
154-
155-
const expectedState = {
156-
'1': { time: 123456789, userIds: [eg.selfUser.user_id] },
157-
};
158-
159-
const newState = typingReducer(initialState, action);
160-
161-
expect(newState).toEqual(expectedState);
123+
expect(
124+
typingReducer(
125+
prevState,
126+
egTypingAction({
127+
op: 'stop',
128+
sender: user1,
129+
recipients: [user1, eg.selfUser],
130+
time: 123456789,
131+
}),
132+
),
133+
).toEqual({ '1': { time: 123456789, userIds: [eg.selfUser.user_id] } });
162134
});
163135

164136
test('if typing state does not exist, no change is made', () => {
165-
const initialState = NULL_OBJECT;
166-
167-
const action = egTypingAction({
168-
op: 'stop',
169-
sender: user1,
170-
recipients: [user1, eg.selfUser],
171-
time: 123456789,
172-
});
173-
174-
const expectedState = {};
175-
176-
const newState = typingReducer(initialState, action);
177-
178-
expect(newState).toEqual(expectedState);
137+
const prevState = NULL_OBJECT;
138+
expect(
139+
typingReducer(
140+
prevState,
141+
egTypingAction({
142+
op: 'stop',
143+
sender: user1,
144+
recipients: [user1, eg.selfUser],
145+
time: 123456789,
146+
}),
147+
),
148+
).toEqual({});
179149
});
180150
});
181151
});

0 commit comments

Comments
 (0)