shlink-web-client/test/servers/reducers/server.test.js

117 lines
4.9 KiB
JavaScript
Raw Normal View History

import { values } from 'ramda';
2018-08-26 11:52:45 +03:00
import reducer, {
createServer,
deleteServer,
listServers,
createServers,
2019-04-28 13:40:50 +03:00
FETCH_SERVERS, FETCH_SERVERS_START,
2018-08-12 11:17:13 +03:00
} from '../../../src/servers/reducers/server';
describe('serverReducer', () => {
2019-04-28 13:40:50 +03:00
const list = {
2018-08-12 11:17:13 +03:00
abc123: { id: 'abc123' },
def456: { id: 'def456' },
2018-08-12 11:17:13 +03:00
};
2019-04-28 13:40:50 +03:00
const expectedFetchServersResult = { type: FETCH_SERVERS, list };
2018-08-12 11:17:13 +03:00
const ServersServiceMock = {
2019-04-28 13:40:50 +03:00
listServers: jest.fn(() => list),
createServer: jest.fn(),
deleteServer: jest.fn(),
createServers: jest.fn(),
2018-08-12 11:17:13 +03:00
};
2018-08-26 11:52:45 +03:00
describe('reducer', () => {
2018-08-12 11:17:13 +03:00
it('returns servers when action is FETCH_SERVERS', () =>
2019-04-28 13:40:50 +03:00
expect(reducer({}, { type: FETCH_SERVERS, list })).toEqual({ loading: false, list }));
2018-08-12 11:17:13 +03:00
});
describe('action creators', () => {
beforeEach(() => {
ServersServiceMock.listServers.mockClear();
ServersServiceMock.createServer.mockReset();
ServersServiceMock.deleteServer.mockReset();
ServersServiceMock.createServers.mockReset();
2018-08-12 11:17:13 +03:00
});
describe('listServers', () => {
2019-04-28 13:40:50 +03:00
const axios = { get: jest.fn().mockResolvedValue({ data: [] }) };
const dispatch = jest.fn();
2018-08-12 11:17:13 +03:00
2019-04-28 13:40:50 +03:00
beforeEach(() => {
axios.get.mockClear();
dispatch.mockReset();
});
it('fetches servers from local storage when found', async () => {
await listServers(ServersServiceMock, axios)()(dispatch);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: FETCH_SERVERS_START });
expect(dispatch).toHaveBeenNthCalledWith(2, expectedFetchServersResult);
expect(ServersServiceMock.listServers).toHaveBeenCalledTimes(1);
expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
2019-04-28 13:40:50 +03:00
expect(axios.get).not.toHaveBeenCalled();
});
it('tries to fetch servers from remote when not found locally', async () => {
const NoListServersServiceMock = { ...ServersServiceMock, listServers: jest.fn(() => ({})) };
await listServers(NoListServersServiceMock, axios)()(dispatch);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: FETCH_SERVERS_START });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: FETCH_SERVERS, list: {} });
expect(NoListServersServiceMock.listServers).toHaveBeenCalledTimes(1);
expect(NoListServersServiceMock.createServer).not.toHaveBeenCalled();
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' };
const result = createServer(ServersServiceMock, () => expectedFetchServersResult)(serverToCreate);
2018-08-12 11:17:13 +03:00
expect(result).toEqual(expectedFetchServersResult);
expect(ServersServiceMock.createServer).toHaveBeenCalledTimes(1);
expect(ServersServiceMock.createServer).toHaveBeenCalledWith(serverToCreate);
expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
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' };
const result = deleteServer(ServersServiceMock, () => expectedFetchServersResult)(serverToDelete);
2018-08-12 11:17:13 +03:00
expect(result).toEqual(expectedFetchServersResult);
expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
expect(ServersServiceMock.deleteServer).toHaveBeenCalledTimes(1);
expect(ServersServiceMock.deleteServer).toHaveBeenCalledWith(serverToDelete);
2018-08-12 11:17:13 +03:00
});
});
2018-08-23 17:35:27 +03:00
describe('createServer', () => {
it('creates multiple servers and then fetches servers again', () => {
2019-04-28 13:40:50 +03:00
const serversToCreate = values(list);
const result = createServers(ServersServiceMock, () => expectedFetchServersResult)(serversToCreate);
2018-08-23 17:35:27 +03:00
expect(result).toEqual(expectedFetchServersResult);
expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
expect(ServersServiceMock.createServers).toHaveBeenCalledTimes(1);
expect(ServersServiceMock.createServers).toHaveBeenCalledWith(serversToCreate);
expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
2018-08-23 17:35:27 +03:00
});
});
2018-08-12 11:17:13 +03:00
});
});