mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-14 03:23:49 +00:00
Created ServersExporter test
This commit is contained in:
95
test/servers/services/ServersExporter.test.js
Normal file
95
test/servers/services/ServersExporter.test.js
Normal file
@@ -0,0 +1,95 @@
|
||||
import { ServersExporter } from '../../../src/servers/services/ServersExporter';
|
||||
import sinon from 'sinon';
|
||||
|
||||
describe('ServersExporter', () => {
|
||||
const createLinkMock = () => ({
|
||||
setAttribute: sinon.fake(),
|
||||
click: sinon.fake(),
|
||||
style: {},
|
||||
});
|
||||
const createWindowMock = (isIe10 = true) => ({
|
||||
navigator: {
|
||||
msSaveBlob: isIe10 ? sinon.fake() : undefined,
|
||||
},
|
||||
document: {
|
||||
createElement: sinon.fake.returns(createLinkMock()),
|
||||
body: {
|
||||
appendChild: sinon.fake(),
|
||||
removeChild: sinon.fake(),
|
||||
},
|
||||
}
|
||||
});
|
||||
const serversServiceMock = {
|
||||
listServers: sinon.fake.returns({
|
||||
abc123: {
|
||||
id: 'abc123',
|
||||
name: 'foo',
|
||||
},
|
||||
def456: {
|
||||
id: 'def456',
|
||||
name: 'bar',
|
||||
},
|
||||
}),
|
||||
};
|
||||
const createCsvjsonMock = (throwError = false) => ({
|
||||
toCSV: throwError ? sinon.fake.throws('') : sinon.fake.returns(''),
|
||||
});
|
||||
|
||||
describe('exportServers', () => {
|
||||
let originalConsole;
|
||||
|
||||
beforeEach(() => {
|
||||
originalConsole = global.console;
|
||||
global.console = { error: sinon.fake() };
|
||||
global.Blob = function Blob() {};
|
||||
global.URL = { createObjectURL: () => '' };
|
||||
serversServiceMock.listServers.resetHistory();
|
||||
});
|
||||
afterEach(() => global.console = originalConsole);
|
||||
|
||||
it('logs an error if something fails', () => {
|
||||
const csvjsonMock = createCsvjsonMock(true);
|
||||
const exporter = new ServersExporter(
|
||||
serversServiceMock,
|
||||
createWindowMock(),
|
||||
csvjsonMock,
|
||||
);
|
||||
|
||||
exporter.exportServers();
|
||||
|
||||
expect(global.console.error.callCount).toEqual(1);
|
||||
expect(csvjsonMock.toCSV.callCount).toEqual(1);
|
||||
});
|
||||
|
||||
it('makes use of msSaveBlob API when available', () => {
|
||||
const windowMock = createWindowMock();
|
||||
const exporter = new ServersExporter(
|
||||
serversServiceMock,
|
||||
windowMock,
|
||||
createCsvjsonMock(),
|
||||
);
|
||||
|
||||
exporter.exportServers();
|
||||
|
||||
expect(serversServiceMock.listServers.callCount).toEqual(1);
|
||||
expect(windowMock.navigator.msSaveBlob.callCount).toEqual(1);
|
||||
expect(windowMock.document.createElement.callCount).toEqual(0);
|
||||
});
|
||||
|
||||
it('makes use of download link API when available', () => {
|
||||
const windowMock = createWindowMock(false);
|
||||
const exporter = new ServersExporter(
|
||||
serversServiceMock,
|
||||
windowMock,
|
||||
createCsvjsonMock(),
|
||||
);
|
||||
|
||||
exporter.exportServers();
|
||||
|
||||
expect(serversServiceMock.listServers.callCount).toEqual(1);
|
||||
expect(windowMock.document.createElement.callCount).toEqual(1);
|
||||
expect(windowMock.document.body.appendChild.callCount).toEqual(1);
|
||||
expect(windowMock.document.body.removeChild.callCount).toEqual(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user