mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-02 05:36:38 +00:00
Deleted no-longer-needed ServersService
This commit is contained in:
@@ -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' },
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user