-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathlighthouseBudget.test.js
136 lines (116 loc) · 2.75 KB
/
lighthouseBudget.test.js
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
const fs = require('fs');
const {
logRow,
getCategoryScores,
isAboveThreshold,
compareToBudget,
readReport,
exitResult,
} = require('./lighthouseBudget');
jest.mock('fs');
global.console = {
warn: jest.fn(),
log: jest.fn(),
table: jest.fn(),
};
global.JSON = {
parse: jest.fn(),
};
const resultMock = (ally, bestPractises, seo) => ({
ally,
bestPractises,
seo,
});
const data = {
categories: {
performance: {
id: 'performance',
score: 0.92,
},
accessibility: {
id: 'accessibility',
score: 1.0,
},
'best-practices': {
id: 'best-practices',
score: 0.41,
},
seo: {
id: 'seo',
score: 0.5,
},
pwa: {
id: 'pwa',
score: 0.7,
},
},
};
const testCategories = ['seo', 'bestPractises', 'ally'];
const testBudget = {
ally: 100,
bestPractises: 62,
seo: 0,
};
test('Log row returns the correct object', () => {
const expected = {
category: 'category',
scoreValue: 40,
budgetValue: 50,
status: 'Fail',
};
const result = logRow('category', 40, 50, false);
expect(result).toStrictEqual(expected);
});
test('getCategoryScores grabs the 3 categories and returns a percentage', () => {
const result = getCategoryScores(data);
expect(result.ally).toEqual(100);
expect(result.bestPractises).toEqual(41);
expect(result.seo).toEqual(50);
});
describe('isAboveThreshold', () => {
test('If score is above the budget return true', () => {
const result = isAboveThreshold(60, 50);
expect(result).toEqual(true);
});
test('If score equals the budget return true', () => {
const result = isAboveThreshold(50, 50);
expect(result).toEqual(true);
});
test('If the score is below the budget reutnr false', () => {
const result = isAboveThreshold(10, 100);
expect(result).toEqual(false);
});
});
const budgetTest = (title, scores, expectedResult) => {
test(title, () => {
const result = compareToBudget(testCategories, scores, testBudget);
expect(result).toEqual(expectedResult);
});
};
describe('compareToBudget', () => {
budgetTest(
'Returns true if all values are above a threshold',
resultMock(100, 80, 20),
true,
);
budgetTest(
'Returns false if one or more values are wrong',
resultMock(90, 80, 20),
false,
);
test('The function prints the result to console.table', () => {
expect(global.console.table).toBeCalled();
});
});
test('readReport reads a file and parses it to Json', () => {
readReport();
expect(fs.readFileSync).toBeCalled();
expect(global.JSON.parse).toBeCalled();
});
test('exitResult', () => {
const mockExit = jest
.spyOn(process, 'exit')
.mockImplementation(number => number);
exitResult(false);
expect(mockExit).toBeCalled();
});