2020-08-29 15:16:37 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2018-12-18 21:59:50 +03:00
|
|
|
import ServersExporter from '../../../src/servers/services/ServersExporter';
|
2020-08-29 15:16:37 +03:00
|
|
|
import LocalStorage from '../../../src/utils/services/LocalStorage';
|
2021-09-19 11:57:36 +03:00
|
|
|
import { appendChild, removeChild, windowMock } from '../../mocks/WindowMock';
|
2018-08-24 11:13:55 +03:00
|
|
|
|
|
|
|
describe('ServersExporter', () => {
|
2020-08-29 15:16:37 +03:00
|
|
|
const storageMock = Mock.of<LocalStorage>({
|
2020-04-27 14:21:07 +03:00
|
|
|
get: jest.fn(() => ({
|
2018-08-24 11:13:55 +03:00
|
|
|
abc123: {
|
|
|
|
id: 'abc123',
|
|
|
|
name: 'foo',
|
2021-11-20 11:44:12 +03:00
|
|
|
autoConnect: true,
|
2018-08-24 11:13:55 +03:00
|
|
|
},
|
|
|
|
def456: {
|
|
|
|
id: 'def456',
|
|
|
|
name: 'bar',
|
2021-11-20 11:44:12 +03:00
|
|
|
autoConnect: false,
|
2018-08-24 11:13:55 +03:00
|
|
|
},
|
2019-04-19 11:29:49 +03:00
|
|
|
})),
|
2020-08-29 15:16:37 +03:00
|
|
|
});
|
2021-02-28 19:21:26 +03:00
|
|
|
const erroneousToCsv = jest.fn(() => {
|
|
|
|
throw new Error('');
|
|
|
|
});
|
2022-03-31 21:18:05 +03:00
|
|
|
const createCsvjsonMock = (throwError = false) => (throwError ? erroneousToCsv : jest.fn(() => ''));
|
2018-08-24 11:13:55 +03:00
|
|
|
|
2021-02-28 19:21:26 +03:00
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
|
2018-08-24 11:13:55 +03:00
|
|
|
describe('exportServers', () => {
|
2020-08-29 15:16:37 +03:00
|
|
|
let originalConsole: Console;
|
|
|
|
const error = jest.fn();
|
2018-08-24 11:13:55 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
originalConsole = global.console;
|
2020-08-29 15:16:37 +03:00
|
|
|
global.console = Mock.of<Console>({ error });
|
2022-03-26 15:07:58 +03:00
|
|
|
(global as any).Blob = class Blob {};
|
2020-08-29 15:16:37 +03:00
|
|
|
(global as any).URL = { createObjectURL: () => '' };
|
2018-08-24 11:13:55 +03:00
|
|
|
});
|
2018-08-26 00:39:27 +03:00
|
|
|
afterEach(() => {
|
|
|
|
global.console = originalConsole;
|
|
|
|
});
|
2018-08-24 11:13:55 +03:00
|
|
|
|
|
|
|
it('logs an error if something fails', () => {
|
|
|
|
const csvjsonMock = createCsvjsonMock(true);
|
2021-03-14 15:16:20 +03:00
|
|
|
const exporter = new ServersExporter(storageMock, windowMock, csvjsonMock);
|
2018-08-24 11:13:55 +03:00
|
|
|
|
|
|
|
exporter.exportServers();
|
|
|
|
|
2020-08-29 15:16:37 +03:00
|
|
|
expect(error).toHaveBeenCalledTimes(1);
|
2021-02-28 19:21:26 +03:00
|
|
|
expect(erroneousToCsv).toHaveBeenCalledTimes(1);
|
2018-08-24 11:13:55 +03:00
|
|
|
});
|
|
|
|
|
2021-03-14 13:47:23 +03:00
|
|
|
it('makes use of download link API', () => {
|
2020-04-27 14:21:07 +03:00
|
|
|
const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock());
|
2021-02-28 19:21:26 +03:00
|
|
|
const { document: { createElement } } = windowMock;
|
2018-08-24 11:13:55 +03:00
|
|
|
|
|
|
|
exporter.exportServers();
|
|
|
|
|
2020-04-27 14:21:07 +03:00
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
2020-08-29 15:16:37 +03:00
|
|
|
expect(createElement).toHaveBeenCalledTimes(1);
|
|
|
|
expect(appendChild).toHaveBeenCalledTimes(1);
|
|
|
|
expect(removeChild).toHaveBeenCalledTimes(1);
|
2018-08-24 11:13:55 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|