This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
161 lines (140 loc) · 3.72 KB
/
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
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
const happoDeepCompare = require('.');
const makeRequest = require('./makeRequest');
jest.mock('./makeRequest');
let subject;
let opts;
let log;
let compareResult;
let env;
beforeEach(() => {
log = [];
compareResult = {
summary: 'Mocked summary',
equal: false,
diffs: [
[
{
url: 'https://happo.io/img/happo-io/93eb2d9e57d43b0b5ca5527587484f18',
component: 'Foo',
variant: 'bar',
target: 'chrome',
},
{
url: 'https://happo.io/img/happo-io/4a340716fa580b80d9f87330d79903dc',
component: 'Foo',
variant: 'bar',
target: 'chrome',
},
],
],
};
makeRequest.mockImplementation(() => Promise.resolve(compareResult));
opts = {
sha1: 'abc',
sha2: 'xyz',
apiKey: 'foo',
apiSecret: 'bar',
};
env = {};
subject = () => happoDeepCompare(opts, env, msg => log.push(msg));
});
it('uses a default threshold', async () => {
const result = await subject();
expect(log).toEqual([
'Comparing abc with xyz...',
'Found 1 diffs to deep-compare using threshold 0.05',
'✓ Foo - bar - chrome diff (0.0004244855470602604) is within threshold',
'Mocked summary',
]);
expect(result.resolved).toEqual(compareResult.diffs);
});
describe('when threshold is lower than diff', () => {
beforeEach(() => {
opts.threshold = 0.0001;
});
it('reports diffs', async () => {
const result = await subject();
expect(log).toEqual([
'Comparing abc with xyz...',
'Found 1 diffs to deep-compare using threshold 0.0001',
'✗ Foo - bar - chrome diff (0.0004244855470602604) is larger than threshold',
'Mocked summary',
]);
expect(result.resolved).toEqual([]);
});
});
describe('when apiKey is missing', () => {
beforeEach(() => {
delete opts.apiKey;
});
it('throws an error', async () => {
await expect(subject()).rejects.toEqual(
new Error('Missing `apiKey` argument'),
);
});
});
describe('when apiSecret is missing', () => {
beforeEach(() => {
delete opts.apiSecret;
});
it('throws an error', async () => {
await expect(subject()).rejects.toEqual(
new Error('Missing `apiSecret` argument'),
);
});
});
describe('when sha1 is missing', () => {
beforeEach(() => {
delete opts.sha1;
});
it('throws an error', async () => {
await expect(subject()).rejects.toEqual(
new Error('Missing `sha1` argument'),
);
});
});
describe('when sha2 is missing', () => {
beforeEach(() => {
delete opts.sha2;
});
it('throws an error', async () => {
await expect(subject()).rejects.toEqual(
new Error('Missing `sha2` argument'),
);
});
});
describe('when threshold is too large', () => {
beforeEach(() => {
opts.threshold = 12;
});
it('throws an error', async () => {
await expect(subject()).rejects.toEqual(
new Error('Argument `threshold` needs to be a number between 0 and 1'),
);
});
});
describe('when threshold is not a number', () => {
beforeEach(() => {
opts.threshold = '0.2';
});
it('throws an error', async () => {
await expect(subject()).rejects.toEqual(
new Error('Argument `threshold` needs to be a number between 0 and 1'),
);
});
});
describe('when threshold is larger than diff', () => {
beforeEach(() => {
opts.threshold = 0.1;
});
it('returns the diff that was deep-compared', async () => {
const result = await subject();
expect(result.resolved).toEqual(compareResult.diffs);
expect(log).toEqual([
'Comparing abc with xyz...',
'Found 1 diffs to deep-compare using threshold 0.1',
'✓ Foo - bar - chrome diff (0.0004244855470602604) is within threshold',
'Mocked summary',
]);
});
});