dispatch/client/js/state/__tests__/reducer-messages.test.js

304 lines
6.4 KiB
JavaScript
Raw Normal View History

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';
import { unix } from 'utils';
2017-05-10 00:57:54 +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,
2020-06-15 08:58:51 +00:00
network: 'srv',
2017-05-15 03:57:12 +00:00
tab: '#chan1',
message: {
from: 'foo',
content: 'msg'
}
});
2018-04-25 03:36:27 +00:00
expect(state).toMatchObject({
2017-05-15 03:57:12 +00:00
srv: {
2018-04-05 23:46:22 +00:00
'#chan1': [
{
from: 'foo',
2020-06-30 11:24:23 +00:00
content: [{ type: 'text', text: 'msg' }]
2018-04-05 23:46:22 +00:00
}
]
2017-05-15 03:57:12 +00:00
}
});
});
2018-06-18 13:47:51 +00:00
it('adds all the messages on ADD_MESSAGES', () => {
2017-05-15 03:57:12 +00:00
const state = reducer(undefined, {
type: actions.ADD_MESSAGES,
2020-06-15 08:58:51 +00:00
network: 'srv',
2017-05-15 03:57:12 +00:00
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'
}
]
});
2018-04-25 03:36:27 +00:00
expect(state).toMatchObject({
2017-05-15 03:57:12 +00:00
srv: {
'#chan1': [
{
from: 'foo',
2020-06-30 11:24:23 +00:00
content: [{ type: 'text', text: 'msg' }]
2018-04-05 23:46:22 +00:00
},
{
2017-05-15 03:57:12 +00:00
from: 'bar',
2020-06-30 11:24:23 +00:00
content: [{ type: 'text', text: 'msg' }]
2017-05-15 03:57:12 +00:00
}
],
2018-04-05 23:46:22 +00:00
'#chan2': [
{
from: 'foo',
2020-06-30 11:24:23 +00:00
content: [{ type: 'text', text: 'msg' }]
2018-04-05 23:46:22 +00:00
}
]
2017-05-15 03:57:12 +00:00
}
});
});
it('handles prepending of messages on ADD_MESSAGES', () => {
2018-04-25 03:36:27 +00:00
let state = {
2017-05-15 03:57:12 +00:00
srv: {
'#chan1': [{ id: 0 }]
}
2018-04-25 03:36:27 +00:00
};
2017-05-15 03:57:12 +00:00
state = reducer(state, {
type: actions.ADD_MESSAGES,
2020-06-15 08:58:51 +00:00
network: 'srv',
2017-05-15 03:57:12 +00:00
tab: '#chan1',
prepend: true,
2020-04-30 05:54:30 +00:00
messages: [
{ id: 1, date: new Date() },
{ id: 2, date: new Date() }
]
2017-05-15 03:57:12 +00:00
});
2018-04-25 03:36:27 +00:00
expect(state).toMatchObject({
2017-05-15 03:57:12 +00:00
srv: {
'#chan1': [{ id: 1 }, { id: 2 }, { id: 0 }]
}
});
});
2018-12-15 10:20:49 +00:00
it('adds date markers when prepending messages', () => {
let state = {
srv: {
'#chan1': [{ id: 0, date: new Date(1990, 0, 3) }]
2018-12-15 10:20:49 +00:00
}
};
state = reducer(state, {
type: actions.ADD_MESSAGES,
2020-06-15 08:58:51 +00:00
network: 'srv',
2018-12-15 10:20:49 +00:00
tab: '#chan1',
prepend: true,
messages: [
{ id: 1, time: unix(new Date(1990, 0, 1)) },
{ id: 2, time: unix(new Date(1990, 0, 2)) }
2018-12-15 10:20:49 +00:00
]
});
expect(state).toMatchObject({
srv: {
'#chan1': [
{ id: 1 },
{ type: 'date' },
{ id: 2 },
{ type: 'date' },
{ id: 0 }
]
}
});
});
it('adds a date marker when adding a message', () => {
let state = {
srv: {
'#chan1': [{ id: 0, date: new Date(1999, 0, 1) }]
}
};
state = reducer(state, {
type: actions.ADD_MESSAGE,
2020-06-15 08:58:51 +00:00
network: 'srv',
2018-12-15 10:20:49 +00:00
tab: '#chan1',
message: { id: 1, date: new Date(1990, 0, 2) }
});
expect(state).toMatchObject({
srv: {
'#chan1': [{ id: 0 }, { type: 'date' }, { id: 1 }]
}
});
});
it('adds date markers when adding messages', () => {
let state = {
srv: {
'#chan1': [{ id: 0, date: new Date(1990, 0, 1) }]
2018-12-15 10:20:49 +00:00
}
};
state = reducer(state, {
type: actions.ADD_MESSAGES,
2020-06-15 08:58:51 +00:00
network: 'srv',
2018-12-15 10:20:49 +00:00
tab: '#chan1',
messages: [
{ id: 1, time: unix(new Date(1990, 0, 2)) },
{ id: 2, time: unix(new Date(1990, 0, 3)) },
{ id: 3, time: unix(new Date(1990, 0, 3)) }
2018-12-15 10:20:49 +00:00
]
});
expect(state).toMatchObject({
srv: {
'#chan1': [
{ id: 0 },
{ type: 'date' },
{ id: 1 },
{ type: 'date' },
{ id: 2 },
{ id: 3 }
]
}
});
});
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-08-12 21:19:17 +00:00
thunk(
action => {
state.messages = reducer(undefined, action);
},
() => state
);
2017-05-10 00:57:54 +00:00
2018-04-25 03:36:27 +00:00
const messages = state.messages;
2017-05-10 00:57:54 +00:00
expect(messages.srv).not.toHaveProperty('srv');
expect(messages.srv['#chan1']).toHaveLength(1);
2020-06-30 11:24:23 +00:00
expect(messages.srv['#chan1'][0].content).toMatchObject([
{ type: 'text', text: 'test' }
]);
2017-05-10 00:57:54 +00:00
expect(messages.srv['#chan3']).toHaveLength(1);
2020-06-30 11:24:23 +00:00
expect(messages.srv['#chan3'][0].content).toMatchObject([
{ type: 'text', text: 'test' }
]);
2017-05-10 00:57:54 +00:00
});
2017-05-15 03:57:12 +00:00
2020-06-15 08:58:51 +00:00
it('deletes all messages related to network when disconnecting', () => {
2018-04-25 03:36:27 +00:00
let state = {
2017-05-15 03:57:12 +00:00
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
}
2018-04-25 03:36:27 +00:00
};
2017-05-15 03:57:12 +00:00
state = reducer(state, {
type: actions.DISCONNECT,
2020-06-15 08:58:51 +00:00
network: 'srv'
2017-05-15 03:57:12 +00:00
});
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
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
}
});
});
it('deletes all messages related to channel when parting', () => {
2018-04-25 03:36:27 +00:00
let state = {
2017-05-15 03:57:12 +00:00
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
}
2018-04-25 03:36:27 +00:00
};
2017-05-15 03:57:12 +00:00
state = reducer(state, {
type: actions.PART,
2020-06-15 08:58:51 +00:00
network: 'srv',
2017-05-15 03:57:12 +00:00
channels: ['#chan1']
});
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
2017-05-15 03:57:12 +00:00
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
}
});
});
it('deletes direct messages when closing a direct message tab', () => {
let state = {
srv: {
bob: [{ content: 'msg1' }, { content: 'msg2' }],
'#chan2': [{ content: 'msg' }]
},
srv2: {
'#chan1': [{ content: 'msg' }]
}
};
state = reducer(state, {
type: actions.CLOSE_PRIVATE_CHAT,
2020-06-15 08:58:51 +00:00
network: 'srv',
nick: 'bob'
});
expect(state).toEqual({
srv: {
'#chan2': [{ content: 'msg' }]
},
srv2: {
'#chan1': [{ content: 'msg' }]
}
});
});
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)
);
});
});