Skip to content

Commit 10ba802

Browse files
committed
Add or Remove subscriptions on subscription events. Add tests
1 parent 1ffa2c7 commit 10ba802

File tree

4 files changed

+357
-191
lines changed

4 files changed

+357
-191
lines changed

src/events/eventActions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ const processEvent = (dispatch, event, getState) => {
4444
if (event.op === 'add') {
4545
dispatch({
4646
type: EVENT_SUBSCRIPTION_ADD,
47-
event,
47+
subscriptions: event.subscriptions,
4848
});
4949
} else if (event.op === 'remove') {
5050
dispatch({
5151
type: EVENT_SUBSCRIPTION_REMOVE,
52-
event,
52+
subscriptions: event.subscriptions,
5353
});
5454
}
5555
break;
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import {
2+
EVENT_SUBSCRIPTION_ADD,
3+
EVENT_SUBSCRIPTION_REMOVE,
4+
} from '../../constants';
5+
import subscriptionsReducers from '../subscriptionsReducers';
6+
7+
describe('subscriptionsReducers', () => {
8+
test('on unrecognized action, returns input state unchanged', () => {
9+
const prevState = { hello: 'world' };
10+
const newState = subscriptionsReducers(prevState, {});
11+
expect(newState).toEqual(prevState);
12+
});
13+
14+
describe('EVENT_SUBSCRIPTION_ADD', () => {
15+
test('if new subscriptions do not exist in state, add them', () => {
16+
const prevState = [];
17+
const action = {
18+
type: EVENT_SUBSCRIPTION_ADD,
19+
subscriptions: [
20+
{
21+
name: 'some stream',
22+
stream_id: 1,
23+
},
24+
{
25+
name: 'some other stream',
26+
stream_id: 2,
27+
}
28+
],
29+
};
30+
const expectedState = [
31+
{
32+
name: 'some stream',
33+
stream_id: 1,
34+
},
35+
{
36+
name: 'some other stream',
37+
stream_id: 2,
38+
}
39+
];
40+
41+
const newState = subscriptionsReducers(prevState, action);
42+
43+
expect(newState).toEqual(expectedState);
44+
});
45+
46+
test('if some of subscriptions already exist, do not add them', () => {
47+
const prevState = [
48+
{
49+
color: 'red',
50+
stream_id: 1,
51+
name: 'some stream'
52+
}
53+
];
54+
const action = {
55+
type: EVENT_SUBSCRIPTION_ADD,
56+
subscriptions: [
57+
{
58+
name: 'some stream',
59+
stream_id: 1,
60+
},
61+
{
62+
name: 'some other stream',
63+
stream_id: 2,
64+
}
65+
],
66+
};
67+
const expectedState = [
68+
{
69+
color: 'red',
70+
name: 'some stream',
71+
stream_id: 1,
72+
},
73+
{
74+
name: 'some other stream',
75+
stream_id: 2,
76+
}
77+
];
78+
79+
const newState = subscriptionsReducers(prevState, action);
80+
81+
expect(newState).toEqual(expectedState);
82+
});
83+
});
84+
85+
describe('EVENT_SUBSCRIPTION_REMOVE', () => {
86+
test('removes subscriptions from state', () => {
87+
const prevState = [
88+
{
89+
color: 'red',
90+
stream_id: 1,
91+
name: 'some stream'
92+
},
93+
{
94+
color: 'green',
95+
stream_id: 2,
96+
name: 'other stream'
97+
},
98+
{
99+
color: 'blue',
100+
stream_id: 3,
101+
name: 'third stream'
102+
},
103+
];
104+
const action = {
105+
type: EVENT_SUBSCRIPTION_REMOVE,
106+
subscriptions: [
107+
{
108+
name: 'some stream',
109+
stream_id: 1,
110+
},
111+
{
112+
name: 'some other stream',
113+
stream_id: 2,
114+
},
115+
],
116+
};
117+
const expectedState = [
118+
{
119+
color: 'blue',
120+
stream_id: 3,
121+
name: 'third stream'
122+
},
123+
];
124+
125+
const newState = subscriptionsReducers(prevState, action);
126+
127+
expect(newState).toEqual(expectedState);
128+
});
129+
130+
test('removes subscriptions that exist, do nothing if not', () => {
131+
const prevState = [
132+
{
133+
name: 'some stream',
134+
stream_id: 1,
135+
},
136+
];
137+
const action = {
138+
type: EVENT_SUBSCRIPTION_REMOVE,
139+
subscriptions: [
140+
{
141+
name: 'some stream',
142+
stream_id: 1,
143+
},
144+
{
145+
name: 'some other stream',
146+
stream_id: 2,
147+
}
148+
],
149+
};
150+
const expectedState = [];
151+
152+
const newState = subscriptionsReducers(prevState, action);
153+
154+
expect(newState).toEqual(expectedState);
155+
});
156+
});
157+
});

src/subscriptions/subscriptionsReducers.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ export default (state = initialState, action) => {
1111
case REALM_INIT:
1212
return action.data.subscriptions;
1313
case EVENT_SUBSCRIPTION_ADD:
14-
return state;
15-
case EVENT_SUBSCRIPTION_REMOVE:
16-
return state;
14+
return state.concat(
15+
action.subscriptions.filter(x =>
16+
!state.find(y => x.stream_id === y.stream_id)
17+
)
18+
);
19+
case EVENT_SUBSCRIPTION_REMOVE: {
20+
return state.filter(x =>
21+
!action.subscriptions.find(y => x.stream_id === y.stream_id)
22+
);
23+
}
1724
default:
1825
return state;
1926
}

0 commit comments

Comments
 (0)