@@ -3,6 +3,8 @@ enum bracketDict {
3
3
'[' = ']' ,
4
4
}
5
5
6
+ type Context = 'pointer' | 'keyboard'
7
+
6
8
/**
7
9
* Read the next key definition from user input
8
10
*
@@ -14,7 +16,7 @@ enum bracketDict {
14
16
* When keeping the key pressed you can choose how long the key is pressed `{descriptor>3}`.
15
17
* You can then release the key per `{descriptor>3/}` or keep it pressed and continue with the next key.
16
18
*/
17
- export function readNextDescriptor ( text : string ) {
19
+ export function readNextDescriptor ( text : string , context : Context ) {
18
20
let pos = 0
19
21
const startBracket =
20
22
text [ pos ] in bracketDict ? ( text [ pos ] as keyof typeof bracketDict ) : ''
@@ -27,14 +29,16 @@ export function readNextDescriptor(text: string) {
27
29
28
30
return {
29
31
type,
30
- ...( type === '' ? readPrintableChar ( text , pos ) : readTag ( text , pos , type ) ) ,
32
+ ...( type === ''
33
+ ? readPrintableChar ( text , pos , context )
34
+ : readTag ( text , pos , type , context ) ) ,
31
35
}
32
36
}
33
37
34
- function readPrintableChar ( text : string , pos : number ) {
38
+ function readPrintableChar ( text : string , pos : number , context : Context ) {
35
39
const descriptor = text [ pos ]
36
40
37
- assertDescriptor ( descriptor , text , pos )
41
+ assertDescriptor ( descriptor , text , pos , context )
38
42
39
43
pos += descriptor . length
40
44
@@ -51,6 +55,7 @@ function readTag(
51
55
text : string ,
52
56
pos : number ,
53
57
startBracket : keyof typeof bracketDict ,
58
+ context : Context ,
54
59
) {
55
60
const releasePreviousModifier = text [ pos ] === '/' ? '/' : ''
56
61
@@ -64,7 +69,7 @@ function readTag(
64
69
? text [ pos ]
65
70
: text . slice ( pos ) . match ( startBracket === '{' ? / ^ \w + | ^ [ ^ } > / ] / : / ^ \w + / ) ?. [ 0 ]
66
71
67
- assertDescriptor ( descriptor , text , pos )
72
+ assertDescriptor ( descriptor , text , pos , context )
68
73
69
74
pos += descriptor . length
70
75
@@ -92,6 +97,7 @@ function readTag(
92
97
. join ( ' or ' ) ,
93
98
text [ pos ] ,
94
99
text ,
100
+ context ,
95
101
) ,
96
102
)
97
103
}
@@ -111,9 +117,10 @@ function assertDescriptor(
111
117
descriptor : string | undefined ,
112
118
text : string ,
113
119
pos : number ,
120
+ context : Context ,
114
121
) : asserts descriptor is string {
115
122
if ( ! descriptor ) {
116
- throw new Error ( getErrorMessage ( 'key descriptor' , text [ pos ] , text ) )
123
+ throw new Error ( getErrorMessage ( 'key descriptor' , text [ pos ] , text , context ) )
117
124
}
118
125
}
119
126
@@ -131,8 +138,13 @@ function getErrorMessage(
131
138
expected : string ,
132
139
found : string | undefined ,
133
140
text : string ,
141
+ context : Context ,
134
142
) {
135
143
return `Expected ${ expected } but found "${ found ?? '' } " in "${ text } "
136
- See https://github.com/testing-library/user-event/blob/main/README.md#keyboardtext-options
144
+ See ${
145
+ context === 'pointer'
146
+ ? `https://testing-library.com/docs/user-event/pointer#pressing-a-button-or-touching-the-screen`
147
+ : `https://testing-library.com/docs/user-event/keyboard`
148
+ }
137
149
for more information about how userEvent parses your input.`
138
150
}
0 commit comments