2023-04-13 21:48:29 +02:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-02-18 11:37:49 +01:00
|
|
|
import { ServersExporter } from '../../../src/servers/services/ServersExporter';
|
2023-02-18 10:40:37 +01:00
|
|
|
import type { LocalStorage } from '../../../src/utils/services/LocalStorage';
|
2022-07-16 10:52:45 +02:00
|
|
|
import { appendChild, removeChild, windowMock } from '../../__mocks__/Window.mock';
|
2018-08-24 10:13:55 +02:00
|
|
|
|
|
|
|
describe('ServersExporter', () => {
|
2023-04-13 21:48:29 +02:00
|
|
|
const storageMock = fromPartial<LocalStorage>({
|
2023-05-27 11:57:26 +02:00
|
|
|
get: vi.fn(() => ({
|
2018-08-24 10:13:55 +02:00
|
|
|
abc123: {
|
|
|
|
id: 'abc123',
|
|
|
|
name: 'foo',
|
2021-11-20 09:44:12 +01:00
|
|
|
autoConnect: true,
|
2018-08-24 10:13:55 +02:00
|
|
|
},
|
|
|
|
def456: {
|
|
|
|
id: 'def456',
|
|
|
|
name: 'bar',
|
2021-11-20 09:44:12 +01:00
|
|
|
autoConnect: false,
|
2018-08-24 10:13:55 +02:00
|
|
|
},
|
2023-04-13 21:48:29 +02:00
|
|
|
} as any)),
|
2020-08-29 14:16:37 +02:00
|
|
|
});
|
2023-05-27 11:57:26 +02:00
|
|
|
const erroneousToCsv = vi.fn(() => {
|
2021-02-28 17:21:26 +01:00
|
|
|
throw new Error('');
|
|
|
|
});
|
2023-05-27 11:57:26 +02:00
|
|
|
const createCsvjsonMock = (throwError = false) => (throwError ? erroneousToCsv : vi.fn(() => ''));
|
2018-08-24 10:13:55 +02:00
|
|
|
|
|
|
|
describe('exportServers', () => {
|
2020-08-29 14:16:37 +02:00
|
|
|
let originalConsole: Console;
|
2023-05-27 11:57:26 +02:00
|
|
|
const error = vi.fn();
|
2018-08-24 10:13:55 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
originalConsole = global.console;
|
2023-04-13 21:48:29 +02:00
|
|
|
global.console = fromPartial<Console>({ error });
|
2022-03-26 13:07:58 +01:00
|
|
|
(global as any).Blob = class Blob {};
|
2020-08-29 14:16:37 +02:00
|
|
|
(global as any).URL = { createObjectURL: () => '' };
|
2018-08-24 10:13:55 +02:00
|
|
|
});
|
2018-08-25 23:39:27 +02:00
|
|
|
afterEach(() => {
|
|
|
|
global.console = originalConsole;
|
|
|
|
});
|
2018-08-24 10:13:55 +02:00
|
|
|
|
|
|
|
it('logs an error if something fails', () => {
|
|
|
|
const csvjsonMock = createCsvjsonMock(true);
|
2021-03-14 13:16:20 +01:00
|
|
|
const exporter = new ServersExporter(storageMock, windowMock, csvjsonMock);
|
2018-08-24 10:13:55 +02:00
|
|
|
|
|
|
|
exporter.exportServers();
|
|
|
|
|
2020-08-29 14:16:37 +02:00
|
|
|
expect(error).toHaveBeenCalledTimes(1);
|
2021-02-28 17:21:26 +01:00
|
|
|
expect(erroneousToCsv).toHaveBeenCalledTimes(1);
|
2018-08-24 10:13:55 +02:00
|
|
|
});
|
|
|
|
|
2021-03-14 11:47:23 +01:00
|
|
|
it('makes use of download link API', () => {
|
2020-04-27 13:21:07 +02:00
|
|
|
const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock());
|
2021-02-28 17:21:26 +01:00
|
|
|
const { document: { createElement } } = windowMock;
|
2018-08-24 10:13:55 +02:00
|
|
|
|
|
|
|
exporter.exportServers();
|
|
|
|
|
2020-04-27 13:21:07 +02:00
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
2020-08-29 14:16:37 +02:00
|
|
|
expect(createElement).toHaveBeenCalledTimes(1);
|
|
|
|
expect(appendChild).toHaveBeenCalledTimes(1);
|
|
|
|
expect(removeChild).toHaveBeenCalledTimes(1);
|
2018-08-24 10:13:55 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|