Skip to content

Commit 2938916

Browse files
chrisbobbegnprice
authored andcommitted
topics tests: Start type-checking; use example data
1 parent 7640aa0 commit 2938916

File tree

1 file changed

+37
-50
lines changed

1 file changed

+37
-50
lines changed

src/topics/__tests__/topicsReducer-test.js

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
/* @flow strict-local */
2+
13
import deepFreeze from 'deep-freeze';
24

5+
import * as eg from '../../__tests__/lib/exampleData';
36
import topicsReducer from '../topicsReducer';
4-
import { ACCOUNT_SWITCH, INIT_TOPICS, EVENT_NEW_MESSAGE } from '../../actionConstants';
7+
import { INIT_TOPICS } from '../../actionConstants';
58
import { NULL_OBJECT } from '../../nullObjects';
69

7-
describe('streamsReducer', () => {
10+
describe('topicsReducer', () => {
811
describe('ACCOUNT_SWITCH', () => {
912
test('resets state to initial state', () => {
10-
const prevState = deepFreeze([{ max_id: 1, name: 'some_topic' }]);
13+
const prevState = deepFreeze({ [eg.stream.stream_id]: [{ max_id: 1, name: 'some topic' }] });
1114

12-
const action = deepFreeze({
13-
type: ACCOUNT_SWITCH,
14-
});
15+
const action = eg.action.account_switch;
1516

1617
const expectedState = NULL_OBJECT;
1718

@@ -23,11 +24,11 @@ describe('streamsReducer', () => {
2324

2425
describe('INIT_TOPICS', () => {
2526
test('adds new topics mapped to stream id', () => {
26-
const prevState = NULL_OBJECT;
27+
const prevState = eg.plusReduxState.topics;
2728

2829
const action = deepFreeze({
2930
type: INIT_TOPICS,
30-
streamId: 1,
31+
streamId: eg.stream.stream_id,
3132
topics: [
3233
{
3334
max_id: 1,
@@ -41,7 +42,7 @@ describe('streamsReducer', () => {
4142
});
4243

4344
const expectedState = {
44-
'1': [
45+
[eg.stream.stream_id]: [
4546
{
4647
max_id: 1,
4748
name: 'topic1',
@@ -60,7 +61,7 @@ describe('streamsReducer', () => {
6061

6162
test('if topics for stream already exist, replace them', () => {
6263
const prevState = deepFreeze({
63-
'1': [
64+
[eg.stream.stream_id]: [
6465
{
6566
max_id: 1,
6667
name: 'some topic',
@@ -70,7 +71,7 @@ describe('streamsReducer', () => {
7071

7172
const action = deepFreeze({
7273
type: INIT_TOPICS,
73-
streamId: 1,
74+
streamId: eg.stream.stream_id,
7475
topics: [
7576
{
7677
max_id: 2,
@@ -84,7 +85,7 @@ describe('streamsReducer', () => {
8485
});
8586

8687
const expectedState = {
87-
'1': [
88+
[eg.stream.stream_id]: [
8889
{
8990
max_id: 2,
9091
name: 'topic1',
@@ -104,47 +105,37 @@ describe('streamsReducer', () => {
104105

105106
describe('EVENT_NEW_MESSAGE', () => {
106107
test('if message is not in stream do not change state', () => {
107-
const prevState = NULL_OBJECT;
108-
109-
const action = {
110-
type: EVENT_NEW_MESSAGE,
111-
message: {
112-
id: 4,
113-
type: 'private',
114-
sender_id: 1,
115-
},
116-
};
108+
const prevState = eg.plusReduxState.topics;
109+
110+
const action = eg.mkActionEventNewMessage(eg.pmMessage());
117111

118112
const actualState = topicsReducer(prevState, action);
119113

120114
expect(actualState).toBe(prevState);
121115
});
122116

123117
test('if stream message and topic exists update with latest message id', () => {
118+
const stream = eg.stream;
119+
const topic = 'some topic';
120+
const oldMessage = eg.streamMessage({ id: 1, stream, subject: topic });
121+
const newMessage = eg.streamMessage({ id: 2, stream, subject: topic });
122+
124123
const prevState = {
125-
123: [
124+
[stream.stream_id]: [
126125
{
127-
max_id: 1,
128-
name: 'some topic',
126+
max_id: oldMessage.id,
127+
name: topic,
129128
},
130129
],
131130
};
132131

133-
const action = deepFreeze({
134-
type: EVENT_NEW_MESSAGE,
135-
message: {
136-
id: 234,
137-
type: 'stream',
138-
stream_id: 123,
139-
subject: 'some topic',
140-
},
141-
});
132+
const action = eg.mkActionEventNewMessage(newMessage);
142133

143134
const expectedState = {
144-
123: [
135+
[stream.stream_id]: [
145136
{
146-
max_id: 234,
147-
name: 'some topic',
137+
max_id: newMessage.id,
138+
name: topic,
148139
},
149140
],
150141
};
@@ -155,23 +146,19 @@ describe('streamsReducer', () => {
155146
});
156147

157148
test('if stream message and topic does not exist, add it', () => {
158-
const prevState = NULL_OBJECT;
149+
const stream = eg.stream;
150+
const topic = 'some topic';
151+
const message = eg.streamMessage({ stream, subject: topic });
159152

160-
const action = deepFreeze({
161-
type: EVENT_NEW_MESSAGE,
162-
message: {
163-
id: 2,
164-
type: 'stream',
165-
stream_id: 123,
166-
subject: 'some topic',
167-
},
168-
});
153+
const prevState = eg.plusReduxState.topics;
154+
155+
const action = eg.mkActionEventNewMessage(message);
169156

170157
const expectedState = {
171-
123: [
158+
[stream.stream_id]: [
172159
{
173-
max_id: 2,
174-
name: 'some topic',
160+
max_id: message.id,
161+
name: topic,
175162
},
176163
],
177164
};

0 commit comments

Comments
 (0)