diff --git a/ts/state/ducks/conversations.ts b/ts/state/ducks/conversations.ts index 89e799b22..bf75092e3 100644 --- a/ts/state/ducks/conversations.ts +++ b/ts/state/ducks/conversations.ts @@ -5459,6 +5459,16 @@ export function reducer( const { payload } = action; const { conversationId, messageId, switchToAssociatedView } = payload; + let conversation: ConversationType | undefined; + + if (conversationId) { + conversation = getOwn(state.conversationLookup, conversationId); + if (!conversation) { + log.error(`Unknown conversation selected, id: [${conversationId}]`); + return state; + } + } + const nextState = { ...omit(state, 'contactSpoofingReview'), selectedConversationId: conversationId, @@ -5466,11 +5476,7 @@ export function reducer( targetedMessageSource: TargetedMessageSource.NavigateToMessage, }; - if (switchToAssociatedView && conversationId) { - const conversation = getOwn(state.conversationLookup, conversationId); - if (!conversation) { - return nextState; - } + if (switchToAssociatedView && conversation) { return { ...omit(nextState, 'composer', 'selectedMessageIds'), showArchived: Boolean(conversation.isArchived), diff --git a/ts/test-electron/state/ducks/conversations_test.ts b/ts/test-electron/state/ducks/conversations_test.ts index 3d788bda6..50682a3f9 100644 --- a/ts/test-electron/state/ducks/conversations_test.ts +++ b/ts/test-electron/state/ducks/conversations_test.ts @@ -372,7 +372,7 @@ describe('both/state/ducks/conversations', () => { } describe('showConversation', () => { - it('selects a conversation id', () => { + it('does not select a conversation if it does not exist', () => { const state = { ...getEmptyState(), }; @@ -385,14 +385,44 @@ describe('both/state/ducks/conversations', () => { const action = dispatch.getCall(0).args[0]; const nextState = reducer(state, action); + assert.isUndefined(nextState.selectedConversationId); + assert.isUndefined(nextState.targetedMessage); + }); + + it('selects a conversation id', () => { + const conversation = getDefaultConversation({ + id: 'abc123', + }); + const state = { + ...getEmptyState(), + conversationLookup: { + [conversation.id]: conversation, + }, + }; + const dispatch = sinon.spy(); + showConversation({ conversationId: 'abc123' })( + dispatch, + getEmptyRootState, + null + ); + const action = dispatch.getCall(0).args[0]; + const nextState = reducer(state, action); + assert.equal(nextState.selectedConversationId, 'abc123'); assert.isUndefined(nextState.targetedMessage); }); it('selects a conversation and a message', () => { + const conversation = getDefaultConversation({ + id: 'abc123', + }); const state = { ...getEmptyState(), + conversationLookup: { + [conversation.id]: conversation, + }, }; + const dispatch = sinon.spy(); showConversation({ conversationId: 'abc123',