mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-10 17:43:51 +00:00
Deleted no-longer-needed ServersService
This commit is contained in:
@@ -25,12 +25,15 @@ const getServerVersion = memoizeWith(identity, (serverId, health) => health().th
|
|||||||
|
|
||||||
export const resetSelectedServer = createAction(RESET_SELECTED_SERVER);
|
export const resetSelectedServer = createAction(RESET_SELECTED_SERVER);
|
||||||
|
|
||||||
export const selectServer = ({ findServerById }, buildShlinkApiClient, loadMercureInfo) => (serverId) => async (
|
export const selectServer = (buildShlinkApiClient, loadMercureInfo) => (serverId) => async (
|
||||||
dispatch
|
dispatch,
|
||||||
|
getState
|
||||||
) => {
|
) => {
|
||||||
dispatch(resetSelectedServer());
|
dispatch(resetSelectedServer());
|
||||||
dispatch(resetShortUrlParams());
|
dispatch(resetShortUrlParams());
|
||||||
const selectedServer = findServerById(serverId);
|
|
||||||
|
const { servers } = getState();
|
||||||
|
const selectedServer = servers[serverId];
|
||||||
|
|
||||||
if (!selectedServer) {
|
if (!selectedServer) {
|
||||||
dispatch({
|
dispatch({
|
||||||
|
|||||||
@@ -25,14 +25,14 @@ const saveCsv = (window, csv) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default class ServersExporter {
|
export default class ServersExporter {
|
||||||
constructor(serversService, window, csvjson) {
|
constructor(storage, window, csvjson) {
|
||||||
this.serversService = serversService;
|
this.storage = storage;
|
||||||
this.window = window;
|
this.window = window;
|
||||||
this.csvjson = csvjson;
|
this.csvjson = csvjson;
|
||||||
}
|
}
|
||||||
|
|
||||||
exportServers = async () => {
|
exportServers = async () => {
|
||||||
const servers = values(this.serversService.listServers()).map(dissoc('id'));
|
const servers = values(this.storage.get('servers') || {}).map(dissoc('id'));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const csv = this.csvjson.toCSV(servers, {
|
const csv = this.csvjson.toCSV(servers, {
|
||||||
|
|||||||
@@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,7 +11,6 @@ import { fetchServers } from '../reducers/remoteServers';
|
|||||||
import ForServerVersion from '../helpers/ForServerVersion';
|
import ForServerVersion from '../helpers/ForServerVersion';
|
||||||
import { ServerError } from '../helpers/ServerError';
|
import { ServerError } from '../helpers/ServerError';
|
||||||
import ServersImporter from './ServersImporter';
|
import ServersImporter from './ServersImporter';
|
||||||
import ServersService from './ServersService';
|
|
||||||
import ServersExporter from './ServersExporter';
|
import ServersExporter from './ServersExporter';
|
||||||
|
|
||||||
const provideServices = (bottle, connect, withRouter) => {
|
const provideServices = (bottle, connect, withRouter) => {
|
||||||
@@ -44,11 +43,10 @@ const provideServices = (bottle, connect, withRouter) => {
|
|||||||
// Services
|
// Services
|
||||||
bottle.constant('csvjson', csvjson);
|
bottle.constant('csvjson', csvjson);
|
||||||
bottle.service('ServersImporter', ServersImporter, 'csvjson');
|
bottle.service('ServersImporter', ServersImporter, 'csvjson');
|
||||||
bottle.service('ServersService', ServersService, 'Storage');
|
bottle.service('ServersExporter', ServersExporter, 'Storage', 'window', 'csvjson');
|
||||||
bottle.service('ServersExporter', ServersExporter, 'ServersService', 'window', 'csvjson');
|
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
bottle.serviceFactory('selectServer', selectServer, 'ServersService', 'buildShlinkApiClient', 'loadMercureInfo');
|
bottle.serviceFactory('selectServer', selectServer, 'buildShlinkApiClient', 'loadMercureInfo');
|
||||||
bottle.serviceFactory('createServer', () => createServer);
|
bottle.serviceFactory('createServer', () => createServer);
|
||||||
bottle.serviceFactory('createServers', () => createServers);
|
bottle.serviceFactory('createServers', () => createServers);
|
||||||
bottle.serviceFactory('deleteServer', () => deleteServer);
|
bottle.serviceFactory('deleteServer', () => deleteServer);
|
||||||
|
|||||||
@@ -32,9 +32,7 @@ describe('selectedServerReducer', () => {
|
|||||||
id: 'abc123',
|
id: 'abc123',
|
||||||
};
|
};
|
||||||
const version = '1.19.0';
|
const version = '1.19.0';
|
||||||
const ServersServiceMock = {
|
const createGetStateMock = (id) => jest.fn().mockReturnValue({ servers: { [id]: selectedServer } });
|
||||||
findServerById: jest.fn(() => selectedServer),
|
|
||||||
};
|
|
||||||
const apiClientMock = {
|
const apiClientMock = {
|
||||||
health: jest.fn(),
|
health: jest.fn(),
|
||||||
};
|
};
|
||||||
@@ -49,6 +47,8 @@ describe('selectedServerReducer', () => {
|
|||||||
[ 'latest', MAX_FALLBACK_VERSION, 'latest' ],
|
[ 'latest', MAX_FALLBACK_VERSION, 'latest' ],
|
||||||
[ '%invalid_semver%', MIN_FALLBACK_VERSION, '%invalid_semver%' ],
|
[ '%invalid_semver%', MIN_FALLBACK_VERSION, '%invalid_semver%' ],
|
||||||
])('dispatches proper actions', async (serverVersion, expectedVersion, expectedPrintableVersion) => {
|
])('dispatches proper actions', async (serverVersion, expectedVersion, expectedPrintableVersion) => {
|
||||||
|
const id = uuid();
|
||||||
|
const getState = createGetStateMock(id);
|
||||||
const expectedSelectedServer = {
|
const expectedSelectedServer = {
|
||||||
...selectedServer,
|
...selectedServer,
|
||||||
version: expectedVersion,
|
version: expectedVersion,
|
||||||
@@ -57,7 +57,7 @@ describe('selectedServerReducer', () => {
|
|||||||
|
|
||||||
apiClientMock.health.mockResolvedValue({ version: serverVersion });
|
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).toHaveBeenCalledTimes(4);
|
||||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: RESET_SELECTED_SERVER });
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: RESET_SELECTED_SERVER });
|
||||||
@@ -67,18 +67,23 @@ describe('selectedServerReducer', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('invokes dependencies', async () => {
|
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);
|
expect(buildApiClient).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('dispatches error when health endpoint fails', async () => {
|
it('dispatches error when health endpoint fails', async () => {
|
||||||
|
const id = uuid();
|
||||||
|
const getState = createGetStateMock(id);
|
||||||
const expectedSelectedServer = { ...selectedServer, serverNotReachable: true };
|
const expectedSelectedServer = { ...selectedServer, serverNotReachable: true };
|
||||||
|
|
||||||
apiClientMock.health.mockRejectedValue({});
|
apiClientMock.health.mockRejectedValue({});
|
||||||
|
|
||||||
await selectServer(ServersServiceMock, buildApiClient, loadMercureInfo)(uuid())(dispatch);
|
await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState);
|
||||||
|
|
||||||
expect(apiClientMock.health).toHaveBeenCalled();
|
expect(apiClientMock.health).toHaveBeenCalled();
|
||||||
expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
|
expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
|
||||||
@@ -86,13 +91,13 @@ describe('selectedServerReducer', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('dispatches error when server is not found', async () => {
|
it('dispatches error when server is not found', async () => {
|
||||||
|
const id = uuid();
|
||||||
|
const getState = jest.fn(() => ({ servers: {} }));
|
||||||
const expectedSelectedServer = { serverNotFound: true };
|
const expectedSelectedServer = { serverNotFound: true };
|
||||||
|
|
||||||
ServersServiceMock.findServerById.mockReturnValue(undefined);
|
await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState);
|
||||||
|
|
||||||
await selectServer(ServersServiceMock, buildApiClient, loadMercureInfo)(uuid())(dispatch);
|
expect(getState).toHaveBeenCalled();
|
||||||
|
|
||||||
expect(ServersServiceMock.findServerById).toHaveBeenCalled();
|
|
||||||
expect(apiClientMock.health).not.toHaveBeenCalled();
|
expect(apiClientMock.health).not.toHaveBeenCalled();
|
||||||
expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
|
expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
|
||||||
expect(loadMercureInfo).not.toHaveBeenCalled();
|
expect(loadMercureInfo).not.toHaveBeenCalled();
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ describe('ServersExporter', () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const serversServiceMock = {
|
const storageMock = {
|
||||||
listServers: jest.fn(() => ({
|
get: jest.fn(() => ({
|
||||||
abc123: {
|
abc123: {
|
||||||
id: 'abc123',
|
id: 'abc123',
|
||||||
name: 'foo',
|
name: 'foo',
|
||||||
@@ -48,19 +48,15 @@ describe('ServersExporter', () => {
|
|||||||
global.console = { error: jest.fn() };
|
global.console = { error: jest.fn() };
|
||||||
global.Blob = class Blob {};
|
global.Blob = class Blob {};
|
||||||
global.URL = { createObjectURL: () => '' };
|
global.URL = { createObjectURL: () => '' };
|
||||||
serversServiceMock.listServers.mockReset();
|
|
||||||
});
|
});
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
global.console = originalConsole;
|
global.console = originalConsole;
|
||||||
|
jest.clearAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('logs an error if something fails', () => {
|
it('logs an error if something fails', () => {
|
||||||
const csvjsonMock = createCsvjsonMock(true);
|
const csvjsonMock = createCsvjsonMock(true);
|
||||||
const exporter = new ServersExporter(
|
const exporter = new ServersExporter(storageMock, createWindowMock(), csvjsonMock);
|
||||||
serversServiceMock,
|
|
||||||
createWindowMock(),
|
|
||||||
csvjsonMock,
|
|
||||||
);
|
|
||||||
|
|
||||||
exporter.exportServers();
|
exporter.exportServers();
|
||||||
|
|
||||||
@@ -70,30 +66,22 @@ describe('ServersExporter', () => {
|
|||||||
|
|
||||||
it('makes use of msSaveBlob API when available', () => {
|
it('makes use of msSaveBlob API when available', () => {
|
||||||
const windowMock = createWindowMock();
|
const windowMock = createWindowMock();
|
||||||
const exporter = new ServersExporter(
|
const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock());
|
||||||
serversServiceMock,
|
|
||||||
windowMock,
|
|
||||||
createCsvjsonMock(),
|
|
||||||
);
|
|
||||||
|
|
||||||
exporter.exportServers();
|
exporter.exportServers();
|
||||||
|
|
||||||
expect(serversServiceMock.listServers).toHaveBeenCalledTimes(1);
|
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||||
expect(windowMock.navigator.msSaveBlob).toHaveBeenCalledTimes(1);
|
expect(windowMock.navigator.msSaveBlob).toHaveBeenCalledTimes(1);
|
||||||
expect(windowMock.document.createElement).not.toHaveBeenCalled();
|
expect(windowMock.document.createElement).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('makes use of download link API when available', () => {
|
it('makes use of download link API when available', () => {
|
||||||
const windowMock = createWindowMock(false);
|
const windowMock = createWindowMock(false);
|
||||||
const exporter = new ServersExporter(
|
const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock());
|
||||||
serversServiceMock,
|
|
||||||
windowMock,
|
|
||||||
createCsvjsonMock(),
|
|
||||||
);
|
|
||||||
|
|
||||||
exporter.exportServers();
|
exporter.exportServers();
|
||||||
|
|
||||||
expect(serversServiceMock.listServers).toHaveBeenCalledTimes(1);
|
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||||
expect(windowMock.document.createElement).toHaveBeenCalledTimes(1);
|
expect(windowMock.document.createElement).toHaveBeenCalledTimes(1);
|
||||||
expect(windowMock.document.body.appendChild).toHaveBeenCalledTimes(1);
|
expect(windowMock.document.body.appendChild).toHaveBeenCalledTimes(1);
|
||||||
expect(windowMock.document.body.removeChild).toHaveBeenCalledTimes(1);
|
expect(windowMock.document.body.removeChild).toHaveBeenCalledTimes(1);
|
||||||
|
|||||||
@@ -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' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user