2017-05-15 03:57:12 +00:00
|
|
|
import { Map, fromJS } from 'immutable';
|
2018-04-05 19:13:32 +00:00
|
|
|
import reducer, { broadcast, getMessageTab } from '../messages';
|
2017-05-10 00:57:54 +00:00
|
|
|
import * as actions from '../actions';
|
2017-06-07 03:13:02 +00:00
|
|
|
import appReducer from '../app';
|
2017-05-10 00:57:54 +00:00
|
|
|
|
2017-06-12 04:39:24 +00:00
|
|
|
describe('message reducer', () => {
|
2017-05-15 03:57:12 +00:00
|
|
|
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: {
|
2018-04-05 23:46:22 +00:00
|
|
|
'#chan1': [
|
|
|
|
{
|
|
|
|
from: 'foo',
|
|
|
|
content: 'msg'
|
|
|
|
}
|
|
|
|
]
|
2017-05-15 03:57:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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'
|
2018-04-05 23:46:22 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-15 03:57:12 +00:00
|
|
|
from: 'bar',
|
|
|
|
content: 'msg'
|
2018-04-05 23:46:22 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-15 03:57:12 +00:00
|
|
|
tab: '#chan2',
|
|
|
|
from: 'foo',
|
|
|
|
content: 'msg'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(state.toJS()).toMatchObject({
|
|
|
|
srv: {
|
|
|
|
'#chan1': [
|
|
|
|
{
|
|
|
|
from: 'foo',
|
|
|
|
content: 'msg'
|
2018-04-05 23:46:22 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-15 03:57:12 +00:00
|
|
|
from: 'bar',
|
|
|
|
content: 'msg'
|
|
|
|
}
|
|
|
|
],
|
2018-04-05 23:46:22 +00:00
|
|
|
'#chan2': [
|
|
|
|
{
|
|
|
|
from: 'foo',
|
|
|
|
content: 'msg'
|
|
|
|
}
|
|
|
|
]
|
2017-05-15 03:57:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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 }]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-10 00:57:54 +00:00
|
|
|
it('adds messages to the correct tabs when broadcasting', () => {
|
|
|
|
let state = {
|
2017-06-07 03:13:02 +00:00
|
|
|
app: appReducer(undefined, { type: '' })
|
2017-05-10 00:57:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const thunk = broadcast('test', 'srv', ['#chan1', '#chan3']);
|
2018-04-05 23:46:22 +00:00
|
|
|
thunk(action => {
|
|
|
|
state.messages = reducer(undefined, action);
|
|
|
|
}, () => state);
|
2017-05-10 00:57:54 +00:00
|
|
|
|
|
|
|
const messages = state.messages.toJS();
|
|
|
|
|
|
|
|
expect(messages.srv).not.toHaveProperty('srv');
|
|
|
|
expect(messages.srv['#chan1']).toHaveLength(1);
|
|
|
|
expect(messages.srv['#chan1'][0].content).toBe('test');
|
|
|
|
expect(messages.srv['#chan3']).toHaveLength(1);
|
|
|
|
expect(messages.srv['#chan3'][0].content).toBe('test');
|
|
|
|
});
|
2017-05-15 03:57:12 +00:00
|
|
|
|
|
|
|
it('deletes all messages related to server when disconnecting', () => {
|
|
|
|
let state = fromJS({
|
|
|
|
srv: {
|
2018-04-05 23:46:22 +00:00
|
|
|
'#chan1': [{ content: 'msg1' }, { content: 'msg2' }],
|
|
|
|
'#chan2': [{ content: 'msg' }]
|
2017-05-15 03:57:12 +00:00
|
|
|
},
|
|
|
|
srv2: {
|
2018-04-05 23:46:22 +00:00
|
|
|
'#chan1': [{ content: 'msg' }]
|
2017-05-15 03:57:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
state = reducer(state, {
|
|
|
|
type: actions.DISCONNECT,
|
|
|
|
server: 'srv'
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(state.toJS()).toEqual({
|
|
|
|
srv2: {
|
2018-04-05 23:46:22 +00:00
|
|
|
'#chan1': [{ content: 'msg' }]
|
2017-05-15 03:57:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deletes all messages related to channel when parting', () => {
|
|
|
|
let state = fromJS({
|
|
|
|
srv: {
|
2018-04-05 23:46:22 +00:00
|
|
|
'#chan1': [{ content: 'msg1' }, { content: 'msg2' }],
|
|
|
|
'#chan2': [{ content: 'msg' }]
|
2017-05-15 03:57:12 +00:00
|
|
|
},
|
|
|
|
srv2: {
|
2018-04-05 23:46:22 +00:00
|
|
|
'#chan1': [{ content: 'msg' }]
|
2017-05-15 03:57:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
state = reducer(state, {
|
|
|
|
type: actions.PART,
|
|
|
|
server: 'srv',
|
|
|
|
channels: ['#chan1']
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(state.toJS()).toEqual({
|
|
|
|
srv: {
|
2018-04-05 23:46:22 +00:00
|
|
|
'#chan2': [{ content: 'msg' }]
|
2017-05-15 03:57:12 +00:00
|
|
|
},
|
|
|
|
srv2: {
|
2018-04-05 23:46:22 +00:00
|
|
|
'#chan1': [{ content: 'msg' }]
|
2017-05-15 03:57:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-05-10 00:57:54 +00:00
|
|
|
});
|
2017-06-23 00:43:36 +00:00
|
|
|
|
|
|
|
describe('getMessageTab()', () => {
|
|
|
|
it('returns the correct tab', () => {
|
|
|
|
const srv = 'chat.freenode.net';
|
|
|
|
[
|
|
|
|
['#cake', '#cake'],
|
|
|
|
['#apple.pie', '#apple.pie'],
|
|
|
|
['bob', 'bob'],
|
|
|
|
[undefined, srv],
|
|
|
|
[null, srv],
|
|
|
|
['*', srv],
|
|
|
|
[srv, srv],
|
|
|
|
['beans.freenode.net', srv]
|
|
|
|
].forEach(([target, expected]) =>
|
|
|
|
expect(getMessageTab(srv, target)).toBe(expected)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|