-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
jest.extend.js
100 lines (88 loc) · 2.13 KB
/
jest.extend.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
function toBeValidCoordinate(coord) {
// coordinate must have two dimensions
if (!Array.isArray(coord) || coord.length !== 2) {
return {
pass: false,
message: `coordinate must have two dimensions`,
};
}
// coordinate dimensions must be valid numbers
if (
typeof coord[0] !== 'number' ||
isNaN(coord[0]) ||
typeof coord[1] !== 'number' ||
isNaN(coord[1])
) {
return {
pass: false,
message: `coordinate dimensions must be valid numbers`,
};
}
// coordinate valid
return {
pass: true,
message: `coordinate valid`,
};
}
function toBeValidBounds(bounds) {
// null bounds are valid
if (bounds === null) {
return {
pass: true,
message: `null bounds are valid`,
};
}
// bounds must be a coordinate pair
if (!Array.isArray(bounds) || bounds.length !== 2) {
return {
pass: false,
message: `bounds must be a coordinate pair`,
};
}
// validate south-west
const validateSW = toBeValidCoordinate(bounds[0]);
if (!validateSW.pass) {
return validateSW;
}
// validate north-east
const validateNE = toBeValidCoordinate(bounds[1]);
if (!validateNE.pass) {
return validateNE;
}
// corners
const [south, west] = bounds[0];
const [north, east] = bounds[1];
// bounds represents a point
// note: this is not really a valid bounds but we will allow it
if (south === north && west === east) {
return {
pass: true,
message: `bounds represents a point`,
};
}
// west should be less than or equal to east
// @note: this is not the strictly case for bounds which cross the antimeridian
if (west > east) {
return {
pass: false,
message: `west should be less than or equal to east`,
};
}
// south should be less than or equal to north
// @note: this is not the strictly case for bounds which cross a pole
if (south > north) {
return {
pass: false,
message: `south should be less than or equal to north`,
};
}
// bounds valid
return {
pass: true,
message: `bounds valid`,
};
}
expect.extend({
toBeValidCoordinate,
toBeValidBounds,
});