Skip to content

Commit

Permalink
streamsReducer tests: Start type-checking; use example data
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbobbe committed Apr 14, 2022
1 parent 6716c79 commit 74ebd17
Showing 1 changed file with 73 additions and 186 deletions.
259 changes: 73 additions & 186 deletions src/streams/__tests__/streamsReducer-test.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,70 @@
/* @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);
});
});

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);

expect(newState).toEqual(expectedState);
});

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);

Expand All @@ -107,78 +74,54 @@ 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);

expect(newState).toEqual(expectedState);
});

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],
},
});

Expand All @@ -192,117 +135,61 @@ 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);

expect(actualState).toEqual(expectedState);
});

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);

Expand Down

0 comments on commit 74ebd17

Please sign in to comment.