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

431 lines
9.8 KiB
JavaScript
Raw Normal View History

2018-04-25 03:36:27 +00:00
import reducer, { compareUsers, getSortedChannels } from '../channels';
2020-06-15 08:58:51 +00:00
import { connect } from '../networks';
import * as actions from '../actions';
describe('channel reducer', () => {
it('removes channels on PART', () => {
2018-04-25 03:36:27 +00:00
let state = {
srv1: {
2018-04-05 23:46:22 +00:00
chan1: {},
chan2: {},
chan3: {}
},
srv2: {
chan1: {}
}
2018-04-25 03:36:27 +00:00
};
state = reducer(state, {
type: actions.PART,
2020-06-15 08:58:51 +00:00
network: 'srv1',
channels: ['chan1', 'chan3']
});
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
srv1: {
chan2: {}
},
srv2: {
chan1: {}
}
});
});
it('handles SOCKET_PART', () => {
let state = reducer(undefined, socket_join('srv', 'chan1', 'nick1'));
state = reducer(state, socket_join('srv', 'chan1', 'nick2'));
state = reducer(state, socket_join('srv', 'chan2', 'nick2'));
state = reducer(state, {
type: actions.socket.PART,
2020-06-15 08:58:51 +00:00
network: 'srv',
channel: 'chan1',
user: 'nick2'
});
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
srv: {
chan1: {
2019-01-25 07:36:21 +00:00
name: 'chan1',
2018-12-06 12:27:53 +00:00
joined: true,
2018-04-05 23:46:22 +00:00
users: [{ mode: '', nick: 'nick1', renderName: 'nick1' }]
},
chan2: {
2019-01-25 07:36:21 +00:00
name: 'chan2',
2018-12-06 12:27:53 +00:00
joined: true,
2018-04-05 23:46:22 +00:00
users: [{ mode: '', nick: 'nick2', renderName: 'nick2' }]
}
}
});
});
it('handles SOCKET_JOIN', () => {
const state = reducer(undefined, socket_join('srv', 'chan1', 'nick1'));
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
srv: {
chan1: {
2019-01-25 07:36:21 +00:00
name: 'chan1',
2018-12-06 12:27:53 +00:00
joined: true,
2018-04-05 23:46:22 +00:00
users: [{ mode: '', nick: 'nick1', renderName: 'nick1' }]
}
}
});
});
it('handles SOCKET_QUIT', () => {
let state = reducer(undefined, socket_join('srv', 'chan1', 'nick1'));
state = reducer(state, socket_join('srv', 'chan1', 'nick2'));
state = reducer(state, socket_join('srv', 'chan2', 'nick2'));
state = reducer(state, {
type: actions.socket.QUIT,
2020-06-15 08:58:51 +00:00
network: 'srv',
user: 'nick2'
});
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
srv: {
chan1: {
2019-01-25 07:36:21 +00:00
name: 'chan1',
2018-12-06 12:27:53 +00:00
joined: true,
2018-04-05 23:46:22 +00:00
users: [{ mode: '', nick: 'nick1', renderName: 'nick1' }]
},
chan2: {
2019-01-25 07:36:21 +00:00
name: 'chan2',
2018-12-06 12:27:53 +00:00
joined: true,
users: []
}
}
});
});
2020-06-15 08:58:51 +00:00
it('handles KICKED', () => {
let state = reducer(
undefined,
connect({
host: 'srv',
nick: 'nick2'
})
);
state = reducer(state, socket_join('srv', 'chan1', 'nick1'));
state = reducer(state, socket_join('srv', 'chan1', 'nick2'));
state = reducer(state, socket_join('srv', 'chan2', 'nick2'));
state = reducer(state, {
type: actions.KICKED,
network: 'srv',
channel: 'chan2',
user: 'nick2',
self: true
});
expect(state).toEqual({
srv: {
chan1: {
name: 'chan1',
joined: true,
users: [
{ mode: '', nick: 'nick1', renderName: 'nick1' },
{ mode: '', nick: 'nick2', renderName: 'nick2' }
]
},
chan2: {
name: 'chan2',
joined: false,
users: []
}
}
});
state = reducer(state, {
type: actions.KICKED,
network: 'srv',
channel: 'chan1',
user: 'nick1'
});
expect(state).toEqual({
srv: {
chan1: {
name: 'chan1',
joined: true,
users: [{ mode: '', nick: 'nick2', renderName: 'nick2' }]
},
chan2: {
name: 'chan2',
joined: false,
users: []
}
}
});
});
it('handles SOCKET_NICK', () => {
let state = reducer(undefined, socket_join('srv', 'chan1', 'nick1'));
state = reducer(state, socket_join('srv', 'chan1', 'nick2'));
state = reducer(state, socket_join('srv', 'chan2', 'nick2'));
state = reducer(state, {
type: actions.socket.NICK,
2020-06-15 08:58:51 +00:00
network: 'srv',
oldNick: 'nick1',
newNick: 'nick3'
});
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
srv: {
chan1: {
2019-01-25 07:36:21 +00:00
name: 'chan1',
2018-12-06 12:27:53 +00:00
joined: true,
users: [
2017-07-06 04:46:53 +00:00
{ mode: '', nick: 'nick3', renderName: 'nick3' },
{ mode: '', nick: 'nick2', renderName: 'nick2' }
]
},
chan2: {
2019-01-25 07:36:21 +00:00
name: 'chan2',
2018-12-06 12:27:53 +00:00
joined: true,
2018-04-05 23:46:22 +00:00
users: [{ mode: '', nick: 'nick2', renderName: 'nick2' }]
}
}
});
});
it('handles SOCKET_USERS', () => {
2018-12-06 12:27:53 +00:00
let state = reducer(undefined, socket_join('srv', 'chan1', 'nick1'));
state = reducer(state, {
type: actions.socket.USERS,
2020-06-15 08:58:51 +00:00
network: 'srv',
channel: 'chan1',
2018-04-05 23:46:22 +00:00
users: ['user3', 'user2', '@user4', 'user1', '+user5']
});
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
srv: {
chan1: {
2019-01-25 07:36:21 +00:00
name: 'chan1',
2018-12-06 12:27:53 +00:00
joined: true,
users: [
2017-07-06 04:46:53 +00:00
{ mode: '', nick: 'user3', renderName: 'user3' },
{ mode: '', nick: 'user2', renderName: 'user2' },
{ mode: 'o', nick: 'user4', renderName: '@user4' },
{ mode: '', nick: 'user1', renderName: 'user1' },
2017-07-06 04:46:53 +00:00
{ mode: 'v', nick: 'user5', renderName: '+user5' }
]
}
}
2018-04-05 23:46:22 +00:00
});
});
it('handles SOCKET_TOPIC', () => {
2018-12-06 12:27:53 +00:00
let state = reducer(undefined, socket_join('srv', 'chan1', 'nick1'));
state = reducer(state, {
type: actions.socket.TOPIC,
2020-06-15 08:58:51 +00:00
network: 'srv',
channel: 'chan1',
topic: 'the topic'
});
2018-12-06 12:27:53 +00:00
expect(state).toMatchObject({
srv: {
chan1: {
2018-12-06 12:27:53 +00:00
topic: 'the topic'
}
}
});
});
it('handles SOCKET_MODE', () => {
let state = reducer(undefined, socket_join('srv', 'chan1', 'nick1'));
state = reducer(state, socket_join('srv', 'chan1', 'nick2'));
state = reducer(state, socket_join('srv', 'chan2', 'nick2'));
state = reducer(state, socket_mode('srv', 'chan1', 'nick1', 'o', ''));
2018-12-06 12:27:53 +00:00
expect(state).toMatchObject({
srv: {
chan1: {
users: [
{ mode: 'o', nick: 'nick1', renderName: '@nick1' },
{ mode: '', nick: 'nick2', renderName: 'nick2' }
]
},
chan2: {
2018-04-05 23:46:22 +00:00
users: [{ mode: '', nick: 'nick2', renderName: 'nick2' }]
}
}
});
2018-04-05 23:46:22 +00:00
state = reducer(state, socket_mode('srv', 'chan1', 'nick1', 'v', 'o'));
state = reducer(state, socket_mode('srv', 'chan1', 'nick2', 'o', ''));
state = reducer(state, socket_mode('srv', 'chan2', 'not_there', 'x', ''));
2018-12-06 12:27:53 +00:00
expect(state).toMatchObject({
srv: {
chan1: {
users: [
2017-07-06 04:46:53 +00:00
{ mode: 'v', nick: 'nick1', renderName: '+nick1' },
{ mode: 'o', nick: 'nick2', renderName: '@nick2' }
]
},
chan2: {
2018-04-05 23:46:22 +00:00
users: [{ mode: '', nick: 'nick2', renderName: 'nick2' }]
}
}
});
});
2020-06-24 23:50:10 +00:00
it('handles channels from INIT', () => {
const state = reducer(undefined, {
2020-06-24 23:50:10 +00:00
type: actions.INIT,
channels: [
2020-06-15 08:58:51 +00:00
{ network: 'srv', name: 'chan1', topic: 'the topic' },
2020-06-24 23:50:10 +00:00
{ network: 'srv', name: 'chan2', joined: true },
2020-06-15 08:58:51 +00:00
{ network: 'srv2', name: 'chan1' }
]
});
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
srv: {
2020-06-24 23:50:10 +00:00
chan1: { name: 'chan1', topic: 'the topic', users: [] },
2019-01-25 07:36:21 +00:00
chan2: { name: 'chan2', joined: true, users: [] }
},
srv2: {
2020-06-24 23:50:10 +00:00
chan1: { name: 'chan1', users: [] }
}
});
});
2020-06-24 23:50:10 +00:00
it('handles networks from INIT', () => {
const state = reducer(undefined, {
2020-06-24 23:50:10 +00:00
type: actions.INIT,
networks: [{ host: '127.0.0.1' }, { host: 'thehost' }]
});
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
'127.0.0.1': {},
thehost: {}
});
});
2020-06-15 08:58:51 +00:00
it('optimistically adds the network on CONNECT', () => {
2018-05-16 03:02:48 +00:00
const state = reducer(
undefined,
connect({ host: '127.0.0.1', nick: 'nick' })
);
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
'127.0.0.1': {}
});
});
2020-06-15 08:58:51 +00:00
it('removes the network on DISCONNECT', () => {
2018-04-25 03:36:27 +00:00
let state = {
srv: {},
srv2: {}
2018-04-25 03:36:27 +00:00
};
state = reducer(state, {
type: actions.DISCONNECT,
2020-06-15 08:58:51 +00:00
network: 'srv2'
});
2018-04-25 03:36:27 +00:00
expect(state).toEqual({
srv: {}
});
});
});
2020-06-15 08:58:51 +00:00
function socket_join(network, channel, user) {
return {
type: actions.socket.JOIN,
2020-06-15 08:58:51 +00:00
network,
2018-04-05 23:46:22 +00:00
user,
channels: [channel]
};
}
2020-06-15 08:58:51 +00:00
function socket_mode(network, channel, user, add, remove) {
return {
type: actions.socket.MODE,
2020-06-15 08:58:51 +00:00
network,
2018-04-05 23:46:22 +00:00
channel,
user,
add,
remove
};
}
2017-07-06 05:01:14 +00:00
describe('compareUsers()', () => {
it('compares users correctly', () => {
2018-04-05 23:46:22 +00:00
expect(
[
{ renderName: 'user5' },
{ renderName: '@user2' },
{ renderName: 'user3' },
{ renderName: 'user2' },
{ renderName: '+user1' },
{ renderName: '~bob' },
{ renderName: '%apples' },
{ renderName: '&cake' }
].sort(compareUsers)
).toEqual([
2017-07-06 05:01:14 +00:00
{ renderName: '~bob' },
{ renderName: '&cake' },
{ renderName: '@user2' },
{ renderName: '%apples' },
2018-04-05 23:46:22 +00:00
{ renderName: '+user1' },
2017-07-06 05:01:14 +00:00
{ renderName: 'user2' },
2018-04-05 23:46:22 +00:00
{ renderName: 'user3' },
2017-07-06 05:01:14 +00:00
{ renderName: 'user5' }
]);
});
});
2018-04-25 03:36:27 +00:00
describe('getSortedChannels', () => {
2020-06-15 08:58:51 +00:00
it('sorts networks and channels', () => {
2018-04-25 03:36:27 +00:00
expect(
getSortedChannels({
channels: {
'bob.com': {},
'127.0.0.1': {
'#chan1': {
2019-02-08 07:56:21 +00:00
name: '#chan1',
2018-04-25 03:36:27 +00:00
users: [],
topic: 'cake'
},
2019-02-08 07:56:21 +00:00
'#pie': {
name: '#pie'
},
'##apples': {
name: '##apples'
}
2018-04-25 03:36:27 +00:00
}
}
})
).toEqual([
{
address: '127.0.0.1',
2019-02-08 07:56:21 +00:00
channels: [
{
name: '##apples'
},
{
name: '#chan1',
users: [],
topic: 'cake'
},
{
name: '#pie'
}
]
2018-04-25 03:36:27 +00:00
},
{
address: 'bob.com',
channels: []
}
]);
});
});