2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
|
|
|
import type { RegularServer, ServersMap, ServerWithId } from '../../../src/servers/data';
|
2022-11-05 12:08:24 +03:00
|
|
|
import {
|
2018-12-18 21:45:09 +03:00
|
|
|
createServers,
|
2023-02-18 13:11:01 +03:00
|
|
|
deleteServer,
|
2020-04-25 11:22:17 +03:00
|
|
|
editServer,
|
2022-11-05 12:08:24 +03:00
|
|
|
serversReducer,
|
2023-02-18 13:11:01 +03:00
|
|
|
setAutoConnect,
|
2020-04-27 11:52:19 +03:00
|
|
|
} from '../../../src/servers/reducers/servers';
|
2018-08-12 11:17:13 +03:00
|
|
|
|
2022-11-05 11:24:12 +03:00
|
|
|
describe('serversReducer', () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const list: ServersMap = {
|
|
|
|
abc123: fromPartial({ id: 'abc123' }),
|
|
|
|
def456: fromPartial({ id: 'def456' }),
|
2018-08-12 11:17:13 +03:00
|
|
|
};
|
|
|
|
|
2018-08-26 11:52:45 +03:00
|
|
|
describe('reducer', () => {
|
2020-04-27 12:30:51 +03:00
|
|
|
it('returns edited server when action is EDIT_SERVER', () =>
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(serversReducer(list, editServer('abc123', { name: 'foo' }))).toEqual({
|
|
|
|
abc123: { id: 'abc123', name: 'foo' },
|
2020-04-27 12:30:51 +03:00
|
|
|
def456: { id: 'def456' },
|
|
|
|
}));
|
|
|
|
|
2021-10-22 21:13:23 +03:00
|
|
|
it('returns as it is when action is EDIT_SERVER and server does not exist', () =>
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(serversReducer(list, editServer('invalid', { name: 'foo' }))).toEqual({
|
2021-10-22 21:13:23 +03:00
|
|
|
abc123: { id: 'abc123' },
|
|
|
|
def456: { id: 'def456' },
|
|
|
|
}));
|
|
|
|
|
2020-04-27 12:30:51 +03:00
|
|
|
it('removes server when action is DELETE_SERVER', () =>
|
2023-04-13 22:48:29 +03:00
|
|
|
expect(serversReducer(list, deleteServer(fromPartial<ServerWithId>({ id: 'abc123' })))).toEqual({
|
2020-04-27 12:30:51 +03:00
|
|
|
def456: { id: 'def456' },
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('appends server when action is CREATE_SERVERS', () =>
|
2023-04-13 22:48:29 +03:00
|
|
|
expect(serversReducer(list, createServers([fromPartial<ServerWithId>({ id: 'ghi789' })]))).toEqual({
|
2020-04-27 12:30:51 +03:00
|
|
|
abc123: { id: 'abc123' },
|
|
|
|
def456: { id: 'def456' },
|
|
|
|
ghi789: { id: 'ghi789' },
|
|
|
|
}));
|
2021-10-22 21:13:23 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[true],
|
|
|
|
[false],
|
2021-10-22 21:13:23 +03:00
|
|
|
])('returns state as it is when trying to set auto-connect on invalid server', (autoConnect) =>
|
2023-04-13 22:48:29 +03:00
|
|
|
expect(serversReducer(list, setAutoConnect(fromPartial<ServerWithId>({ id: 'invalid' }), autoConnect))).toEqual({
|
2021-10-22 21:13:23 +03:00
|
|
|
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 },
|
|
|
|
};
|
|
|
|
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(serversReducer(
|
|
|
|
listWithDisabledAutoConnect,
|
2023-04-13 22:48:29 +03:00
|
|
|
setAutoConnect(fromPartial<ServerWithId>({ id: 'abc123' }), false),
|
2023-03-18 14:35:33 +03:00
|
|
|
)).toEqual({
|
2021-10-22 21:13:23 +03:00
|
|
|
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 },
|
|
|
|
};
|
|
|
|
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(serversReducer(
|
|
|
|
listWithEnabledAutoConnect,
|
2023-04-13 22:48:29 +03:00
|
|
|
setAutoConnect(fromPartial<ServerWithId>({ id: 'def456' }), true),
|
2023-03-18 14:35:33 +03:00
|
|
|
)).toEqual({
|
2021-10-22 21:13:23 +03:00
|
|
|
abc123: { id: 'abc123', autoConnect: false },
|
|
|
|
def456: { id: 'def456', autoConnect: true },
|
|
|
|
});
|
|
|
|
});
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('action creators', () => {
|
2020-03-15 13:29:20 +03:00
|
|
|
describe('editServer', () => {
|
2020-04-27 12:30:51 +03:00
|
|
|
it('returns expected action', () => {
|
|
|
|
const serverData = { name: 'edited' };
|
2023-03-18 14:35:33 +03:00
|
|
|
const { payload } = editServer('123', serverData);
|
2020-03-15 13:29:20 +03:00
|
|
|
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(payload).toEqual({ serverId: '123', serverData });
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deleteServer', () => {
|
2020-04-27 12:30:51 +03:00
|
|
|
it('returns expected action', () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const serverToDelete = fromPartial<RegularServer>({ id: 'abc123' });
|
2023-03-18 14:35:33 +03:00
|
|
|
const { payload } = deleteServer(serverToDelete);
|
2018-08-12 11:17:13 +03:00
|
|
|
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(payload).toEqual({ id: 'abc123' });
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
});
|
2018-08-23 17:35:27 +03:00
|
|
|
|
2020-03-15 13:29:20 +03:00
|
|
|
describe('createServers', () => {
|
2020-04-27 12:30:51 +03:00
|
|
|
it('returns expected action', () => {
|
2023-11-01 12:07:50 +03:00
|
|
|
const newServers = Object.values(list);
|
2023-03-18 14:35:33 +03:00
|
|
|
const { payload } = createServers(newServers);
|
2018-08-23 17:35:27 +03:00
|
|
|
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(payload).toEqual(list);
|
2018-08-23 17:35:27 +03:00
|
|
|
});
|
2021-10-22 21:13:23 +03:00
|
|
|
|
|
|
|
it('generates an id for every provided server if they do not have it', () => {
|
2023-11-01 12:07:50 +03:00
|
|
|
const servers = Object.values(list).map(({ id, ...rest }) => rest);
|
2022-11-05 11:24:12 +03:00
|
|
|
const { payload } = createServers(servers);
|
2021-10-22 21:13:23 +03:00
|
|
|
|
2023-11-01 12:07:50 +03:00
|
|
|
expect(Object.values(payload).every(({ id }) => !!id)).toEqual(true);
|
2021-10-22 21:13:23 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setAutoConnect', () => {
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[true],
|
|
|
|
[false],
|
2021-10-22 21:13:23 +03:00
|
|
|
])('returns expected action', (autoConnect) => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const serverToEdit = fromPartial<RegularServer>({ id: 'abc123' });
|
2023-03-18 14:35:33 +03:00
|
|
|
const { payload } = setAutoConnect(serverToEdit, autoConnect);
|
2021-10-22 21:13:23 +03:00
|
|
|
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(payload).toEqual({ serverId: 'abc123', autoConnect });
|
2021-10-22 21:13:23 +03:00
|
|
|
});
|
2018-08-23 17:35:27 +03:00
|
|
|
});
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
});
|