1
+ /* @flow strict-local */
2
+
1
3
import deepFreeze from 'deep-freeze' ;
2
4
5
+ import * as eg from '../../__tests__/lib/exampleData' ;
3
6
import topicsReducer from '../topicsReducer' ;
4
- import { ACCOUNT_SWITCH , INIT_TOPICS , EVENT_NEW_MESSAGE } from '../../actionConstants' ;
7
+ import { INIT_TOPICS } from '../../actionConstants' ;
5
8
import { NULL_OBJECT } from '../../nullObjects' ;
6
9
7
- describe ( 'streamsReducer ' , ( ) => {
10
+ describe ( 'topicsReducer ' , ( ) => {
8
11
describe ( 'ACCOUNT_SWITCH' , ( ) => {
9
12
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 ' } ] } ) ;
11
14
12
- const action = deepFreeze ( {
13
- type : ACCOUNT_SWITCH ,
14
- } ) ;
15
+ const action = eg . action . account_switch ;
15
16
16
17
const expectedState = NULL_OBJECT ;
17
18
@@ -23,11 +24,11 @@ describe('streamsReducer', () => {
23
24
24
25
describe ( 'INIT_TOPICS' , ( ) => {
25
26
test ( 'adds new topics mapped to stream id' , ( ) => {
26
- const prevState = NULL_OBJECT ;
27
+ const prevState = eg . plusReduxState . topics ;
27
28
28
29
const action = deepFreeze ( {
29
30
type : INIT_TOPICS ,
30
- streamId : 1 ,
31
+ streamId : eg . stream . stream_id ,
31
32
topics : [
32
33
{
33
34
max_id : 1 ,
@@ -41,7 +42,7 @@ describe('streamsReducer', () => {
41
42
} ) ;
42
43
43
44
const expectedState = {
44
- '1' : [
45
+ [ eg . stream . stream_id ] : [
45
46
{
46
47
max_id : 1 ,
47
48
name : 'topic1' ,
@@ -60,7 +61,7 @@ describe('streamsReducer', () => {
60
61
61
62
test ( 'if topics for stream already exist, replace them' , ( ) => {
62
63
const prevState = deepFreeze ( {
63
- '1' : [
64
+ [ eg . stream . stream_id ] : [
64
65
{
65
66
max_id : 1 ,
66
67
name : 'some topic' ,
@@ -70,7 +71,7 @@ describe('streamsReducer', () => {
70
71
71
72
const action = deepFreeze ( {
72
73
type : INIT_TOPICS ,
73
- streamId : 1 ,
74
+ streamId : eg . stream . stream_id ,
74
75
topics : [
75
76
{
76
77
max_id : 2 ,
@@ -84,7 +85,7 @@ describe('streamsReducer', () => {
84
85
} ) ;
85
86
86
87
const expectedState = {
87
- '1' : [
88
+ [ eg . stream . stream_id ] : [
88
89
{
89
90
max_id : 2 ,
90
91
name : 'topic1' ,
@@ -104,47 +105,37 @@ describe('streamsReducer', () => {
104
105
105
106
describe ( 'EVENT_NEW_MESSAGE' , ( ) => {
106
107
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 ( ) ) ;
117
111
118
112
const actualState = topicsReducer ( prevState , action ) ;
119
113
120
114
expect ( actualState ) . toBe ( prevState ) ;
121
115
} ) ;
122
116
123
117
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
+
124
123
const prevState = {
125
- 123 : [
124
+ [ stream . stream_id ] : [
126
125
{
127
- max_id : 1 ,
128
- name : 'some topic' ,
126
+ max_id : oldMessage . id ,
127
+ name : topic ,
129
128
} ,
130
129
] ,
131
130
} ;
132
131
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 ) ;
142
133
143
134
const expectedState = {
144
- 123 : [
135
+ [ stream . stream_id ] : [
145
136
{
146
- max_id : 234 ,
147
- name : 'some topic' ,
137
+ max_id : newMessage . id ,
138
+ name : topic ,
148
139
} ,
149
140
] ,
150
141
} ;
@@ -155,23 +146,19 @@ describe('streamsReducer', () => {
155
146
} ) ;
156
147
157
148
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 } ) ;
159
152
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 ) ;
169
156
170
157
const expectedState = {
171
- 123 : [
158
+ [ stream . stream_id ] : [
172
159
{
173
- max_id : 2 ,
174
- name : 'some topic' ,
160
+ max_id : message . id ,
161
+ name : topic ,
175
162
} ,
176
163
] ,
177
164
} ;
0 commit comments