2018-08-12 11:17:13 +03:00
|
|
|
import reduce, {
|
|
|
|
_createServer,
|
|
|
|
_deleteServer,
|
|
|
|
_listServers,
|
2018-08-23 17:35:27 +03:00
|
|
|
_createServers,
|
2018-08-12 11:17:13 +03:00
|
|
|
FETCH_SERVERS,
|
|
|
|
} from '../../../src/servers/reducers/server';
|
|
|
|
import * as sinon from 'sinon';
|
2018-08-23 17:35:27 +03:00
|
|
|
import { values } from 'ramda';
|
2018-08-12 11:17:13 +03:00
|
|
|
|
|
|
|
describe('serverReducer', () => {
|
|
|
|
const servers = {
|
|
|
|
abc123: { id: 'abc123' },
|
|
|
|
def456: { id: 'def456' }
|
|
|
|
};
|
|
|
|
const ServersServiceMock = {
|
|
|
|
listServers: sinon.fake.returns(servers),
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
describe('reduce', () => {
|
|
|
|
it('returns servers when action is FETCH_SERVERS', () =>
|
|
|
|
expect(reduce({}, { type: FETCH_SERVERS, servers })).toEqual(servers)
|
|
|
|
);
|
|
|
|
|
|
|
|
it('returns default when action is unknown', () =>
|
|
|
|
expect(reduce({}, { type: 'unknown' })).toEqual({})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
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', () => {
|
|
|
|
const result = _listServers(ServersServiceMock);
|
|
|
|
|
|
|
|
expect(result).toEqual({ type: FETCH_SERVERS, servers });
|
|
|
|
expect(ServersServiceMock.listServers.callCount).toEqual(1);
|
|
|
|
expect(ServersServiceMock.createServer.callCount).toEqual(0);
|
|
|
|
expect(ServersServiceMock.deleteServer.callCount).toEqual(0);
|
2018-08-23 17:35:27 +03:00
|
|
|
expect(ServersServiceMock.createServers.callCount).toEqual(0);
|
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, serverToCreate);
|
|
|
|
|
|
|
|
expect(result).toEqual({ type: FETCH_SERVERS, servers });
|
|
|
|
expect(ServersServiceMock.listServers.callCount).toEqual(1);
|
|
|
|
expect(ServersServiceMock.createServer.callCount).toEqual(1);
|
|
|
|
expect(ServersServiceMock.createServer.firstCall.calledWith(serverToCreate)).toEqual(true);
|
|
|
|
expect(ServersServiceMock.deleteServer.callCount).toEqual(0);
|
2018-08-23 17:35:27 +03:00
|
|
|
expect(ServersServiceMock.createServers.callCount).toEqual(0);
|
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, serverToDelete);
|
|
|
|
|
|
|
|
expect(result).toEqual({ type: FETCH_SERVERS, servers });
|
|
|
|
expect(ServersServiceMock.listServers.callCount).toEqual(1);
|
|
|
|
expect(ServersServiceMock.createServer.callCount).toEqual(0);
|
2018-08-23 17:35:27 +03:00
|
|
|
expect(ServersServiceMock.createServers.callCount).toEqual(0);
|
2018-08-12 11:17:13 +03:00
|
|
|
expect(ServersServiceMock.deleteServer.callCount).toEqual(1);
|
|
|
|
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', () => {
|
|
|
|
const serversToCreate = values(servers);
|
|
|
|
const result = _createServers(ServersServiceMock, serversToCreate);
|
|
|
|
|
|
|
|
expect(result).toEqual({ type: FETCH_SERVERS, servers });
|
|
|
|
expect(ServersServiceMock.listServers.callCount).toEqual(1);
|
|
|
|
expect(ServersServiceMock.createServer.callCount).toEqual(0);
|
|
|
|
expect(ServersServiceMock.createServers.callCount).toEqual(1);
|
|
|
|
expect(ServersServiceMock.createServers.firstCall.calledWith(serversToCreate)).toEqual(true);
|
|
|
|
expect(ServersServiceMock.deleteServer.callCount).toEqual(0);
|
|
|
|
});
|
|
|
|
});
|
2018-08-12 11:17:13 +03:00
|
|
|
});
|
|
|
|
});
|