-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjest-setup.ts
77 lines (70 loc) · 1.9 KB
/
jest-setup.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
import '@testing-library/jest-dom'
interface CustomMatchers<R = unknown> {
toHaveCaret(text: string): R
}
declare global {
namespace jest {
interface Expect extends CustomMatchers {}
interface Matchers<R> extends CustomMatchers<R> {}
interface InverseAsymmetricMatchers extends CustomMatchers {}
}
}
expect.extend({
toHaveCaret(received, text: string) {
if (!(received instanceof HTMLInputElement)) {
return {
message: () =>
this.utils.matcherErrorMessage(
this.utils.matcherHint('toHaveCaret', undefined, `"${text}"`, {
isNot: this.isNot,
promise: this.promise,
}),
`${this.utils
.RECEIVED_COLOR`received`} value must be an HTMLInputElement`,
this.utils.printWithType(
'Received',
received,
this.utils.printReceived
)
),
pass: Boolean(this.isNot),
}
}
let state = received.value
if (
document.activeElement === received &&
typeof received.selectionEnd === 'number'
) {
state =
state.slice(0, received.selectionEnd) +
'|' +
state.slice(received.selectionEnd)
}
if (
document.activeElement === received &&
typeof received.selectionStart === 'number' &&
received.selectionEnd !== received.selectionStart
) {
state =
state.slice(0, received.selectionStart) +
'|' +
state.slice(received.selectionStart)
}
return {
message: () =>
this.utils.matcherHint('toHaveCaret', undefined, `"${text}"`, {
isNot: this.isNot,
promise: this.promise,
}) +
'\n\n' +
this.utils.printDiffOrStringify(
text,
state,
'Expected',
'Received',
this.expand ?? false
),
pass: state === text,
}
},
})