diff --git a/client/src/js/__tests__/reducer-messages.test.js b/client/src/js/__tests__/reducer-messages.test.js index 467607fd..c339a5a1 100644 --- a/client/src/js/__tests__/reducer-messages.test.js +++ b/client/src/js/__tests__/reducer-messages.test.js @@ -1,9 +1,91 @@ -import { Map } from 'immutable'; +import { Map, fromJS } from 'immutable'; import reducer from '../reducers/messages'; import * as actions from '../actions'; import { broadcast } from '../actions/message'; describe('reducers/messages', () => { + it('adds the message on ADD_MESSAGE', () => { + const state = reducer(undefined, { + type: actions.ADD_MESSAGE, + server: 'srv', + tab: '#chan1', + message: { + from: 'foo', + content: 'msg' + } + }); + + expect(state.toJS()).toMatchObject({ + srv: { + '#chan1': [{ + from: 'foo', + content: 'msg' + }] + } + }); + }); + + it('adds all the messsages on ADD_MESSAGES', () => { + const state = reducer(undefined, { + type: actions.ADD_MESSAGES, + server: 'srv', + tab: '#chan1', + messages: [ + { + from: 'foo', + content: 'msg' + }, { + from: 'bar', + content: 'msg' + }, { + tab: '#chan2', + from: 'foo', + content: 'msg' + } + ] + }); + + expect(state.toJS()).toMatchObject({ + srv: { + '#chan1': [ + { + from: 'foo', + content: 'msg' + }, { + from: 'bar', + content: 'msg' + } + ], + '#chan2': [{ + from: 'foo', + content: 'msg' + }] + } + }); + }); + + it('handles prepending of messages on ADD_MESSAGES', () => { + let state = fromJS({ + srv: { + '#chan1': [{ id: 0 }] + } + }); + + state = reducer(state, { + type: actions.ADD_MESSAGES, + server: 'srv', + tab: '#chan1', + prepend: true, + messages: [{ id: 1 }, { id: 2 }] + }); + + expect(state.toJS()).toMatchObject({ + srv: { + '#chan1': [{ id: 1 }, { id: 2 }, { id: 0 }] + } + }); + }); + it('adds messages to the correct tabs when broadcasting', () => { let state = { environment: Map({ @@ -26,4 +108,74 @@ describe('reducers/messages', () => { expect(messages.srv['#chan3']).toHaveLength(1); expect(messages.srv['#chan3'][0].content).toBe('test'); }); + + it('deletes all messages related to server when disconnecting', () => { + let state = fromJS({ + srv: { + '#chan1': [ + { content: 'msg1' }, + { content: 'msg2' } + ], + '#chan2': [ + { content: 'msg' } + ] + }, + srv2: { + '#chan1': [ + { content: 'msg' } + ] + } + }); + + state = reducer(state, { + type: actions.DISCONNECT, + server: 'srv' + }); + + expect(state.toJS()).toEqual({ + srv2: { + '#chan1': [ + { content: 'msg' } + ] + } + }); + }); + + it('deletes all messages related to channel when parting', () => { + let state = fromJS({ + srv: { + '#chan1': [ + { content: 'msg1' }, + { content: 'msg2' } + ], + '#chan2': [ + { content: 'msg' } + ] + }, + srv2: { + '#chan1': [ + { content: 'msg' } + ] + } + }); + + state = reducer(state, { + type: actions.PART, + server: 'srv', + channels: ['#chan1'] + }); + + expect(state.toJS()).toEqual({ + srv: { + '#chan2': [ + { content: 'msg' } + ] + }, + srv2: { + '#chan1': [ + { content: 'msg' } + ] + } + }); + }); }); diff --git a/client/src/js/actions.js b/client/src/js/actions.js index 31c8ea3e..c4509613 100644 --- a/client/src/js/actions.js +++ b/client/src/js/actions.js @@ -19,7 +19,6 @@ export const PART = 'PART'; export const RAW = 'RAW'; export const SEARCH_MESSAGES = 'SEARCH_MESSAGES'; export const SELECT_TAB = 'SELECT_TAB'; -export const SEND_MESSAGE = 'SEND_MESSAGE'; export const SET_CERT = 'SET_CERT'; export const SET_CERT_ERROR = 'SET_CERT_ERROR'; export const SET_ENVIRONMENT = 'SET_ENVIRONMENT'; diff --git a/client/src/js/actions/message.js b/client/src/js/actions/message.js index cff60f86..02090c96 100644 --- a/client/src/js/actions/message.js +++ b/client/src/js/actions/message.js @@ -87,7 +87,7 @@ export function sendMessage(content, to, server) { const state = getState(); dispatch({ - type: actions.SEND_MESSAGE, + type: actions.ADD_MESSAGE, server, tab: to, message: initMessage({ diff --git a/client/src/js/reducers/messages.js b/client/src/js/reducers/messages.js index 4e77b570..4b5666cb 100644 --- a/client/src/js/reducers/messages.js +++ b/client/src/js/reducers/messages.js @@ -18,10 +18,6 @@ const Message = Record({ breakpoints: null }); -function addMessage(state, { server, tab, message }) { - return state.updateIn([server, tab], List(), list => list.push(new Message(message))); -} - export const getMessages = state => state.messages; export const getSelectedMessages = createSelector( @@ -31,8 +27,9 @@ export const getSelectedMessages = createSelector( ); export default createReducer(Map(), { - [actions.SEND_MESSAGE]: addMessage, - [actions.ADD_MESSAGE]: addMessage, + [actions.ADD_MESSAGE](state, { server, tab, message }) { + return state.updateIn([server, tab], List(), list => list.push(new Message(message))); + }, [actions.ADD_MESSAGES](state, { server, tab, messages, prepend }) { return state.withMutations(s => {