|
1
|
|
|
import ServersExporter from '../../../src/servers/services/ServersExporter'; |
|
2
|
|
|
|
|
3
|
|
|
describe('ServersExporter', () => { |
|
4
|
|
|
const createLinkMock = () => ({ |
|
5
|
|
|
setAttribute: jest.fn(), |
|
|
|
|
|
|
6
|
|
|
click: jest.fn(), |
|
7
|
|
|
style: {}, |
|
8
|
|
|
}); |
|
9
|
|
|
const createWindowMock = (isIe10 = true) => ({ |
|
10
|
|
|
navigator: { |
|
11
|
|
|
msSaveBlob: isIe10 ? jest.fn() : undefined, |
|
|
|
|
|
|
12
|
|
|
}, |
|
13
|
|
|
document: { |
|
14
|
|
|
createElement: jest.fn(() => createLinkMock()), |
|
15
|
|
|
body: { |
|
16
|
|
|
appendChild: jest.fn(), |
|
17
|
|
|
removeChild: jest.fn(), |
|
18
|
|
|
}, |
|
19
|
|
|
}, |
|
20
|
|
|
}); |
|
21
|
|
|
const storageMock = { |
|
22
|
|
|
get: jest.fn(() => ({ |
|
|
|
|
|
|
23
|
|
|
abc123: { |
|
24
|
|
|
id: 'abc123', |
|
25
|
|
|
name: 'foo', |
|
26
|
|
|
}, |
|
27
|
|
|
def456: { |
|
28
|
|
|
id: 'def456', |
|
29
|
|
|
name: 'bar', |
|
30
|
|
|
}, |
|
31
|
|
|
})), |
|
32
|
|
|
}; |
|
33
|
|
|
const createCsvjsonMock = (throwError = false) => ({ |
|
34
|
|
|
toCSV: jest.fn(() => { |
|
|
|
|
|
|
35
|
|
|
if (throwError) { |
|
36
|
|
|
throw new Error(''); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return ''; |
|
40
|
|
|
}), |
|
41
|
|
|
}); |
|
42
|
|
|
|
|
43
|
|
|
describe('exportServers', () => { |
|
44
|
|
|
let originalConsole; |
|
45
|
|
|
|
|
46
|
|
|
beforeEach(() => { |
|
47
|
|
|
originalConsole = global.console; |
|
48
|
|
|
global.console = { error: jest.fn() }; |
|
|
|
|
|
|
49
|
|
|
global.Blob = class Blob {}; |
|
50
|
|
|
global.URL = { createObjectURL: () => '' }; |
|
51
|
|
|
}); |
|
52
|
|
|
afterEach(() => { |
|
53
|
|
|
global.console = originalConsole; |
|
54
|
|
|
jest.clearAllMocks(); |
|
|
|
|
|
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
it('logs an error if something fails', () => { |
|
58
|
|
|
const csvjsonMock = createCsvjsonMock(true); |
|
59
|
|
|
const exporter = new ServersExporter(storageMock, createWindowMock(), csvjsonMock); |
|
60
|
|
|
|
|
61
|
|
|
exporter.exportServers(); |
|
62
|
|
|
|
|
63
|
|
|
expect(global.console.error).toHaveBeenCalledTimes(1); |
|
64
|
|
|
expect(csvjsonMock.toCSV).toHaveBeenCalledTimes(1); |
|
65
|
|
|
}); |
|
66
|
|
|
|
|
67
|
|
|
it('makes use of msSaveBlob API when available', () => { |
|
68
|
|
|
const windowMock = createWindowMock(); |
|
69
|
|
|
const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock()); |
|
70
|
|
|
|
|
71
|
|
|
exporter.exportServers(); |
|
72
|
|
|
|
|
73
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1); |
|
74
|
|
|
expect(windowMock.navigator.msSaveBlob).toHaveBeenCalledTimes(1); |
|
75
|
|
|
expect(windowMock.document.createElement).not.toHaveBeenCalled(); |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
it('makes use of download link API when available', () => { |
|
79
|
|
|
const windowMock = createWindowMock(false); |
|
80
|
|
|
const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock()); |
|
81
|
|
|
|
|
82
|
|
|
exporter.exportServers(); |
|
83
|
|
|
|
|
84
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1); |
|
85
|
|
|
expect(windowMock.document.createElement).toHaveBeenCalledTimes(1); |
|
86
|
|
|
expect(windowMock.document.body.appendChild).toHaveBeenCalledTimes(1); |
|
87
|
|
|
expect(windowMock.document.body.removeChild).toHaveBeenCalledTimes(1); |
|
88
|
|
|
}); |
|
89
|
|
|
}); |
|
90
|
|
|
}); |
|
91
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.