Completed
Push — master ( 158ed8...34f194 )
by Alejandro
04:53 queued 02:19
created

test/utils/utils.test.js   A

Complexity

Total Complexity 18
Complexity/F 1

Size

Lines of Code 106
Function Count 18

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 70
mnd 0
bc 0
fnc 18
dl 0
loc 106
rs 10
bpm 0
cpm 1
noi 4
c 0
b 0
f 0
1
import L from 'leaflet';
2
import marker2x from 'leaflet/dist/images/marker-icon-2x.png';
3
import marker from 'leaflet/dist/images/marker-icon.png';
4
import markerShadow from 'leaflet/dist/images/marker-shadow.png';
5
import {
6
  stateFlagTimeout as stateFlagTimeoutFactory,
0 ignored issues
show
Unused Code introduced by
The variable stateFlagTimeout seems to be never used. Consider removing it.
Loading history...
7
  determineOrderDir,
8
  fixLeafletIcons,
9
  rangeOf,
10
  roundTen,
11
} from '../../src/utils/utils';
12
13
describe('utils', () => {
14
  describe('stateFlagTimeout', () => {
15
    it('sets state and initializes timeout with provided delay', () => {
16
      const setTimeout = jest.fn((callback) => callback());
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...
17
      const setState = jest.fn();
18
      const stateFlagTimeout = stateFlagTimeoutFactory(setTimeout);
19
      const delay = 5000;
20
21
      stateFlagTimeout(setState, 'foo', false, delay);
22
23
      expect(setState).toHaveBeenCalledTimes(2);
24
      expect(setState).toHaveBeenNthCalledWith(1, { foo: false });
25
      expect(setState).toHaveBeenNthCalledWith(2, { foo: true });
26
      expect(setTimeout).toHaveBeenCalledTimes(1);
27
      expect(setTimeout).toHaveBeenCalledWith(expect.anything(), delay);
0 ignored issues
show
Bug introduced by
The variable expect seems to be never declared. If this is a global, consider adding a /** global: expect */ 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...
28
    });
29
  });
30
31
  describe('determineOrderDir', () => {
32
    it('returns ASC when current order field and selected field are different', () => {
33
      expect(determineOrderDir('foo', 'bar')).toEqual('ASC');
34
      expect(determineOrderDir('bar', 'foo')).toEqual('ASC');
35
    });
36
37
    it('returns ASC when no current order dir is provided', () => {
38
      expect(determineOrderDir('foo', 'foo')).toEqual('ASC');
39
      expect(determineOrderDir('bar', 'bar')).toEqual('ASC');
40
    });
41
42
    it('returns DESC when current order field and selected field are equal and current order dir is ASC', () => {
43
      expect(determineOrderDir('foo', 'foo', 'ASC')).toEqual('DESC');
44
      expect(determineOrderDir('bar', 'bar', 'ASC')).toEqual('DESC');
45
    });
46
47
    it('returns undefined when current order field and selected field are equal and current order dir is DESC', () => {
48
      expect(determineOrderDir('foo', 'foo', 'DESC')).toBeUndefined();
49
      expect(determineOrderDir('bar', 'bar', 'DESC')).toBeUndefined();
50
    });
51
  });
52
53
  describe('fixLeafletIcons', () => {
54
    it('updates icons used by leaflet', () => {
55
      fixLeafletIcons();
56
57
      const { iconRetinaUrl, iconUrl, shadowUrl } = L.Icon.Default.prototype.options;
58
59
      expect(iconRetinaUrl).toEqual(marker2x);
60
      expect(iconUrl).toEqual(marker);
61
      expect(shadowUrl).toEqual(markerShadow);
62
    });
63
  });
64
65
  describe('rangeOf', () => {
66
    const func = (i) => `result_${i}`;
67
    const size = 5;
68
69
    it('builds a range of specified size invike provided function', () => {
70
      expect(rangeOf(size, func)).toEqual([
71
        'result_1',
72
        'result_2',
73
        'result_3',
74
        'result_4',
75
        'result_5',
76
      ]);
77
    });
78
79
    it('builds a range starting at provided pos', () => {
80
      const startAt = 3;
81
82
      expect(rangeOf(size, func, startAt)).toEqual([
83
        'result_3',
84
        'result_4',
85
        'result_5',
86
      ]);
87
    });
88
  });
89
90
  describe('roundTen', () => {
91
    it('rounds provided number to the next multiple of ten', () => {
92
      const expectationsPairs = [
93
        [ 10, 10 ],
94
        [ 12, 20 ],
95
        [ 158, 160 ],
96
        [ 5, 10 ],
97
        [ -42, -40 ],
98
      ];
99
100
      expect.assertions(expectationsPairs.length);
0 ignored issues
show
Bug introduced by
The variable expect seems to be never declared. If this is a global, consider adding a /** global: expect */ 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...
101
      expectationsPairs.forEach(([ number, expected ]) => {
102
        expect(roundTen(number)).toEqual(expected);
103
      });
104
    });
105
  });
106
});
107