summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/number_utility_spec.js
blob: f4483f5098b5e87e9f3a9ea226a1aedefe9fcd1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {
  formatRelevantDigits,
  bytesToKiB,
  bytesToMiB,
  bytesToGiB,
  numberToHumanSize,
  sum,
  isOdd,
  median,
  changeInPercent,
  formattedChangeInPercent,
  isNumeric,
  isPositiveInteger,
} from '~/lib/utils/number_utils';

describe('Number Utils', () => {
  describe('formatRelevantDigits', () => {
    it('returns an empty string when the number is NaN', () => {
      expect(formatRelevantDigits('fail')).toBe('');
    });

    it('returns 4 decimals when there is 4 plus digits to the left', () => {
      const formattedNumber = formatRelevantDigits('1000.1234567');
      const rightFromDecimal = formattedNumber.split('.')[1];
      const leftFromDecimal = formattedNumber.split('.')[0];

      expect(rightFromDecimal.length).toBe(4);
      expect(leftFromDecimal.length).toBe(4);
    });

    it('returns 3 decimals when there is 1 digit to the left', () => {
      const formattedNumber = formatRelevantDigits('0.1234567');
      const rightFromDecimal = formattedNumber.split('.')[1];
      const leftFromDecimal = formattedNumber.split('.')[0];

      expect(rightFromDecimal.length).toBe(3);
      expect(leftFromDecimal.length).toBe(1);
    });

    it('returns 2 decimals when there is 2 digits to the left', () => {
      const formattedNumber = formatRelevantDigits('10.1234567');
      const rightFromDecimal = formattedNumber.split('.')[1];
      const leftFromDecimal = formattedNumber.split('.')[0];

      expect(rightFromDecimal.length).toBe(2);
      expect(leftFromDecimal.length).toBe(2);
    });

    it('returns 1 decimal when there is 3 digits to the left', () => {
      const formattedNumber = formatRelevantDigits('100.1234567');
      const rightFromDecimal = formattedNumber.split('.')[1];
      const leftFromDecimal = formattedNumber.split('.')[0];

      expect(rightFromDecimal.length).toBe(1);
      expect(leftFromDecimal.length).toBe(3);
    });
  });

  describe('bytesToKiB', () => {
    it('calculates KiB for the given bytes', () => {
      expect(bytesToKiB(1024)).toEqual(1);
      expect(bytesToKiB(1000)).toEqual(0.9765625);
    });
  });

  describe('bytesToMiB', () => {
    it('calculates MiB for the given bytes', () => {
      expect(bytesToMiB(1048576)).toEqual(1);
      expect(bytesToMiB(1000000)).toEqual(0.95367431640625);
    });
  });

  describe('bytesToGiB', () => {
    it('calculates GiB for the given bytes', () => {
      expect(bytesToGiB(1073741824)).toEqual(1);
      expect(bytesToGiB(10737418240)).toEqual(10);
    });
  });

  describe('numberToHumanSize', () => {
    it('should return bytes', () => {
      expect(numberToHumanSize(654)).toEqual('654 bytes');
    });

    it('should return KiB', () => {
      expect(numberToHumanSize(1079)).toEqual('1.05 KiB');
    });

    it('should return MiB', () => {
      expect(numberToHumanSize(10485764)).toEqual('10.00 MiB');
    });

    it('should return GiB', () => {
      expect(numberToHumanSize(10737418240)).toEqual('10.00 GiB');
    });
  });

  describe('sum', () => {
    it('should add up two values', () => {
      expect(sum(1, 2)).toEqual(3);
    });

    it('should add up all the values in an array when passed to a reducer', () => {
      expect([1, 2, 3, 4, 5].reduce(sum)).toEqual(15);
    });
  });

  describe('isOdd', () => {
    it('should return 0 with a even number', () => {
      expect(isOdd(2)).toEqual(0);
    });

    it('should return 1 with a odd number', () => {
      expect(isOdd(1)).toEqual(1);
    });
  });

  describe('median', () => {
    it('computes the median for a given array with odd length', () => {
      const items = [10, 27, 20, 5, 19];
      expect(median(items)).toBe(19);
    });

    it('computes the median for a given array with even length', () => {
      const items = [10, 27, 20, 5, 19, 4];
      expect(median(items)).toBe(14.5);
    });
  });

  describe('changeInPercent', () => {
    it.each`
      firstValue | secondValue | expectedOutput
      ${99}      | ${100}      | ${1}
      ${100}     | ${99}       | ${-1}
      ${0}       | ${99}       | ${Infinity}
      ${2}       | ${2}        | ${0}
      ${-100}    | ${-99}      | ${1}
    `(
      'computes the change between $firstValue and $secondValue in percent',
      ({ firstValue, secondValue, expectedOutput }) => {
        expect(changeInPercent(firstValue, secondValue)).toBe(expectedOutput);
      },
    );
  });

  describe('formattedChangeInPercent', () => {
    it('prepends "%" to the output', () => {
      expect(formattedChangeInPercent(1, 2)).toMatch(/%$/);
    });

    it('indicates if the change was a decrease', () => {
      expect(formattedChangeInPercent(100, 99)).toContain('-1');
    });

    it('indicates if the change was an increase', () => {
      expect(formattedChangeInPercent(99, 100)).toContain('+1');
    });

    it('shows "-" per default if the change can not be expressed in an integer', () => {
      expect(formattedChangeInPercent(0, 1)).toBe('-');
    });

    it('shows the given fallback if the change can not be expressed in an integer', () => {
      expect(formattedChangeInPercent(0, 1, { nonFiniteResult: '*' })).toBe('*');
    });
  });

  describe('isNumeric', () => {
    it.each`
      value        | outcome
      ${0}         | ${true}
      ${12345}     | ${true}
      ${'0'}       | ${true}
      ${'12345'}   | ${true}
      ${1.0}       | ${true}
      ${'1.0'}     | ${true}
      ${'abcd'}    | ${false}
      ${'abcd100'} | ${false}
      ${''}        | ${false}
      ${false}     | ${false}
      ${true}      | ${false}
      ${undefined} | ${false}
      ${null}      | ${false}
    `('when called with $value it returns $outcome', ({ value, outcome }) => {
      expect(isNumeric(value)).toBe(outcome);
    });
  });

  describe.each`
    value        | outcome
    ${0}         | ${true}
    ${'0'}       | ${true}
    ${12345}     | ${true}
    ${'12345'}   | ${true}
    ${-1}        | ${false}
    ${'-1'}      | ${false}
    ${1.01}      | ${false}
    ${'1.01'}    | ${false}
    ${'abcd'}    | ${false}
    ${'100abcd'} | ${false}
    ${'abcd100'} | ${false}
    ${''}        | ${false}
    ${false}     | ${false}
    ${true}      | ${false}
    ${undefined} | ${false}
    ${null}      | ${false}
    ${Infinity}  | ${false}
  `('isPositiveInteger', ({ value, outcome }) => {
    it(`when called with ${typeof value} ${value} it returns ${outcome}`, () => {
      expect(isPositiveInteger(value)).toBe(outcome);
    });
  });
});