Skip to content

Commit aab9f85

Browse files
committed
caughtUp tests [nfc]: Tighten code style a bit
1 parent 2f20dfe commit aab9f85

File tree

1 file changed

+46
-100
lines changed

1 file changed

+46
-100
lines changed

src/caughtup/__tests__/caughtUpReducer-test.js

Lines changed: 46 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,23 @@ import { objectFromEntries } from '../../jsBackport';
1818
describe('caughtUpReducer', () => {
1919
describe('MESSAGE_FETCH_START', () => {
2020
test('when fetch starts caught up does not change', () => {
21-
const initialState = deepFreeze({
22-
[HOME_NARROW_STR]: {
23-
older: true,
24-
newer: true,
25-
},
26-
});
27-
28-
const action = deepFreeze({
29-
...eg.action.message_fetch_start,
30-
narrow: HOME_NARROW,
31-
});
32-
33-
const newState = caughtUpReducer(initialState, action);
34-
35-
expect(newState).toBe(initialState);
21+
const prevState = deepFreeze({ [HOME_NARROW_STR]: { older: true, newer: true } });
22+
expect(
23+
caughtUpReducer(
24+
prevState,
25+
deepFreeze({ ...eg.action.message_fetch_start, narrow: HOME_NARROW }),
26+
),
27+
).toBe(prevState);
3628
});
3729

3830
test('if fetching for a search narrow, ignore', () => {
39-
const initialState = deepFreeze({
40-
[HOME_NARROW_STR]: {
41-
older: false,
42-
newer: false,
43-
},
44-
});
45-
46-
const action = deepFreeze({
47-
...eg.action.message_fetch_start,
48-
narrow: SEARCH_NARROW('some query'),
49-
});
50-
51-
const newState = caughtUpReducer(initialState, action);
52-
53-
expect(newState).toEqual(initialState);
31+
const prevState = deepFreeze({ [HOME_NARROW_STR]: { older: false, newer: false } });
32+
expect(
33+
caughtUpReducer(
34+
prevState,
35+
deepFreeze({ ...eg.action.message_fetch_start, narrow: SEARCH_NARROW('some query') }),
36+
),
37+
).toEqual(prevState);
5438
});
5539
});
5640

@@ -67,88 +51,50 @@ describe('caughtUpReducer', () => {
6751
// Include some other narrow to test that the reducer doesn't go mess
6852
// something up there.
6953
const initialState = deepFreeze({ [keyFromNarrow(narrow1)]: { older: true, newer: true } });
70-
71-
const messageFetchStartAction = deepFreeze({
72-
...eg.action.message_fetch_start,
73-
narrow: narrow2,
74-
});
75-
76-
const state1 = caughtUpReducer(initialState, messageFetchStartAction);
77-
78-
const messageFetchErrorAction = deepFreeze({
79-
type: MESSAGE_FETCH_ERROR,
80-
narrow: narrow2,
81-
error: new Error(),
82-
});
83-
84-
const finalState = caughtUpReducer(state1, messageFetchErrorAction);
85-
86-
expect(finalState).toEqual(initialState);
54+
expect(
55+
[
56+
deepFreeze({ ...eg.action.message_fetch_start, narrow: narrow2 }),
57+
deepFreeze({ type: MESSAGE_FETCH_ERROR, narrow: narrow2, error: new Error() }),
58+
].reduce(caughtUpReducer, initialState),
59+
).toEqual(initialState);
8760
});
8861
});
8962

9063
describe('MESSAGE_FETCH_COMPLETE', () => {
9164
test('apply `foundNewest` and `foundOldest` when true', () => {
92-
const initialState = deepFreeze({});
93-
94-
const action = deepFreeze({
95-
...eg.action.message_fetch_complete,
96-
foundNewest: true,
97-
foundOldest: true,
98-
});
99-
100-
const expectedState = {
101-
[HOME_NARROW_STR]: {
102-
older: true,
103-
newer: true,
104-
},
105-
};
106-
107-
const newState = caughtUpReducer(initialState, action);
108-
109-
expect(newState).toEqual(expectedState);
65+
const prevState = deepFreeze({});
66+
expect(
67+
caughtUpReducer(
68+
prevState,
69+
deepFreeze({ ...eg.action.message_fetch_complete, foundNewest: true, foundOldest: true }),
70+
),
71+
).toEqual({ [HOME_NARROW_STR]: { older: true, newer: true } });
11072
});
11173

11274
test('if fetched messages are from a search narrow, ignore them', () => {
113-
const initialState = deepFreeze({});
114-
115-
const action = deepFreeze({
116-
...eg.action.message_fetch_complete,
117-
narrow: SEARCH_NARROW('some query'),
118-
foundOldest: true,
119-
foundNewest: true,
120-
});
121-
122-
const newState = caughtUpReducer(initialState, action);
123-
124-
expect(newState).toEqual(initialState);
75+
const prevState = deepFreeze({});
76+
expect(
77+
caughtUpReducer(
78+
prevState,
79+
deepFreeze({
80+
...eg.action.message_fetch_complete,
81+
narrow: SEARCH_NARROW('some query'),
82+
foundOldest: true,
83+
foundNewest: true,
84+
}),
85+
),
86+
).toEqual(prevState);
12587
});
12688
});
12789

12890
test('new false results do not reset previous true state', () => {
129-
const initialState = deepFreeze({
130-
[HOME_NARROW_STR]: {
131-
older: true,
132-
newer: true,
133-
},
134-
});
135-
136-
const action = deepFreeze({
137-
...eg.action.message_fetch_complete,
138-
foundOldest: false,
139-
foundNewest: false,
140-
});
141-
142-
const expectedState = {
143-
[HOME_NARROW_STR]: {
144-
older: true,
145-
newer: true,
146-
},
147-
};
148-
149-
const newState = caughtUpReducer(initialState, action);
150-
151-
expect(newState).toEqual(expectedState);
91+
const prevState = deepFreeze({ [HOME_NARROW_STR]: { older: true, newer: true } });
92+
expect(
93+
caughtUpReducer(
94+
prevState,
95+
deepFreeze({ ...eg.action.message_fetch_complete, foundOldest: false, foundNewest: false }),
96+
),
97+
).toEqual({ [HOME_NARROW_STR]: { older: true, newer: true } });
15298
});
15399

154100
describe('EVENT_UPDATE_MESSAGE', () => {

0 commit comments

Comments
 (0)