From a7f941e8e4c7dd84b0f06eb4143bd2caacf4a7b0 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 27 Apr 2020 13:21:07 +0200 Subject: [PATCH] Deleted no-longer-needed ServersService --- src/servers/reducers/selectedServer.js | 9 +- src/servers/services/ServersExporter.js | 6 +- src/servers/services/ServersService.js | 38 ----- src/servers/services/provideServices.js | 6 +- test/servers/reducers/selectedServer.test.js | 27 ++-- test/servers/services/ServersExporter.test.js | 28 ++-- test/servers/services/ServersService.test.js | 130 ------------------ 7 files changed, 35 insertions(+), 209 deletions(-) delete mode 100644 src/servers/services/ServersService.js delete mode 100644 test/servers/services/ServersService.test.js diff --git a/src/servers/reducers/selectedServer.js b/src/servers/reducers/selectedServer.js index 64d94ae1..835df2a7 100644 --- a/src/servers/reducers/selectedServer.js +++ b/src/servers/reducers/selectedServer.js @@ -25,12 +25,15 @@ const getServerVersion = memoizeWith(identity, (serverId, health) => health().th export const resetSelectedServer = createAction(RESET_SELECTED_SERVER); -export const selectServer = ({ findServerById }, buildShlinkApiClient, loadMercureInfo) => (serverId) => async ( - dispatch +export const selectServer = (buildShlinkApiClient, loadMercureInfo) => (serverId) => async ( + dispatch, + getState ) => { dispatch(resetSelectedServer()); dispatch(resetShortUrlParams()); - const selectedServer = findServerById(serverId); + + const { servers } = getState(); + const selectedServer = servers[serverId]; if (!selectedServer) { dispatch({ diff --git a/src/servers/services/ServersExporter.js b/src/servers/services/ServersExporter.js index 9caf5ab9..01a33316 100644 --- a/src/servers/services/ServersExporter.js +++ b/src/servers/services/ServersExporter.js @@ -25,14 +25,14 @@ const saveCsv = (window, csv) => { }; export default class ServersExporter { - constructor(serversService, window, csvjson) { - this.serversService = serversService; + constructor(storage, window, csvjson) { + this.storage = storage; this.window = window; this.csvjson = csvjson; } exportServers = async () => { - const servers = values(this.serversService.listServers()).map(dissoc('id')); + const servers = values(this.storage.get('servers') || {}).map(dissoc('id')); try { const csv = this.csvjson.toCSV(servers, { diff --git a/src/servers/services/ServersService.js b/src/servers/services/ServersService.js deleted file mode 100644 index 9ec44afa..00000000 --- a/src/servers/services/ServersService.js +++ /dev/null @@ -1,38 +0,0 @@ -import { assoc, dissoc, reduce } from 'ramda'; - -const SERVERS_STORAGE_KEY = 'servers'; - -export default class ServersService { - constructor(storage) { - this.storage = storage; - } - - listServers = () => this.storage.get(SERVERS_STORAGE_KEY) || {}; - - findServerById = (serverId) => this.listServers()[serverId]; - - createServer = (server) => this.createServers([ server ]); - - createServers = (servers) => { - const allServers = reduce( - (serversObj, server) => assoc(server.id, server, serversObj), - this.listServers(), - servers - ); - - this.storage.set(SERVERS_STORAGE_KEY, allServers); - }; - - deleteServer = ({ id }) => - this.storage.set(SERVERS_STORAGE_KEY, dissoc(id, this.listServers())); - - editServer = (id, serverData) => { - const allServers = this.listServers(); - - if (!allServers[id]) { - return; - } - - this.storage.set(SERVERS_STORAGE_KEY, assoc(id, { ...allServers[id], ...serverData }, allServers)); - } -} diff --git a/src/servers/services/provideServices.js b/src/servers/services/provideServices.js index 9168bc52..a431d363 100644 --- a/src/servers/services/provideServices.js +++ b/src/servers/services/provideServices.js @@ -11,7 +11,6 @@ import { fetchServers } from '../reducers/remoteServers'; import ForServerVersion from '../helpers/ForServerVersion'; import { ServerError } from '../helpers/ServerError'; import ServersImporter from './ServersImporter'; -import ServersService from './ServersService'; import ServersExporter from './ServersExporter'; const provideServices = (bottle, connect, withRouter) => { @@ -44,11 +43,10 @@ const provideServices = (bottle, connect, withRouter) => { // Services bottle.constant('csvjson', csvjson); bottle.service('ServersImporter', ServersImporter, 'csvjson'); - bottle.service('ServersService', ServersService, 'Storage'); - bottle.service('ServersExporter', ServersExporter, 'ServersService', 'window', 'csvjson'); + bottle.service('ServersExporter', ServersExporter, 'Storage', 'window', 'csvjson'); // Actions - bottle.serviceFactory('selectServer', selectServer, 'ServersService', 'buildShlinkApiClient', 'loadMercureInfo'); + bottle.serviceFactory('selectServer', selectServer, 'buildShlinkApiClient', 'loadMercureInfo'); bottle.serviceFactory('createServer', () => createServer); bottle.serviceFactory('createServers', () => createServers); bottle.serviceFactory('deleteServer', () => deleteServer); diff --git a/test/servers/reducers/selectedServer.test.js b/test/servers/reducers/selectedServer.test.js index 2efd8291..9307841f 100644 --- a/test/servers/reducers/selectedServer.test.js +++ b/test/servers/reducers/selectedServer.test.js @@ -32,9 +32,7 @@ describe('selectedServerReducer', () => { id: 'abc123', }; const version = '1.19.0'; - const ServersServiceMock = { - findServerById: jest.fn(() => selectedServer), - }; + const createGetStateMock = (id) => jest.fn().mockReturnValue({ servers: { [id]: selectedServer } }); const apiClientMock = { health: jest.fn(), }; @@ -49,6 +47,8 @@ describe('selectedServerReducer', () => { [ 'latest', MAX_FALLBACK_VERSION, 'latest' ], [ '%invalid_semver%', MIN_FALLBACK_VERSION, '%invalid_semver%' ], ])('dispatches proper actions', async (serverVersion, expectedVersion, expectedPrintableVersion) => { + const id = uuid(); + const getState = createGetStateMock(id); const expectedSelectedServer = { ...selectedServer, version: expectedVersion, @@ -57,7 +57,7 @@ describe('selectedServerReducer', () => { apiClientMock.health.mockResolvedValue({ version: serverVersion }); - await selectServer(ServersServiceMock, buildApiClient, loadMercureInfo)(uuid())(dispatch); + await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState); expect(dispatch).toHaveBeenCalledTimes(4); expect(dispatch).toHaveBeenNthCalledWith(1, { type: RESET_SELECTED_SERVER }); @@ -67,18 +67,23 @@ describe('selectedServerReducer', () => { }); it('invokes dependencies', async () => { - await selectServer(ServersServiceMock, buildApiClient, loadMercureInfo)(uuid())(() => {}); + const id = uuid(); + const getState = createGetStateMock(id); - expect(ServersServiceMock.findServerById).toHaveBeenCalledTimes(1); + await selectServer(buildApiClient, loadMercureInfo)(id)(() => {}, getState); + + expect(getState).toHaveBeenCalledTimes(1); expect(buildApiClient).toHaveBeenCalledTimes(1); }); it('dispatches error when health endpoint fails', async () => { + const id = uuid(); + const getState = createGetStateMock(id); const expectedSelectedServer = { ...selectedServer, serverNotReachable: true }; apiClientMock.health.mockRejectedValue({}); - await selectServer(ServersServiceMock, buildApiClient, loadMercureInfo)(uuid())(dispatch); + await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState); expect(apiClientMock.health).toHaveBeenCalled(); expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer }); @@ -86,13 +91,13 @@ describe('selectedServerReducer', () => { }); it('dispatches error when server is not found', async () => { + const id = uuid(); + const getState = jest.fn(() => ({ servers: {} })); const expectedSelectedServer = { serverNotFound: true }; - ServersServiceMock.findServerById.mockReturnValue(undefined); + await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState); - await selectServer(ServersServiceMock, buildApiClient, loadMercureInfo)(uuid())(dispatch); - - expect(ServersServiceMock.findServerById).toHaveBeenCalled(); + expect(getState).toHaveBeenCalled(); expect(apiClientMock.health).not.toHaveBeenCalled(); expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer }); expect(loadMercureInfo).not.toHaveBeenCalled(); diff --git a/test/servers/services/ServersExporter.test.js b/test/servers/services/ServersExporter.test.js index 772df918..1dc2156e 100644 --- a/test/servers/services/ServersExporter.test.js +++ b/test/servers/services/ServersExporter.test.js @@ -18,8 +18,8 @@ describe('ServersExporter', () => { }, }, }); - const serversServiceMock = { - listServers: jest.fn(() => ({ + const storageMock = { + get: jest.fn(() => ({ abc123: { id: 'abc123', name: 'foo', @@ -48,19 +48,15 @@ describe('ServersExporter', () => { global.console = { error: jest.fn() }; global.Blob = class Blob {}; global.URL = { createObjectURL: () => '' }; - serversServiceMock.listServers.mockReset(); }); afterEach(() => { global.console = originalConsole; + jest.clearAllMocks(); }); it('logs an error if something fails', () => { const csvjsonMock = createCsvjsonMock(true); - const exporter = new ServersExporter( - serversServiceMock, - createWindowMock(), - csvjsonMock, - ); + const exporter = new ServersExporter(storageMock, createWindowMock(), csvjsonMock); exporter.exportServers(); @@ -70,30 +66,22 @@ describe('ServersExporter', () => { it('makes use of msSaveBlob API when available', () => { const windowMock = createWindowMock(); - const exporter = new ServersExporter( - serversServiceMock, - windowMock, - createCsvjsonMock(), - ); + const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock()); exporter.exportServers(); - expect(serversServiceMock.listServers).toHaveBeenCalledTimes(1); + expect(storageMock.get).toHaveBeenCalledTimes(1); expect(windowMock.navigator.msSaveBlob).toHaveBeenCalledTimes(1); expect(windowMock.document.createElement).not.toHaveBeenCalled(); }); it('makes use of download link API when available', () => { const windowMock = createWindowMock(false); - const exporter = new ServersExporter( - serversServiceMock, - windowMock, - createCsvjsonMock(), - ); + const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock()); exporter.exportServers(); - expect(serversServiceMock.listServers).toHaveBeenCalledTimes(1); + expect(storageMock.get).toHaveBeenCalledTimes(1); expect(windowMock.document.createElement).toHaveBeenCalledTimes(1); expect(windowMock.document.body.appendChild).toHaveBeenCalledTimes(1); expect(windowMock.document.body.removeChild).toHaveBeenCalledTimes(1); diff --git a/test/servers/services/ServersService.test.js b/test/servers/services/ServersService.test.js deleted file mode 100644 index 3c8b805f..00000000 --- a/test/servers/services/ServersService.test.js +++ /dev/null @@ -1,130 +0,0 @@ -import ServersService from '../../../src/servers/services/ServersService'; - -describe('ServersService', () => { - const servers = { - abc123: { id: 'abc123' }, - def456: { id: 'def456' }, - }; - const createService = (withServers = true) => { - const storageMock = { - set: jest.fn(), - get: jest.fn(() => withServers ? servers : undefined), - }; - const service = new ServersService(storageMock); - - return [ service, storageMock ]; - }; - - describe('listServers', () => { - it('returns an empty object when servers are not found in storage', () => { - const [ service, storageMock ] = createService(false); - - const result = service.listServers(); - - expect(result).toEqual({}); - expect(storageMock.get).toHaveBeenCalledTimes(1); - expect(storageMock.set).not.toHaveBeenCalled(); - }); - - it('returns value from storage when found', () => { - const [ service, storageMock ] = createService(); - - const result = service.listServers(); - - expect(result).toEqual(servers); - expect(storageMock.get).toHaveBeenCalledTimes(1); - expect(storageMock.set).not.toHaveBeenCalled(); - }); - }); - - describe('findServerById', () => { - it('returns undefined when requested server is not found', () => { - const [ service, storageMock ] = createService(); - - const result = service.findServerById('ghi789'); - - expect(result).toBeUndefined(); - expect(storageMock.get).toHaveBeenCalledTimes(1); - expect(storageMock.set).not.toHaveBeenCalled(); - }); - - it('returns server from list when found', () => { - const [ service, storageMock ] = createService(); - - const result = service.findServerById('abc123'); - - expect(result).toEqual({ id: 'abc123' }); - expect(storageMock.get).toHaveBeenCalledTimes(1); - expect(storageMock.set).not.toHaveBeenCalled(); - }); - }); - - describe('createServer', () => { - it('adds one server to the list', () => { - const [ service, storageMock ] = createService(); - - service.createServer({ id: 'ghi789' }); - - expect(storageMock.get).toHaveBeenCalledTimes(1); - expect(storageMock.set).toHaveBeenCalledTimes(1); - expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), { - abc123: { id: 'abc123' }, - def456: { id: 'def456' }, - ghi789: { id: 'ghi789' }, - }); - }); - }); - - describe('createServers', () => { - it('adds multiple servers to the list', () => { - const [ service, storageMock ] = createService(); - - service.createServers([{ id: 'ghi789' }, { id: 'jkl123' }]); - - expect(storageMock.get).toHaveBeenCalledTimes(1); - expect(storageMock.set).toHaveBeenCalledTimes(1); - expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), { - abc123: { id: 'abc123' }, - def456: { id: 'def456' }, - ghi789: { id: 'ghi789' }, - jkl123: { id: 'jkl123' }, - }); - }); - }); - - describe('deleteServer', () => { - it('removes one server from the list', () => { - const [ service, storageMock ] = createService(); - - service.deleteServer({ id: 'abc123' }); - - expect(storageMock.get).toHaveBeenCalledTimes(1); - expect(storageMock.set).toHaveBeenCalledTimes(1); - expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), { - def456: { id: 'def456' }, - }); - }); - }); - - describe('editServer', () => { - it('dos nothing is provided server does not exist', () => { - const [ service, storageMock ] = createService(); - - service.editServer('notFound', {}); - - expect(storageMock.set).not.toHaveBeenCalled(); - }); - - it('updates the list with provided server data', () => { - const [ service, storageMock ] = createService(); - const serverData = { name: 'foo', apiKey: 'bar' }; - - service.editServer('abc123', serverData); - - expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), { - abc123: { id: 'abc123', ...serverData }, - def456: { id: 'def456' }, - }); - }); - }); -});