mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-10 18:27:25 +03:00
Deleted no-longer-needed ServersService
This commit is contained in:
parent
b08c6748c7
commit
a7f941e8e4
7 changed files with 35 additions and 209 deletions
|
@ -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({
|
||||
|
|
|
@ -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, {
|
||||
|
|
|
@ -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 { 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);
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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' },
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue