forked from fabricjs/fabric.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jest.extend.ts
141 lines (135 loc) · 3.94 KB
/
jest.extend.ts
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
import { expect } from '@jest/globals';
import { toMatchSnapshot } from 'jest-snapshot';
import type { CloneDeepWithCustomizer } from 'lodash';
import { cloneDeepWith } from 'lodash';
import { FabricObject } from './src/shapes/Object/Object';
import type { TMat2D } from './src/typedefs';
type ExtendedOptions<T = unknown> =
| {
cloneDeepWith?: CloneDeepWithCustomizer<T>;
} & object;
type ObjectOptions<T = unknown> = ExtendedOptions<T> & {
includeDefaultValues?: boolean;
};
export const roundSnapshotOptions = {
cloneDeepWith: (value: any) => {
if (typeof value === 'number') {
return Math.round(value);
}
},
};
expect.extend({
toMatchSnapshot(
this: any,
received: any,
propertiesOrHint?: ExtendedOptions,
hint?: string
) {
if (typeof received === 'string') {
return toMatchSnapshot.call(
this,
received,
propertiesOrHint || hint || ''
);
}
const { cloneDeepWith: customizer, ...properties } = propertiesOrHint || {};
return toMatchSnapshot.call(
this,
customizer ? cloneDeepWith(received, customizer) : received,
properties,
hint
);
},
toMatchObjectSnapshot(
this: any,
received: FabricObject | Record<string, any>,
{
cloneDeepWith: customizer,
includeDefaultValues,
...properties
}: ObjectOptions = {},
hint?: string
) {
let snapshot: Record<string, any>;
if (received instanceof FabricObject) {
const restore = received.includeDefaultValues;
typeof includeDefaultValues === 'boolean' &&
(received.includeDefaultValues = includeDefaultValues);
snapshot = received.toObject();
received.includeDefaultValues = restore;
} else {
snapshot = received;
}
delete snapshot.version;
return toMatchSnapshot.call(
this,
cloneDeepWith(snapshot, (value, key, object, stack) => {
const clone = customizer?.(value, key, object, stack);
if (clone) {
return clone;
} else if (key === 'width') {
return Math.round(value);
}
}),
properties,
hint
);
},
toEqualRoundedMatrix(actual: TMat2D, expected: TMat2D, precision = 10) {
const error = Math.pow(10, -precision);
return {
message: () => {
return `expected ${this.utils.printReceived(
actual
)} to be rounded to ${this.utils.printExpected(
expected.map((x) => Math.round(x / error) * error)
)}`;
},
pass: actual.every((x, i) => Math.abs(x - expected[i]) < error),
};
},
});
// // written in official docs but DOESN'T work
// declare module 'expect' {
// interface AsymmetricMatchers {
// toMatchSnapshot(propertiesOrHint?: ExtendedOptions, hint?: string): void;
// }
// interface Matchers<R, T> {
// toMatchSnapshot(propertiesOrHint?: ExtendedOptions<T>, hint?: string): R;
// }
// }
// not written in official docs but DOES work
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface AsymmetricMatchers {
toMatchSnapshot(
propertiesOrHint?: ExtendedOptions | string,
hint?: string
): void;
toMatchObjectSnapshot(
propertiesOrHint?: ObjectOptions | string,
hint?: string
): void;
toEqualRoundedMatrix(expected: TMat2D, precision?: number): void;
}
interface Matchers<R, T> {
toMatchSnapshot<U extends { [P in keyof T]: any }>(
propertyMatchers: Partial<
U & { cloneDeepWith: CloneDeepWithCustomizer<T> }
>,
snapshotName?: string
): R;
toMatchObjectSnapshot<U extends { [P in keyof T]: any }>(
propertyMatchers?: Partial<
U & {
cloneDeepWith: CloneDeepWithCustomizer<T>;
includeDefaultValues?: boolean;
}
>,
snapshotName?: string
): R;
toEqualRoundedMatrix(expected: TMat2D, precision?: number): R;
}
}
}