Created ServersService test

This commit is contained in:
Alejandro Celaya 2018-08-24 10:58:37 +02:00
parent 53a4240219
commit 76ae27707b
4 changed files with 134 additions and 21 deletions

View file

@ -1,5 +1,5 @@
import ShlinkApiClient from '../../api/ShlinkApiClient';
import ServersService from '../../servers/services/ServersService';
import serversService from '../../servers/services/ServersService';
import { resetShortUrlParams } from '../../short-urls/reducers/shortUrlsListParams'
import { curry } from 'ramda';
@ -21,10 +21,10 @@ export default function reducer(state = defaultState, action) {
export const resetSelectedServer = () => ({ type: RESET_SELECTED_SERVER });
export const _selectServer = (ShlinkApiClient, ServersService, serverId) => dispatch => {
export const _selectServer = (ShlinkApiClient, serversService, serverId) => dispatch => {
dispatch(resetShortUrlParams());
const selectedServer = ServersService.findServerById(serverId);
const selectedServer = serversService.findServerById(serverId);
ShlinkApiClient.setConfig(selectedServer);
dispatch({
@ -32,4 +32,4 @@ export const _selectServer = (ShlinkApiClient, ServersService, serverId) => disp
selectedServer
})
};
export const selectServer = curry(_selectServer)(ShlinkApiClient, ServersService);
export const selectServer = curry(_selectServer)(ShlinkApiClient, serversService);

View file

@ -1,4 +1,4 @@
import ServersService from '../services/ServersService';
import serversService from '../services/ServersService';
import { curry } from 'ramda';
export const FETCH_SERVERS = 'shlink/servers/FETCH_SERVERS';
@ -12,26 +12,26 @@ export default function reducer(state = {}, action) {
}
}
export const _listServers = ServersService => ({
export const _listServers = serversService => ({
type: FETCH_SERVERS,
servers: ServersService.listServers(),
servers: serversService.listServers(),
});
export const listServers = () => _listServers(ServersService);
export const listServers = () => _listServers(serversService);
export const _createServer = (ServersService, server) => {
ServersService.createServer(server);
return _listServers(ServersService);
export const _createServer = (serversService, server) => {
serversService.createServer(server);
return _listServers(serversService);
};
export const createServer = curry(_createServer)(ServersService);
export const createServer = curry(_createServer)(serversService);
export const _deleteServer = (ServersService, server) => {
ServersService.deleteServer(server);
return _listServers(ServersService);
export const _deleteServer = (serversService, server) => {
serversService.deleteServer(server);
return _listServers(serversService);
};
export const deleteServer = curry(_deleteServer)(ServersService);
export const deleteServer = curry(_deleteServer)(serversService);
export const _createServers = (ServersService, servers) => {
ServersService.createServers(servers);
return _listServers(ServersService);
export const _createServers = (serversService, servers) => {
serversService.createServers(servers);
return _listServers(serversService);
};
export const createServers = curry(_createServers)(ServersService);
export const createServers = curry(_createServers)(serversService);

View file

@ -30,4 +30,5 @@ export class ServersService {
);
}
export default new ServersService(Storage);
const serversService = new ServersService(Storage);
export default serversService;

View file

@ -0,0 +1,112 @@
import { ServersService } from '../../../src/servers/services/ServersService';
import sinon from 'sinon';
import { last } from 'ramda';
describe('ServersService', () => {
const servers = {
abc123: { id: 'abc123' },
def456: { id: 'def456' },
};
const createStorageMock = returnValue => ({
set: sinon.fake(),
get: sinon.fake.returns(returnValue),
});
describe('listServers', () => {
it('returns an empty object when servers are not found in storage', () => {
const storageMock = createStorageMock();
const service = new ServersService(storageMock);
const result = service.listServers();
expect(result).toEqual({});
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(0);
});
it('returns value from storage when found', () => {
const storageMock = createStorageMock(servers);
const service = new ServersService(storageMock);
const result = service.listServers();
expect(result).toEqual(servers);
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(0);
});
});
describe('findServerById', () => {
it('returns undefined when requested server is not found', () => {
const storageMock = createStorageMock(servers);
const service = new ServersService(storageMock);
const result = service.findServerById('ghi789');
expect(result).toBeUndefined();
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(0);
});
it('returns server from list when found', () => {
const storageMock = createStorageMock(servers);
const service = new ServersService(storageMock);
const result = service.findServerById('abc123');
expect(result).toEqual({ id: 'abc123' });
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(0);
});
});
describe('createServer', () => {
it('adds one server to the list', () => {
const storageMock = createStorageMock(servers);
const service = new ServersService(storageMock);
service.createServer({ id: 'ghi789' });
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(1);
expect(last(storageMock.set.lastCall.args)).toEqual({
abc123: { id: 'abc123' },
def456: { id: 'def456' },
ghi789: { id: 'ghi789' },
});
});
});
describe('createServers', () => {
it('adds multiple servers to the list', () => {
const storageMock = createStorageMock(servers);
const service = new ServersService(storageMock);
service.createServers([{ id: 'ghi789' }, { id: 'jkl123' }]);
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(1);
expect(last(storageMock.set.lastCall.args)).toEqual({
abc123: { id: 'abc123' },
def456: { id: 'def456' },
ghi789: { id: 'ghi789' },
jkl123: { id: 'jkl123' },
});
});
});
describe('deleteServer', () => {
it('removes one server from the list', () => {
const storageMock = createStorageMock(servers);
const service = new ServersService(storageMock);
service.deleteServer({ id: 'abc123' });
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(1);
expect(last(storageMock.set.lastCall.args)).toEqual({
def456: { id: 'def456' },
});
});
});
});