shlink-web-client/test/servers/reducers/servers.test.ts

159 lines
4.7 KiB
TypeScript
Raw Normal View History

import { dissoc, values } from 'ramda';
2020-08-23 11:20:31 +03:00
import { Mock } from 'ts-mockery';
2022-11-05 12:08:24 +03:00
import {
deleteServer,
createServers,
editServer,
setAutoConnect,
2022-11-05 12:08:24 +03:00
serversReducer,
2020-04-27 11:52:19 +03:00
} from '../../../src/servers/reducers/servers';
2020-08-23 11:20:31 +03:00
import { RegularServer } from '../../../src/servers/data';
2018-08-12 11:17:13 +03:00
describe('serversReducer', () => {
2019-04-28 13:40:50 +03:00
const list = {
2020-08-23 11:20:31 +03:00
abc123: Mock.of<RegularServer>({ id: 'abc123' }),
def456: Mock.of<RegularServer>({ id: 'def456' }),
2018-08-12 11:17:13 +03:00
};
afterEach(jest.clearAllMocks);
2018-08-26 11:52:45 +03:00
describe('reducer', () => {
it('returns edited server when action is EDIT_SERVER', () =>
2022-11-05 12:08:24 +03:00
expect(serversReducer(list, {
type: editServer.toString(),
payload: { serverId: 'abc123', serverData: { foo: 'foo' } },
2022-11-05 12:08:24 +03:00
})).toEqual({
abc123: { id: 'abc123', foo: 'foo' },
def456: { id: 'def456' },
}));
it('returns as it is when action is EDIT_SERVER and server does not exist', () =>
2022-11-05 12:08:24 +03:00
expect(serversReducer(list, {
type: editServer.toString(),
payload: { serverId: 'invalid', serverData: { foo: 'foo' } },
2022-11-05 12:08:24 +03:00
})).toEqual({
abc123: { id: 'abc123' },
def456: { id: 'def456' },
}));
it('removes server when action is DELETE_SERVER', () =>
2022-11-05 12:08:24 +03:00
expect(serversReducer(list, {
type: deleteServer.toString(),
payload: { id: 'abc123' },
})).toEqual({
def456: { id: 'def456' },
}));
it('appends server when action is CREATE_SERVERS', () =>
2022-11-05 12:08:24 +03:00
expect(serversReducer(list, {
type: createServers.toString(),
payload: {
ghi789: { id: 'ghi789' },
},
2022-11-05 12:08:24 +03:00
})).toEqual({
abc123: { id: 'abc123' },
def456: { id: 'def456' },
ghi789: { id: 'ghi789' },
}));
it.each([
2022-03-26 14:17:42 +03:00
[true],
[false],
])('returns state as it is when trying to set auto-connect on invalid server', (autoConnect) =>
2022-11-05 12:08:24 +03:00
expect(serversReducer(list, {
type: setAutoConnect.toString(),
payload: { serverId: 'invalid', autoConnect },
2022-11-05 12:08:24 +03:00
})).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 },
};
2022-11-05 12:08:24 +03:00
expect(serversReducer(listWithDisabledAutoConnect, {
type: setAutoConnect.toString(),
payload: { serverId: 'abc123', autoConnect: false },
2022-11-05 12:08:24 +03:00
})).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 },
};
2022-11-05 12:08:24 +03:00
expect(serversReducer(listWithEnabledAutoConnect, {
type: setAutoConnect.toString(),
payload: { serverId: 'def456', autoConnect: true },
2022-11-05 12:08:24 +03:00
})).toEqual({
abc123: { id: 'abc123', autoConnect: false },
def456: { id: 'def456', autoConnect: true },
});
});
2018-08-12 11:17:13 +03:00
});
describe('action creators', () => {
describe('editServer', () => {
it('returns expected action', () => {
const serverData = { name: 'edited' };
const result = editServer('123', serverData);
expect(result).toEqual({
2022-11-05 12:08:24 +03:00
type: editServer.toString(),
payload: { serverId: '123', serverData },
});
2018-08-12 11:17:13 +03:00
});
});
describe('deleteServer', () => {
it('returns expected action', () => {
2020-08-23 11:20:31 +03:00
const serverToDelete = Mock.of<RegularServer>({ id: 'abc123' });
const result = deleteServer(serverToDelete);
2018-08-12 11:17:13 +03:00
expect(result).toEqual({
2022-11-05 12:08:24 +03:00
type: deleteServer.toString(),
payload: { id: 'abc123' },
});
2018-08-12 11:17:13 +03:00
});
});
2018-08-23 17:35:27 +03:00
describe('createServers', () => {
it('returns expected action', () => {
const newServers = values(list);
const result = createServers(newServers);
2018-08-23 17:35:27 +03:00
2022-11-05 12:08:24 +03:00
expect(result).toEqual(expect.objectContaining({ type: createServers.toString() }));
2018-08-23 17:35:27 +03:00
});
it('generates an id for every provided server if they do not have it', () => {
const servers = values(list).map(dissoc('id'));
const { payload } = createServers(servers);
expect(values(payload).every(({ id }) => !!id)).toEqual(true);
});
});
describe('setAutoConnect', () => {
it.each([
2022-03-26 14:17:42 +03:00
[true],
[false],
])('returns expected action', (autoConnect) => {
const serverToEdit = Mock.of<RegularServer>({ id: 'abc123' });
const result = setAutoConnect(serverToEdit, autoConnect);
expect(result).toEqual({
2022-11-05 12:08:24 +03:00
type: setAutoConnect.toString(),
payload: { serverId: 'abc123', autoConnect },
});
});
2018-08-23 17:35:27 +03:00
});
2018-08-12 11:17:13 +03:00
});
});