From 76ae27707bacee6a27c6fb8dd5da921c2155a951 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 24 Aug 2018 10:58:37 +0200 Subject: [PATCH] Created ServersService test --- src/servers/reducers/selectedServer.js | 8 +- src/servers/reducers/server.js | 32 +++--- src/servers/services/ServersService.js | 3 +- test/servers/services/ServersService.test.js | 112 +++++++++++++++++++ 4 files changed, 134 insertions(+), 21 deletions(-) create mode 100644 test/servers/services/ServersService.test.js diff --git a/src/servers/reducers/selectedServer.js b/src/servers/reducers/selectedServer.js index 3ed83fa6..1fc9b2e5 100644 --- a/src/servers/reducers/selectedServer.js +++ b/src/servers/reducers/selectedServer.js @@ -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); diff --git a/src/servers/reducers/server.js b/src/servers/reducers/server.js index adc6e895..978c82fd 100644 --- a/src/servers/reducers/server.js +++ b/src/servers/reducers/server.js @@ -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); diff --git a/src/servers/services/ServersService.js b/src/servers/services/ServersService.js index 35f7f9b5..a8b96b8f 100644 --- a/src/servers/services/ServersService.js +++ b/src/servers/services/ServersService.js @@ -30,4 +30,5 @@ export class ServersService { ); } -export default new ServersService(Storage); +const serversService = new ServersService(Storage); +export default serversService; diff --git a/test/servers/services/ServersService.test.js b/test/servers/services/ServersService.test.js new file mode 100644 index 00000000..b07db206 --- /dev/null +++ b/test/servers/services/ServersService.test.js @@ -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' }, + }); + }); + }); +});