2018-08-26 00:39:27 +03:00
|
|
|
import * as sinon from 'sinon';
|
|
|
|
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,
|
2018-08-12 11:17:13 +03:00
|
|
|
FETCH_SERVERS,
|
|
|
|
} from '../../../src/servers/reducers/server';
|
|
|
|
|
|
|
|
describe('serverReducer', () => {
|
2019-03-17 11:06:10 +03:00
|
|
|
const payload = {
|
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
|
|
|
};
|
2019-03-17 11:06:10 +03:00
|
|
|
const expectedFetchServersResult = { type: FETCH_SERVERS, payload };
|
2018-08-12 11:17:13 +03:00
|
|
|
const ServersServiceMock = {
|
2019-03-17 11:06:10 +03:00
|
|
|
listServers: sinon.fake.returns(payload),
|
2018-08-12 11:17:13 +03:00
|
|
|
createServer: sinon.fake(),
|
|
|
|
deleteServer: sinon.fake(),
|
2018-08-23 17:35:27 +03:00
|
|
|
createServers: sinon.fake(),
|
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-03-17 11:06:10 +03:00
|
|
|
expect(reducer({}, { type: FETCH_SERVERS, payload })).toEqual(payload));
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('action creators', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
ServersServiceMock.listServers.resetHistory();
|
|
|
|
ServersServiceMock.createServer.resetHistory();
|
|
|
|
ServersServiceMock.deleteServer.resetHistory();
|
2018-08-23 17:35:27 +03:00
|
|
|
ServersServiceMock.createServers.resetHistory();
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('listServers', () => {
|
|
|
|
it('fetches servers and returns them as part of the action', () => {
|
2018-12-18 21:45:09 +03:00
|
|
|
const result = listServers(ServersServiceMock)();
|
2018-08-12 11:17:13 +03:00
|
|
|
|
2019-01-13 01:59:03 +03:00
|
|
|
expect(result).toEqual(expectedFetchServersResult);
|
|
|
|
expect(ServersServiceMock.listServers.calledOnce).toEqual(true);
|
|
|
|
expect(ServersServiceMock.createServer.called).toEqual(false);
|
|
|
|
expect(ServersServiceMock.deleteServer.called).toEqual(false);
|
|
|
|
expect(ServersServiceMock.createServers.called).toEqual(false);
|
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);
|
|
|
|
expect(ServersServiceMock.createServer.calledOnce).toEqual(true);
|
2018-08-12 11:17:13 +03:00
|
|
|
expect(ServersServiceMock.createServer.firstCall.calledWith(serverToCreate)).toEqual(true);
|
2019-01-13 01:59:03 +03:00
|
|
|
expect(ServersServiceMock.listServers.called).toEqual(false);
|
|
|
|
expect(ServersServiceMock.deleteServer.called).toEqual(false);
|
|
|
|
expect(ServersServiceMock.createServers.called).toEqual(false);
|
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);
|
|
|
|
expect(ServersServiceMock.listServers.called).toEqual(false);
|
|
|
|
expect(ServersServiceMock.createServer.called).toEqual(false);
|
|
|
|
expect(ServersServiceMock.createServers.called).toEqual(false);
|
|
|
|
expect(ServersServiceMock.deleteServer.calledOnce).toEqual(true);
|
2018-08-12 11:17:13 +03:00
|
|
|
expect(ServersServiceMock.deleteServer.firstCall.calledWith(serverToDelete)).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
2018-08-23 17:35:27 +03:00
|
|
|
|
|
|
|
describe('createServer', () => {
|
|
|
|
it('creates multiple servers and then fetches servers again', () => {
|
2019-03-17 11:06:10 +03:00
|
|
|
const serversToCreate = values(payload);
|
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);
|
|
|
|
expect(ServersServiceMock.listServers.called).toEqual(false);
|
|
|
|
expect(ServersServiceMock.createServer.called).toEqual(false);
|
|
|
|
expect(ServersServiceMock.createServers.calledOnce).toEqual(true);
|
2018-08-23 17:35:27 +03:00
|
|
|
expect(ServersServiceMock.createServers.firstCall.calledWith(serversToCreate)).toEqual(true);
|
2019-01-13 01:59:03 +03:00
|
|
|
expect(ServersServiceMock.deleteServer.called).toEqual(false);
|
2018-08-23 17:35:27 +03:00
|
|
|
});
|
|
|
|
});
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
});
|