diff --git a/src/streams/__tests__/streamsReducer-test.js b/src/streams/__tests__/streamsReducer-test.js index e9a8aaccd93..f9dcb67ed4c 100644 --- a/src/streams/__tests__/streamsReducer-test.js +++ b/src/streams/__tests__/streamsReducer-test.js @@ -1,21 +1,20 @@ +/* @flow strict-local */ + import deepFreeze from 'deep-freeze'; +import * as eg from '../../__tests__/lib/exampleData'; import { EventTypes } from '../../api/eventTypes'; -import { ACCOUNT_SWITCH, EVENT } from '../../actionConstants'; +import { EVENT } from '../../actionConstants'; import streamsReducer from '../streamsReducer'; describe('streamsReducer', () => { describe('ACCOUNT_SWITCH', () => { test('resets state to initial state', () => { - const initialState = deepFreeze(['some_stream']); - - const action = deepFreeze({ - type: ACCOUNT_SWITCH, - }); + const initialState = deepFreeze([eg.makeStream({ name: 'some_stream' })]); const expectedState = []; - const actualState = streamsReducer(initialState, action); + const actualState = streamsReducer(initialState, eg.action.account_switch); expect(actualState).toEqual(expectedState); }); @@ -23,36 +22,22 @@ describe('streamsReducer', () => { describe('EVENT -> stream -> create', () => { test('add new stream', () => { + const stream1 = eg.makeStream({ name: 'some stream', stream_id: 1 }); + const stream2 = eg.makeStream({ name: 'some other stream', stream_id: 2 }); + const initialState = deepFreeze([]); const action = deepFreeze({ type: EVENT, event: { + id: 0, type: EventTypes.stream, op: 'create', - streams: [ - { - name: 'some stream', - stream_id: 1, - }, - { - name: 'some other stream', - stream_id: 2, - }, - ], + streams: [stream1, stream2], }, }); - const expectedState = [ - { - name: 'some stream', - stream_id: 1, - }, - { - name: 'some other stream', - stream_id: 2, - }, - ]; + const expectedState = [stream1, stream2]; const newState = streamsReducer(initialState, action); @@ -60,44 +45,26 @@ describe('streamsReducer', () => { }); test('if stream already exist, do not add it', () => { - const initialState = deepFreeze([ - { - description: 'description', - stream_id: 1, - name: 'some stream', - }, - ]); + const stream1 = eg.makeStream({ + description: 'description', + stream_id: 1, + name: 'some stream', + }); + const stream2 = eg.makeStream({ name: 'some other stream', stream_id: 2 }); + + const initialState = deepFreeze([stream1]); const action = deepFreeze({ type: EVENT, event: { + id: 0, type: EventTypes.stream, op: 'create', - streams: [ - { - description: 'description', - name: 'some stream', - stream_id: 1, - }, - { - name: 'some other stream', - stream_id: 2, - }, - ], + streams: [stream1, stream2], }, }); - const expectedState = [ - { - description: 'description', - name: 'some stream', - stream_id: 1, - }, - { - name: 'some other stream', - stream_id: 2, - }, - ]; + const expectedState = [stream1, stream2]; const newState = streamsReducer(initialState, action); @@ -107,49 +74,35 @@ describe('streamsReducer', () => { describe('EVENT -> stream -> delete', () => { test('removes stream from state', () => { - const initialState = deepFreeze([ - { - description: 'description', - stream_id: 1, - name: 'some stream', - }, - { - description: 'description', - stream_id: 2, - name: 'other stream', - }, - { - description: 'description', - stream_id: 3, - name: 'third stream', - }, - ]); + const stream1 = eg.makeStream({ + description: 'description', + stream_id: 1, + name: 'some stream', + }); + const stream2 = eg.makeStream({ + description: 'description', + stream_id: 2, + name: 'other stream', + }); + const stream3 = eg.makeStream({ + description: 'description', + stream_id: 3, + name: 'third stream', + }); + + const initialState = deepFreeze([stream1, stream2, stream3]); const action = deepFreeze({ type: EVENT, event: { + id: 0, type: EventTypes.stream, op: 'delete', - streams: [ - { - name: 'some stream', - stream_id: 1, - }, - { - name: 'some other stream', - stream_id: 2, - }, - ], + streams: [stream1, stream2], }, }); - const expectedState = [ - { - description: 'description', - stream_id: 3, - name: 'third stream', - }, - ]; + const expectedState = [stream3]; const newState = streamsReducer(initialState, action); @@ -157,28 +110,18 @@ describe('streamsReducer', () => { }); test('removes streams that exist, do nothing if not', () => { - const initialState = deepFreeze([ - { - name: 'some stream', - stream_id: 1, - }, - ]); + const stream1 = eg.makeStream({ name: 'some stream', stream_id: 1 }); + const stream2 = eg.makeStream({ name: 'some other stream', stream_id: 2 }); + + const initialState = deepFreeze([stream1]); const action = deepFreeze({ type: EVENT, event: { + id: 0, type: EventTypes.stream, op: 'delete', - streams: [ - { - name: 'some stream', - stream_id: 1, - }, - { - name: 'some other stream', - stream_id: 2, - }, - ], + streams: [stream1, stream2], }, }); @@ -192,55 +135,26 @@ describe('streamsReducer', () => { describe('EVENT -> stream -> update', () => { test('Change the name property', () => { - const initialState = deepFreeze([ - { - stream_id: 123, - name: 'competition', - email_address: '123@realm.com', - }, - { - stream_id: 67, - name: 'design', - email_address: '67@realm.com', - }, - { - stream_id: 53, - name: 'mobile', - email_address: '53@realm.com', - }, - ]); + const stream123 = eg.makeStream({ stream_id: 123, name: 'competition' }); + const stream67 = eg.makeStream({ stream_id: 67, name: 'design' }); + const stream53 = eg.makeStream({ stream_id: 53, name: 'mobile' }); + + const initialState = deepFreeze([stream123, stream67, stream53]); const action = deepFreeze({ type: EVENT, event: { + id: 2, type: EventTypes.stream, - stream_id: 123, op: 'update', - eventId: 2, - id: 2, + stream_id: 123, name: 'competition', property: 'name', value: 'real competition', }, }); - const expectedState = [ - { - stream_id: 123, - name: 'real competition', - email_address: '123@realm.com', - }, - { - stream_id: 67, - name: 'design', - email_address: '67@realm.com', - }, - { - stream_id: 53, - name: 'mobile', - email_address: '53@realm.com', - }, - ]; + const expectedState = [{ ...stream123, name: 'real competition' }, stream67, stream53]; const actualState = streamsReducer(initialState, action); @@ -248,61 +162,34 @@ describe('streamsReducer', () => { }); test('Change the description property', () => { - const initialState = deepFreeze([ - { - stream_id: 123, - name: 'competition', - email_address: '123@realm.com', - description: 'slack', - }, - { - stream_id: 67, - name: 'design', - email_address: '67@realm.com', - description: 'basic design', - }, - { - stream_id: 53, - name: 'mobile', - email_address: '53@realm.com', - description: 'android', - }, - ]); + const stream123 = eg.makeStream({ + stream_id: 123, + name: 'competition', + description: 'slack', + }); + const stream67 = eg.makeStream({ + stream_id: 67, + name: 'design', + description: 'basic design', + }); + const stream53 = eg.makeStream({ stream_id: 53, name: 'mobile', description: 'android' }); + + const initialState = deepFreeze([stream123, stream67, stream53]); const action = deepFreeze({ type: EVENT, event: { + id: 2, type: EventTypes.stream, - stream_id: 53, op: 'update', - eventId: 2, - id: 2, + stream_id: 53, name: 'mobile', property: 'description', value: 'iOS + android', }, }); - const expectedState = [ - { - stream_id: 123, - name: 'competition', - email_address: '123@realm.com', - description: 'slack', - }, - { - stream_id: 67, - name: 'design', - email_address: '67@realm.com', - description: 'basic design', - }, - { - stream_id: 53, - name: 'mobile', - email_address: '53@realm.com', - description: 'iOS + android', - }, - ]; + const expectedState = [stream123, stream67, { ...stream53, description: 'iOS + android' }]; const actualState = streamsReducer(initialState, action);