2018-04-25 03:36:27 +00:00
|
|
|
import reducer, { compareUsers, getSortedChannels } from '../channels';
|
2018-04-05 23:46:22 +00:00
|
|
|
import { connect } from '../servers';
|
2017-04-23 06:45:35 +00:00
|
|
|
import * as actions from '../actions';
|
|
|
|
|
2017-06-12 04:39:24 +00:00
|
|
|
describe('channel reducer', () => {
|
2017-04-23 06:45:35 +00:00
|
|
|
it('removes channels on PART', () => {
|
2018-04-25 03:36:27 +00:00
|
|
|
let state = {
|
2017-04-23 06:45:35 +00:00
|
|
|
srv1: {
|
2018-04-05 23:46:22 +00:00
|
|
|
chan1: {},
|
|
|
|
chan2: {},
|
|
|
|
chan3: {}
|
2017-04-23 06:45:35 +00:00
|
|
|
},
|
|
|
|
srv2: {
|
|
|
|
chan1: {}
|
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
};
|
2017-04-23 06:45:35 +00:00
|
|
|
|
|
|
|
state = reducer(state, {
|
|
|
|
type: actions.PART,
|
|
|
|
server: 'srv1',
|
|
|
|
channels: ['chan1', 'chan3']
|
|
|
|
});
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
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, {
|
2017-05-26 06:20:00 +00:00
|
|
|
type: actions.socket.PART,
|
2017-04-23 06:45:35 +00:00
|
|
|
server: 'srv',
|
|
|
|
channel: 'chan1',
|
|
|
|
user: 'nick2'
|
|
|
|
});
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
srv: {
|
|
|
|
chan1: {
|
2018-04-05 23:46:22 +00:00
|
|
|
users: [{ mode: '', nick: 'nick1', renderName: 'nick1' }]
|
2017-04-23 06:45:35 +00:00
|
|
|
},
|
|
|
|
chan2: {
|
2018-04-05 23:46:22 +00:00
|
|
|
users: [{ mode: '', nick: 'nick2', renderName: 'nick2' }]
|
2017-04-23 06:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles SOCKET_JOIN', () => {
|
|
|
|
const state = reducer(undefined, socket_join('srv', 'chan1', 'nick1'));
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
srv: {
|
|
|
|
chan1: {
|
2018-04-05 23:46:22 +00:00
|
|
|
users: [{ mode: '', nick: 'nick1', renderName: 'nick1' }]
|
2017-04-23 06:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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, {
|
2017-05-26 06:20:00 +00:00
|
|
|
type: actions.socket.QUIT,
|
2017-04-23 06:45:35 +00:00
|
|
|
server: 'srv',
|
|
|
|
user: 'nick2'
|
|
|
|
});
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
srv: {
|
|
|
|
chan1: {
|
2018-04-05 23:46:22 +00:00
|
|
|
users: [{ mode: '', nick: 'nick1', renderName: 'nick1' }]
|
2017-04-23 06:45:35 +00:00
|
|
|
},
|
|
|
|
chan2: {
|
|
|
|
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, {
|
2017-05-26 06:20:00 +00:00
|
|
|
type: actions.socket.NICK,
|
2017-04-23 06:45:35 +00:00
|
|
|
server: 'srv',
|
2017-06-21 05:23:07 +00:00
|
|
|
oldNick: 'nick1',
|
|
|
|
newNick: 'nick3'
|
2017-04-23 06:45:35 +00:00
|
|
|
});
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
srv: {
|
|
|
|
chan1: {
|
|
|
|
users: [
|
2017-07-06 04:46:53 +00:00
|
|
|
{ mode: '', nick: 'nick3', renderName: 'nick3' },
|
|
|
|
{ mode: '', nick: 'nick2', renderName: 'nick2' }
|
2017-04-23 06:45:35 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
chan2: {
|
2018-04-05 23:46:22 +00:00
|
|
|
users: [{ mode: '', nick: 'nick2', renderName: 'nick2' }]
|
2017-04-23 06:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles SOCKET_USERS', () => {
|
|
|
|
const state = reducer(undefined, {
|
2017-05-26 06:20:00 +00:00
|
|
|
type: actions.socket.USERS,
|
2017-04-23 06:45:35 +00:00
|
|
|
server: 'srv',
|
|
|
|
channel: 'chan1',
|
2018-04-05 23:46:22 +00:00
|
|
|
users: ['user3', 'user2', '@user4', 'user1', '+user5']
|
2017-04-23 06:45:35 +00:00
|
|
|
});
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
srv: {
|
|
|
|
chan1: {
|
|
|
|
users: [
|
2017-07-06 04:46:53 +00:00
|
|
|
{ mode: '', nick: 'user3', renderName: 'user3' },
|
|
|
|
{ mode: '', nick: 'user2', renderName: 'user2' },
|
2017-04-23 06:45:35 +00:00
|
|
|
{ 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' }
|
2017-04-23 06:45:35 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 23:46:22 +00:00
|
|
|
});
|
2017-04-23 06:45:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles SOCKET_TOPIC', () => {
|
|
|
|
const state = reducer(undefined, {
|
2017-05-26 06:20:00 +00:00
|
|
|
type: actions.socket.TOPIC,
|
2017-04-23 06:45:35 +00:00
|
|
|
server: 'srv',
|
|
|
|
channel: 'chan1',
|
|
|
|
topic: 'the topic'
|
|
|
|
});
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
srv: {
|
|
|
|
chan1: {
|
2018-04-25 03:36:27 +00:00
|
|
|
topic: 'the topic',
|
|
|
|
users: []
|
2017-04-23 06:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
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' }]
|
2017-04-23 06:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-05 23:46:22 +00:00
|
|
|
state = reducer(state, socket_mode('srv', 'chan1', 'nick1', 'v', 'o'));
|
2017-04-23 06:45:35 +00:00
|
|
|
state = reducer(state, socket_mode('srv', 'chan1', 'nick2', 'o', ''));
|
|
|
|
state = reducer(state, socket_mode('srv', 'chan2', 'not_there', 'x', ''));
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
srv: {
|
|
|
|
chan1: {
|
|
|
|
users: [
|
2017-07-06 04:46:53 +00:00
|
|
|
{ mode: 'v', nick: 'nick1', renderName: '+nick1' },
|
|
|
|
{ mode: 'o', nick: 'nick2', renderName: '@nick2' }
|
2017-04-23 06:45:35 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
chan2: {
|
2018-04-05 23:46:22 +00:00
|
|
|
users: [{ mode: '', nick: 'nick2', renderName: 'nick2' }]
|
2017-04-23 06:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles SOCKET_CHANNELS', () => {
|
|
|
|
const state = reducer(undefined, {
|
2017-05-26 06:20:00 +00:00
|
|
|
type: actions.socket.CHANNELS,
|
2017-04-23 06:45:35 +00:00
|
|
|
data: [
|
|
|
|
{ server: 'srv', name: 'chan1', topic: 'the topic' },
|
|
|
|
{ server: 'srv', name: 'chan2' },
|
|
|
|
{ server: 'srv2', name: 'chan1' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
srv: {
|
|
|
|
chan1: { topic: 'the topic', users: [] },
|
|
|
|
chan2: { users: [] }
|
|
|
|
},
|
|
|
|
srv2: {
|
|
|
|
chan1: { users: [] }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles SOCKET_SERVERS', () => {
|
|
|
|
const state = reducer(undefined, {
|
2017-05-26 06:20:00 +00:00
|
|
|
type: actions.socket.SERVERS,
|
2018-04-05 23:46:22 +00:00
|
|
|
data: [{ host: '127.0.0.1' }, { host: 'thehost' }]
|
2017-04-23 06:45:35 +00:00
|
|
|
});
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
'127.0.0.1': {},
|
|
|
|
thehost: {}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('optimistically adds the server on CONNECT', () => {
|
|
|
|
const state = reducer(undefined, connect('127.0.0.1:1337', 'nick', {}));
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
'127.0.0.1': {}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes the server on DISCONNECT', () => {
|
2018-04-25 03:36:27 +00:00
|
|
|
let state = {
|
2017-04-23 06:45:35 +00:00
|
|
|
srv: {},
|
|
|
|
srv2: {}
|
2018-04-25 03:36:27 +00:00
|
|
|
};
|
2017-04-23 06:45:35 +00:00
|
|
|
|
|
|
|
state = reducer(state, {
|
|
|
|
type: actions.DISCONNECT,
|
|
|
|
server: 'srv2'
|
|
|
|
});
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
expect(state).toEqual({
|
2017-04-23 06:45:35 +00:00
|
|
|
srv: {}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function socket_join(server, channel, user) {
|
|
|
|
return {
|
2017-05-26 06:20:00 +00:00
|
|
|
type: actions.socket.JOIN,
|
2018-04-05 23:46:22 +00:00
|
|
|
server,
|
|
|
|
user,
|
2017-04-23 06:45:35 +00:00
|
|
|
channels: [channel]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function socket_mode(server, channel, user, add, remove) {
|
|
|
|
return {
|
2017-05-26 06:20:00 +00:00
|
|
|
type: actions.socket.MODE,
|
2018-04-05 23:46:22 +00:00
|
|
|
server,
|
|
|
|
channel,
|
|
|
|
user,
|
|
|
|
add,
|
|
|
|
remove
|
2017-04-23 06:45:35 +00:00
|
|
|
};
|
|
|
|
}
|
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', () => {
|
|
|
|
it('sorts servers and channels', () => {
|
|
|
|
expect(
|
|
|
|
getSortedChannels({
|
|
|
|
channels: {
|
|
|
|
'bob.com': {},
|
|
|
|
'127.0.0.1': {
|
|
|
|
'#chan1': {
|
|
|
|
users: [],
|
|
|
|
topic: 'cake'
|
|
|
|
},
|
|
|
|
'#pie': {},
|
|
|
|
'##apples': {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
).toEqual([
|
|
|
|
{
|
|
|
|
address: '127.0.0.1',
|
|
|
|
channels: ['##apples', '#chan1', '#pie']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
address: 'bob.com',
|
|
|
|
channels: []
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|