Skip to content

Commit

Permalink
flags reducer: Assert flags present on EVENT_NEW_MESSAGE action.
Browse files Browse the repository at this point in the history
Much like we did for the various `unread` sub-reducers in the
previous commit, this simplifies a bit the potential paths through
this code.

There was a test for this impossible case; remove that.

While we're here, also remove a similar impossible check on
`messages` being falsy, as if we got an EVENT_UPDATE_MESSAGE_FLAGS
action with no `messages` property.
  • Loading branch information
gnprice authored and chrisbobbe committed May 24, 2021
1 parent 54ae694 commit b2433dc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
15 changes: 0 additions & 15 deletions src/chat/__tests__/flagsReducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,6 @@ describe('flagsReducer', () => {
});

describe('EVENT_NEW_MESSAGE', () => {
test('when no flags key is passed, do not fail, do nothing', () => {
const prevState = NULL_OBJECT;

const action = deepFreeze({
type: EVENT_NEW_MESSAGE,
message: { id: 2 },
});

const expectedState = {};

const actualState = flagsReducer(prevState, action);

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

test('adds to store flags from new message', () => {
const prevState = NULL_OBJECT;

Expand Down
10 changes: 7 additions & 3 deletions src/chat/flagsReducer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* @flow strict-local */
import invariant from 'invariant';

import type { Action, FlagsState, Message } from '../types';
import {
REALM_INIT,
Expand Down Expand Up @@ -29,9 +31,9 @@ const initialState = {
const addFlagsForMessages = (
state: FlagsState,
messages: $ReadOnlyArray<number>,
flags?: $ReadOnlyArray<string>,
flags: $ReadOnlyArray<string>,
): FlagsState => {
if (!messages || messages.length === 0 || !flags || flags.length === 0) {
if (messages.length === 0 || flags.length === 0) {
return state;
}

Expand Down Expand Up @@ -111,8 +113,10 @@ export default (state: FlagsState = initialState, action: Action): FlagsState =>
case MESSAGE_FETCH_COMPLETE:
return processFlagsForMessages(state, action.messages);

case EVENT_NEW_MESSAGE:
case EVENT_NEW_MESSAGE: {
invariant(action.message.flags, 'message in EVENT_NEW_MESSAGE must have flags');
return addFlagsForMessages(state, [action.message.id], action.message.flags);
}

case EVENT_UPDATE_MESSAGE_FLAGS:
return eventUpdateMessageFlags(state, action);
Expand Down

0 comments on commit b2433dc

Please sign in to comment.