Add more message reducer tests

This commit is contained in:
Ken-Håvard Lieng 2017-05-15 05:57:12 +02:00
parent c598ace18c
commit a4d40bef39
4 changed files with 157 additions and 9 deletions

View File

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

View File

@ -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';

View File

@ -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({

View File

@ -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 => {