Completed
Push — master ( 3953e9...fad0bf )
by Alejandro
15s queued 11s
created

test/servers/services/ServersExporter.test.js   A

Complexity

Total Complexity 16
Complexity/F 1.14

Size

Lines of Code 90
Function Count 14

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 58
mnd 2
bc 2
fnc 14
dl 0
loc 90
rs 10
bpm 0.1428
cpm 1.1428
noi 6
c 0
b 0
f 0
1
import ServersExporter from '../../../src/servers/services/ServersExporter';
2
3
describe('ServersExporter', () => {
4
  const createLinkMock = () => ({
5
    setAttribute: jest.fn(),
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

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.

Loading history...
6
    click: jest.fn(),
7
    style: {},
8
  });
9
  const createWindowMock = (isIe10 = true) => ({
10
    navigator: {
11
      msSaveBlob: isIe10 ? jest.fn() : undefined,
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

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.

Loading history...
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(() => ({
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

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.

Loading history...
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(() => {
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

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.

Loading history...
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() };
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

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.

Loading history...
49
      global.Blob = class Blob {};
50
      global.URL = { createObjectURL: () => '' };
51
    });
52
    afterEach(() => {
53
      global.console = originalConsole;
54
      jest.clearAllMocks();
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

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.

Loading history...
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