Added logic to toggle auto-connect on servers

This commit is contained in:
Alejandro Celaya
2021-10-22 20:13:23 +02:00
parent ada5488a6c
commit 7637ce3107
6 changed files with 132 additions and 23 deletions

View File

@@ -1,13 +1,15 @@
import { values } from 'ramda';
import { dissoc, values } from 'ramda';
import { Mock } from 'ts-mockery';
import reducer, {
createServer,
deleteServer,
createServers,
editServer,
setAutoConnect,
EDIT_SERVER,
DELETE_SERVER,
CREATE_SERVERS,
SET_AUTO_CONNECT,
} from '../../../src/servers/reducers/servers';
import { RegularServer } from '../../../src/servers/data';
@@ -29,6 +31,15 @@ describe('serverReducer', () => {
def456: { id: 'def456' },
}));
it('returns as it is when action is EDIT_SERVER and server does not exist', () =>
expect(reducer(
list,
{ type: EDIT_SERVER, serverId: 'invalid', serverData: { foo: 'foo' } } as any,
)).toEqual({
abc123: { id: 'abc123' },
def456: { id: 'def456' },
}));
it('removes server when action is DELETE_SERVER', () =>
expect(reducer(list, { type: DELETE_SERVER, serverId: 'abc123' } as any)).toEqual({
def456: { id: 'def456' },
@@ -45,6 +56,51 @@ describe('serverReducer', () => {
def456: { id: 'def456' },
ghi789: { id: 'ghi789' },
}));
it.each([
[ true ],
[ false ],
])('returns state as it is when trying to set auto-connect on invalid server', (autoConnect) =>
expect(reducer(list, {
type: SET_AUTO_CONNECT,
serverId: 'invalid',
autoConnect,
} as any)).toEqual({
abc123: { id: 'abc123' },
def456: { id: 'def456' },
}));
it('disables auto-connect on a server which is already set to auto-connect', () => {
const listWithDisabledAutoConnect = {
...list,
abc123: { ...list.abc123, autoConnect: true },
};
expect(reducer(listWithDisabledAutoConnect, {
type: SET_AUTO_CONNECT,
serverId: 'abc123',
autoConnect: false,
} as any)).toEqual({
abc123: { id: 'abc123', autoConnect: false },
def456: { id: 'def456' },
});
});
it('disables auto-connect on all servers except selected one', () => {
const listWithEnabledAutoConnect = {
...list,
abc123: { ...list.abc123, autoConnect: true },
};
expect(reducer(listWithEnabledAutoConnect, {
type: SET_AUTO_CONNECT,
serverId: 'def456',
autoConnect: true,
} as any)).toEqual({
abc123: { id: 'abc123', autoConnect: false },
def456: { id: 'def456', autoConnect: true },
});
});
});
describe('action creators', () => {
@@ -82,6 +138,25 @@ describe('serverReducer', () => {
expect(result).toEqual(expect.objectContaining({ type: CREATE_SERVERS }));
});
it('generates an id for every provided server if they do not have it', () => {
const servers = values(list).map(dissoc('id'));
const { newServers } = createServers(servers);
expect(values(newServers).every(({ id }) => !!id)).toEqual(true);
});
});
describe('setAutoConnect', () => {
it.each([
[ true ],
[ false ],
])('returns expected action', (autoConnect) => {
const serverToEdit = Mock.of<RegularServer>({ id: 'abc123' });
const result = setAutoConnect(serverToEdit, autoConnect);
expect(result).toEqual({ type: SET_AUTO_CONNECT, serverId: 'abc123', autoConnect });
});
});
});
});