Skip to content

Commit 692a881

Browse files
committed
fetching tests [nfc]: Tighten code style a bit
1 parent 06d2f84 commit 692a881

File tree

1 file changed

+49
-101
lines changed

1 file changed

+49
-101
lines changed

src/chat/__tests__/fetchingReducer-test.js

Lines changed: 49 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -25,132 +25,80 @@ describe('fetchingReducer', () => {
2525

2626
describe('MESSAGE_FETCH_START', () => {
2727
test('if messages are fetched before or after the corresponding flag is set', () => {
28-
const prevState = deepFreeze({
29-
[HOME_NARROW_STR]: { older: false, newer: false },
30-
});
31-
32-
const action = deepFreeze({
33-
type: MESSAGE_FETCH_START,
34-
narrow: HOME_NARROW,
35-
numBefore: 10,
36-
numAfter: 10,
37-
});
38-
39-
const expectedState = {
40-
[HOME_NARROW_STR]: { older: true, newer: true },
41-
};
42-
43-
const newState = fetchingReducer(prevState, action);
44-
45-
expect(newState).toEqual(expectedState);
28+
const prevState = deepFreeze({ [HOME_NARROW_STR]: { older: false, newer: false } });
29+
expect(
30+
fetchingReducer(
31+
prevState,
32+
deepFreeze({
33+
type: MESSAGE_FETCH_START,
34+
narrow: HOME_NARROW,
35+
numBefore: 10,
36+
numAfter: 10,
37+
}),
38+
),
39+
).toEqual({ [HOME_NARROW_STR]: { older: true, newer: true } });
4640
});
4741

4842
test('if key for narrow does not exist, it is created and corresponding flags are set', () => {
49-
const prevState = deepFreeze({
50-
[HOME_NARROW_STR]: { older: false, newer: false },
51-
});
52-
5343
const narrow = streamNarrow(eg.stream.stream_id);
54-
const action = deepFreeze({
55-
type: MESSAGE_FETCH_START,
56-
narrow,
57-
numBefore: 10,
58-
numAfter: 0,
59-
});
6044

61-
const expectedState = {
45+
const prevState = deepFreeze({ [HOME_NARROW_STR]: { older: false, newer: false } });
46+
expect(
47+
fetchingReducer(
48+
prevState,
49+
deepFreeze({ type: MESSAGE_FETCH_START, narrow, numBefore: 10, numAfter: 0 }),
50+
),
51+
).toEqual({
6252
[HOME_NARROW_STR]: { older: false, newer: false },
6353
[keyFromNarrow(narrow)]: { older: true, newer: false },
64-
};
65-
66-
const newState = fetchingReducer(prevState, action);
67-
68-
expect(newState).toEqual(expectedState);
54+
});
6955
});
7056

7157
test('if fetching for a search narrow, ignore', () => {
72-
const prevState = deepFreeze({
73-
[HOME_NARROW_STR]: {
74-
older: false,
75-
newer: false,
76-
},
77-
});
78-
79-
const action = deepFreeze({
80-
...eg.action.message_fetch_start,
81-
narrow: SEARCH_NARROW('some query'),
82-
});
83-
84-
const newState = fetchingReducer(prevState, action);
85-
86-
expect(newState).toEqual(prevState);
58+
const prevState = deepFreeze({ [HOME_NARROW_STR]: { older: false, newer: false } });
59+
expect(
60+
fetchingReducer(
61+
deepFreeze(prevState),
62+
deepFreeze({ ...eg.action.message_fetch_start, narrow: SEARCH_NARROW('some query') }),
63+
),
64+
).toEqual(prevState);
8765
});
8866
});
8967

9068
describe('MESSAGE_FETCH_ERROR', () => {
9169
test('reverses the effect of MESSAGE_FETCH_START as much as possible', () => {
9270
// As of the addition of this test, that means setting
9371
// DEFAULT_FETCHING as the key.
94-
const state0 = deepFreeze({});
95-
96-
const messageFetchStartAction = deepFreeze({
97-
...eg.action.message_fetch_start,
98-
narrow: HOME_NARROW,
99-
});
100-
101-
const state1 = fetchingReducer(state0, messageFetchStartAction);
102-
103-
const messageFetchErrorAction = deepFreeze({
104-
type: MESSAGE_FETCH_ERROR,
105-
narrow: HOME_NARROW,
106-
error: new Error(),
107-
});
108-
109-
const finalState = fetchingReducer(state1, messageFetchErrorAction);
110-
111-
const expectedState = {
112-
[HOME_NARROW_STR]: DEFAULT_FETCHING,
113-
};
114-
115-
expect(finalState).toEqual(expectedState);
72+
expect(
73+
[
74+
deepFreeze({ ...eg.action.message_fetch_start, narrow: HOME_NARROW }),
75+
deepFreeze({ type: MESSAGE_FETCH_ERROR, narrow: HOME_NARROW, error: new Error() }),
76+
].reduce(fetchingReducer, eg.baseReduxState.fetching),
77+
).toEqual({ [HOME_NARROW_STR]: DEFAULT_FETCHING });
11678
});
11779
});
11880

11981
describe('MESSAGE_FETCH_COMPLETE', () => {
12082
test('sets corresponding fetching flags to false, if messages are received before or after', () => {
121-
const prevState = deepFreeze({
122-
[HOME_NARROW_STR]: { older: true, newer: true },
123-
});
124-
125-
const action = {
126-
...eg.action.message_fetch_complete,
127-
narrow: HOME_NARROW,
128-
numBefore: 10,
129-
numAfter: 0,
130-
};
131-
132-
const expectedState = {
133-
[HOME_NARROW_STR]: { older: false, newer: true },
134-
};
135-
136-
const newState = fetchingReducer(prevState, action);
137-
138-
expect(newState).toEqual(expectedState);
83+
const prevState = deepFreeze({ [HOME_NARROW_STR]: { older: true, newer: true } });
84+
expect(
85+
fetchingReducer(prevState, {
86+
...eg.action.message_fetch_complete,
87+
narrow: HOME_NARROW,
88+
numBefore: 10,
89+
numAfter: 0,
90+
}),
91+
).toEqual({ [HOME_NARROW_STR]: { older: false, newer: true } });
13992
});
14093
});
14194

14295
test('if fetched messages are from a search narrow, ignore them', () => {
143-
const prevState = deepFreeze({
144-
[HOME_NARROW_STR]: { older: true, newer: true },
145-
});
146-
147-
const action = deepFreeze({
148-
...eg.action.message_fetch_complete,
149-
narrow: SEARCH_NARROW('some query'),
150-
});
151-
152-
const newState = fetchingReducer(prevState, action);
153-
154-
expect(newState).toEqual(prevState);
96+
const prevState = deepFreeze({ [HOME_NARROW_STR]: { older: true, newer: true } });
97+
expect(
98+
fetchingReducer(
99+
prevState,
100+
deepFreeze({ ...eg.action.message_fetch_complete, narrow: SEARCH_NARROW('some query') }),
101+
),
102+
).toEqual(prevState);
155103
});
156104
});

0 commit comments

Comments
 (0)