2018-08-26 00:39:27 +03:00
|
|
|
import { values } from 'ramda';
|
2018-08-26 11:52:45 +03:00
|
|
|
import reducer, {
|
2018-12-18 21:45:09 +03:00
|
|
|
createServer,
|
|
|
|
deleteServer,
|
|
|
|
listServers,
|
|
|
|
createServers,
|
2020-04-25 11:22:17 +03:00
|
|
|
editServer,
|
2020-04-27 11:52:19 +03:00
|
|
|
LIST_SERVERS,
|
|
|
|
} from '../../../src/servers/reducers/servers';
|
2018-08-12 11:17:13 +03:00
|
|
|
|
|
|
|
describe('serverReducer', () => {
|
2019-04-28 13:40:50 +03:00
|
|
|
const list = {
|
2018-08-12 11:17:13 +03:00
|
|
|
abc123: { id: 'abc123' },
|
2018-08-26 00:39:27 +03:00
|
|
|
def456: { id: 'def456' },
|
2018-08-12 11:17:13 +03:00
|
|
|
};
|
2020-04-27 11:52:19 +03:00
|
|
|
const expectedFetchServersResult = { type: LIST_SERVERS, list };
|
2018-08-12 11:17:13 +03:00
|
|
|
const ServersServiceMock = {
|
2019-04-28 13:40:50 +03:00
|
|
|
listServers: jest.fn(() => list),
|
2019-04-19 11:29:49 +03:00
|
|
|
createServer: jest.fn(),
|
2020-03-15 13:29:20 +03:00
|
|
|
editServer: jest.fn(),
|
2019-04-19 11:29:49 +03:00
|
|
|
deleteServer: jest.fn(),
|
|
|
|
createServers: jest.fn(),
|
2018-08-12 11:17:13 +03:00
|
|
|
};
|
|
|
|
|
2019-10-21 20:26:09 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
|
2018-08-26 11:52:45 +03:00
|
|
|
describe('reducer', () => {
|
2020-04-27 11:52:19 +03:00
|
|
|
it('returns servers when action is LIST_SERVERS', () =>
|
|
|
|
expect(reducer({}, { type: LIST_SERVERS, list })).toEqual(list));
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('action creators', () => {
|
|
|
|
describe('listServers', () => {
|
2019-10-21 20:26:09 +03:00
|
|
|
const axios = { get: jest.fn() };
|
2019-04-28 13:40:50 +03:00
|
|
|
const dispatch = jest.fn();
|
2019-10-21 20:26:09 +03:00
|
|
|
const NoListServersServiceMock = { ...ServersServiceMock, listServers: jest.fn(() => ({})) };
|
2019-04-28 13:40:50 +03:00
|
|
|
|
|
|
|
it('fetches servers from local storage when found', async () => {
|
|
|
|
await listServers(ServersServiceMock, axios)()(dispatch);
|
|
|
|
|
2020-04-27 11:49:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(1);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, expectedFetchServersResult);
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(ServersServiceMock.listServers).toHaveBeenCalledTimes(1);
|
|
|
|
expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
|
2020-03-15 13:29:20 +03:00
|
|
|
expect(ServersServiceMock.editServer).not.toHaveBeenCalled();
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
|
|
|
|
expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
|
2019-04-28 13:40:50 +03:00
|
|
|
expect(axios.get).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2020-02-17 20:21:52 +03:00
|
|
|
it.each([
|
2019-10-21 20:26:09 +03:00
|
|
|
[
|
|
|
|
Promise.resolve({
|
2019-10-21 20:38:32 +03:00
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: '111',
|
|
|
|
name: 'acel.me from servers.json',
|
|
|
|
url: 'https://acel.me',
|
|
|
|
apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '222',
|
|
|
|
name: 'Local from servers.json',
|
|
|
|
url: 'http://localhost:8000',
|
|
|
|
apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a',
|
|
|
|
},
|
|
|
|
],
|
2019-10-21 20:26:09 +03:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
111: {
|
|
|
|
id: '111',
|
|
|
|
name: 'acel.me from servers.json',
|
|
|
|
url: 'https://acel.me',
|
|
|
|
apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0',
|
|
|
|
},
|
|
|
|
222: {
|
|
|
|
id: '222',
|
|
|
|
name: 'Local from servers.json',
|
|
|
|
url: 'http://localhost:8000',
|
|
|
|
apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[ Promise.resolve('<html></html>'), {}],
|
|
|
|
[ Promise.reject({}), {}],
|
2020-02-17 20:21:52 +03:00
|
|
|
])('tries to fetch servers from remote when not found locally', async (mockedValue, expectedList) => {
|
2019-10-21 20:26:09 +03:00
|
|
|
axios.get.mockReturnValue(mockedValue);
|
2019-04-28 13:40:50 +03:00
|
|
|
|
|
|
|
await listServers(NoListServersServiceMock, axios)()(dispatch);
|
|
|
|
|
2020-04-27 11:49:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(1);
|
2020-04-27 11:52:19 +03:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SERVERS, list: expectedList });
|
2019-04-28 13:40:50 +03:00
|
|
|
expect(NoListServersServiceMock.listServers).toHaveBeenCalledTimes(1);
|
|
|
|
expect(NoListServersServiceMock.createServer).not.toHaveBeenCalled();
|
2020-03-15 13:29:20 +03:00
|
|
|
expect(NoListServersServiceMock.editServer).not.toHaveBeenCalled();
|
2019-04-28 13:40:50 +03:00
|
|
|
expect(NoListServersServiceMock.deleteServer).not.toHaveBeenCalled();
|
|
|
|
expect(NoListServersServiceMock.createServers).toHaveBeenCalledTimes(1);
|
|
|
|
expect(axios.get).toHaveBeenCalledTimes(1);
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createServer', () => {
|
|
|
|
it('adds new server and then fetches servers again', () => {
|
|
|
|
const serverToCreate = { id: 'abc123' };
|
2019-01-13 01:59:03 +03:00
|
|
|
const result = createServer(ServersServiceMock, () => expectedFetchServersResult)(serverToCreate);
|
2018-08-12 11:17:13 +03:00
|
|
|
|
2019-01-13 01:59:03 +03:00
|
|
|
expect(result).toEqual(expectedFetchServersResult);
|
2020-03-15 13:29:20 +03:00
|
|
|
expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(ServersServiceMock.createServer).toHaveBeenCalledTimes(1);
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(ServersServiceMock.createServer).toHaveBeenCalledWith(serverToCreate);
|
2020-03-15 13:29:20 +03:00
|
|
|
expect(ServersServiceMock.editServer).not.toHaveBeenCalled();
|
|
|
|
expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
|
|
|
|
expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('editServer', () => {
|
|
|
|
it('edits existing server and then fetches servers again', () => {
|
|
|
|
const serverToEdit = { name: 'edited' };
|
|
|
|
const result = editServer(ServersServiceMock, () => expectedFetchServersResult)('123', serverToEdit);
|
|
|
|
|
|
|
|
expect(result).toEqual(expectedFetchServersResult);
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
|
2020-03-15 13:29:20 +03:00
|
|
|
expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
|
|
|
|
expect(ServersServiceMock.editServer).toHaveBeenCalledTimes(1);
|
|
|
|
expect(ServersServiceMock.editServer).toHaveBeenCalledWith('123', serverToEdit);
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
|
|
|
|
expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deleteServer', () => {
|
|
|
|
it('deletes a server and then fetches servers again', () => {
|
|
|
|
const serverToDelete = { id: 'abc123' };
|
2019-01-13 01:59:03 +03:00
|
|
|
const result = deleteServer(ServersServiceMock, () => expectedFetchServersResult)(serverToDelete);
|
2018-08-12 11:17:13 +03:00
|
|
|
|
2019-01-13 01:59:03 +03:00
|
|
|
expect(result).toEqual(expectedFetchServersResult);
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
|
|
|
|
expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
|
|
|
|
expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
|
2020-03-15 13:29:20 +03:00
|
|
|
expect(ServersServiceMock.editServer).not.toHaveBeenCalled();
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(ServersServiceMock.deleteServer).toHaveBeenCalledTimes(1);
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(ServersServiceMock.deleteServer).toHaveBeenCalledWith(serverToDelete);
|
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', () => {
|
2018-08-23 17:35:27 +03:00
|
|
|
it('creates multiple servers and then fetches servers again', () => {
|
2019-04-28 13:40:50 +03:00
|
|
|
const serversToCreate = values(list);
|
2019-01-13 01:59:03 +03:00
|
|
|
const result = createServers(ServersServiceMock, () => expectedFetchServersResult)(serversToCreate);
|
2018-08-23 17:35:27 +03:00
|
|
|
|
2019-01-13 01:59:03 +03:00
|
|
|
expect(result).toEqual(expectedFetchServersResult);
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
|
|
|
|
expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
|
2020-03-15 13:29:20 +03:00
|
|
|
expect(ServersServiceMock.editServer).not.toHaveBeenCalled();
|
|
|
|
expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(ServersServiceMock.createServers).toHaveBeenCalledTimes(1);
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(ServersServiceMock.createServers).toHaveBeenCalledWith(serversToCreate);
|
2018-08-23 17:35:27 +03:00
|
|
|
});
|
|
|
|
});
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
});
|