-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathhelpers.spec.js
More file actions
84 lines (65 loc) · 1.98 KB
/
helpers.spec.js
File metadata and controls
84 lines (65 loc) · 1.98 KB
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
const {
flattenArr,
dataFetcher,
sortList,
formatCurrency,
handlePromises
} = require('./helpers.js');
const axios = require('axios');
jest.mock('axios');
describe('flattenArr', () => {
it('return a non-nested arr', () => {
const input = [1, 2, 3, 4];
const expectedOutput = [1, 2, 3, 4];
expect(flattenArr(input)).toEqual(expectedOutput);
});
it('flattens a nested arr', () => {
const input = [1, 2, 3, [4, 5, [6, 7, [8, [9, [10]]]]]];
const expectedOutput = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
expect(flattenArr(input)).toEqual(expectedOutput);
});
});
describe('dataFetcher', () => {
it('handles a successful response', async () => {
axios.get.mockImplementation(() => Promise.resolve({ data: { users: [] } }));
const data = await dataFetcher();
expect(data).toEqual({ data: { users: [] } });
});
it('handles an error response', async () => {
axios.get.mockImplementation(() => Promise.reject('Boom'));
try {
await dataFetcher();
} catch (e) {
expect(e).toEqual(new Error({ error: 'Boom', message: 'An Error Occurred' }));
}
});
});
describe('sortList', () => {
it('calls a sorter function if it is available', () => {
const sortFn = jest.fn();
sortList([3, 2, 1], sortFn);
expect(sortFn).toBeCalled();
expect(sortFn).toBeCalledTimes(1);
expect(sortFn.mock.calls).toEqual([[[3, 2, 1]]]);
});
it('does not call a sorter function if the array has a length <= 1', () => {
const sortFn = jest.fn();
sortList([1], sortFn);
expect(sortFn).not.toBeCalled();
expect(sortFn).toBeCalledTimes(0);
});
});
/**
* Add you test/s here and get this helper file to 100% test coverage!!!
* You can check that your coverage meets 100% by running `npm run test:coverage`
*/
describe('formatCurrency', () => {
it('does <insert your test here>', () => {
return true;
});
});
describe('handlePromises', () => {
it('does <insert your test here>', () => {
return true;
});
});