Skip to content

Commit d259170

Browse files
committed
alert words tests: Start type checking; use example data
And tighten the code style a bit.
1 parent ca8cdd9 commit d259170

File tree

1 file changed

+43
-42
lines changed

1 file changed

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

35
import alertWordsReducer from '../alertWordsReducer';
4-
import { REGISTER_COMPLETE, EVENT_ALERT_WORDS } from '../../actionConstants';
6+
import { EVENT_ALERT_WORDS } from '../../actionConstants';
7+
import * as eg from '../../__tests__/lib/exampleData';
58

69
describe('alertWordsReducer', () => {
710
describe('REGISTER_COMPLETE', () => {
811
test('when `alert_words` data is provided init state with it', () => {
9-
const initialState = deepFreeze([]);
10-
const action = deepFreeze({
11-
type: REGISTER_COMPLETE,
12-
data: {
13-
alert_words: ['word', '@mobile-core', 'alert'],
14-
},
15-
});
16-
const expectedState = ['word', '@mobile-core', 'alert'];
17-
18-
const actualState = alertWordsReducer(initialState, action);
19-
20-
expect(actualState).toEqual(expectedState);
12+
const prevState = deepFreeze([]);
13+
expect(
14+
alertWordsReducer(
15+
prevState,
16+
eg.mkActionRegisterComplete({ alert_words: ['word', '@mobile-core', 'alert'] }),
17+
),
18+
).toEqual(['word', '@mobile-core', 'alert']);
2119
});
2220

2321
// TODO(#5102): Delete; see comment on implementation.
2422
test('when no `alert_words` data is given reset state', () => {
25-
const initialState = deepFreeze(['word']);
26-
const action = deepFreeze({
27-
type: REGISTER_COMPLETE,
28-
data: {},
29-
});
30-
const expectedState = [];
31-
32-
const actualState = alertWordsReducer(initialState, action);
23+
const prevState = deepFreeze(['word']);
24+
const actualState = alertWordsReducer(
25+
prevState,
26+
eg.mkActionRegisterComplete({
27+
// Hmm, we should need a Flow suppression here. This property is
28+
// marked required in InitialData, and this explicit undefined is
29+
// meant to defy that; see TODO(#5102) above.
30+
// mkActionRegisterComplete is designed to accept input with this or
31+
// any property *omitted*… and I think, as a side effect of handling
32+
// that, Flow mistakenly accepts an explicit undefined here, so it
33+
// doesn't catch the resulting malformed InitialData.
34+
alert_words: undefined,
35+
}),
36+
);
3337

34-
expect(actualState).toEqual(expectedState);
38+
expect(actualState).toEqual([]);
3539
});
3640
});
3741

3842
describe('EVENT_ALERT_WORDS', () => {
3943
test('on first call adds new data', () => {
40-
const initialState = deepFreeze([]);
41-
const action = deepFreeze({
42-
type: EVENT_ALERT_WORDS,
43-
alert_words: ['word', '@mobile-core', 'alert'],
44-
});
45-
const expectedState = ['word', '@mobile-core', 'alert'];
46-
47-
const actualState = alertWordsReducer(initialState, action);
48-
49-
expect(actualState).toEqual(expectedState);
44+
const prevState = deepFreeze([]);
45+
expect(
46+
alertWordsReducer(
47+
prevState,
48+
deepFreeze({ type: EVENT_ALERT_WORDS, alert_words: ['word', '@mobile-core', 'alert'] }),
49+
),
50+
).toEqual(['word', '@mobile-core', 'alert']);
5051
});
5152

5253
test('subsequent calls replace existing data', () => {
53-
const initialState = deepFreeze(['word', '@mobile-core', 'alert']);
54-
const action = deepFreeze({
55-
type: EVENT_ALERT_WORDS,
56-
alert_words: ['word', '@mobile-core', 'new alert'],
57-
});
58-
const expectedState = ['word', '@mobile-core', 'new alert'];
59-
60-
const actualState = alertWordsReducer(initialState, action);
61-
62-
expect(actualState).toEqual(expectedState);
54+
const prevState = deepFreeze(['word', '@mobile-core', 'alert']);
55+
expect(
56+
alertWordsReducer(
57+
prevState,
58+
deepFreeze({
59+
type: EVENT_ALERT_WORDS,
60+
alert_words: ['word', '@mobile-core', 'new alert'],
61+
}),
62+
),
63+
).toEqual(['word', '@mobile-core', 'new alert']);
6364
});
6465
});
6566
});

0 commit comments

Comments
 (0)