Skip to content

Commit 4eb02a1

Browse files
committed
fix(clear): make re-use type
1 parent c0e89ea commit 4eb02a1

File tree

4 files changed

+37
-51
lines changed

4 files changed

+37
-51
lines changed

src/user-event/__tests__/clear.js

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,13 @@ test('clears text', async () => {
88
expect(getEventCalls()).toMatchInlineSnapshot(`
99
Events fired on: input[value=""]
1010
11-
mouseover: Left (0)
12-
mousemove: Left (0)
13-
mousedown: Left (0)
1411
focus
15-
mouseup: Left (0)
16-
click: Left (0)
17-
mousedown: Left (0)
18-
mouseup: Left (0)
19-
click: Left (0)
20-
dblclick: Left (0)
2112
select
22-
keydown: Backspace (8)
23-
keyup: Backspace (8)
24-
input: "{SELECTION}hello{/SELECTION}" -> "hello"
25-
change
13+
select
14+
keydown: Delete (46)
15+
input: "{SELECTION}hello{/SELECTION}" -> ""
16+
select
17+
keyup: Delete (46)
2618
`)
2719
})
2820

@@ -42,19 +34,11 @@ test('does not clear text on readonly inputs', async () => {
4234
expect(getEventCalls()).toMatchInlineSnapshot(`
4335
Events fired on: input[value="hello"]
4436
45-
mouseover: Left (0)
46-
mousemove: Left (0)
47-
mousedown: Left (0)
4837
focus
49-
mouseup: Left (0)
50-
click: Left (0)
51-
mousedown: Left (0)
52-
mouseup: Left (0)
53-
click: Left (0)
54-
dblclick: Left (0)
5538
select
56-
keydown: Backspace (8)
57-
keyup: Backspace (8)
39+
select
40+
keydown: Delete (46)
41+
keyup: Delete (46)
5842
`)
5943
})
6044

src/user-event/clear.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {wrapAsync} from '../wrap-async'
2+
import {isInputElement} from './utils'
3+
import {type} from './type'
4+
5+
async function clear(element) {
6+
if (element.disabled) return
7+
// TODO: track the selection range ourselves so we don't have to do this input "type" trickery
8+
// just like cypress does: https://github.com/cypress-io/cypress/blob/8d7f1a0bedc3c45a2ebf1ff50324b34129fdc683/packages/driver/src/dom/selection.ts#L16-L37
9+
const elementType = element.type
10+
// type is a readonly property on textarea, so check if element is an input before trying to modify it
11+
if (isInputElement(element)) {
12+
// setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email"
13+
element.type = 'text'
14+
}
15+
await type(element, '{selectall}{del}')
16+
if (isInputElement(element)) {
17+
element.type = elementType
18+
}
19+
}
20+
clear = wrapAsync(clear)
21+
22+
export {clear}

src/user-event/index.js

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {wrapAsync} from '../wrap-async'
22
import {fireEvent} from './tick-fire-event'
3+
import {isInputElement} from './utils'
34
import {type} from './type'
45
import {tick} from './tick'
56

@@ -219,24 +220,6 @@ async function backspace(element) {
219220
}
220221
}
221222

222-
async function selectAll(element) {
223-
await dblClick(element) // simulate events (will not actually select)
224-
const elementType = element.type
225-
// type is a readonly property on textarea, so check if element is an input before trying to modify it
226-
if (isInputElement(element)) {
227-
// setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email"
228-
element.type = 'text'
229-
}
230-
element.setSelectionRange(0, element.value.length)
231-
if (isInputElement(element)) {
232-
element.type = elementType
233-
}
234-
}
235-
236-
function isInputElement(element) {
237-
return element.tagName.toLowerCase() === 'input'
238-
}
239-
240223
function getPreviouslyFocusedElement(element) {
241224
const focusedElement = element.ownerDocument.activeElement
242225
const wasAnotherElementFocused =
@@ -367,14 +350,6 @@ async function toggleSelectOptions(element, values, init) {
367350
}
368351
toggleSelectOptions = wrapAsync(toggleSelectOptions)
369352

370-
async function clear(element) {
371-
if (element.disabled) return
372-
373-
await selectAll(element)
374-
await backspace(element)
375-
}
376-
clear = wrapAsync(clear)
377-
378353
async function upload(element, fileOrFiles, {clickInit, changeInit} = {}) {
379354
if (element.disabled) return
380355
const focusedElement = element.ownerDocument.activeElement
@@ -496,13 +471,13 @@ export {
496471
dblClick,
497472
selectOptions,
498473
toggleSelectOptions,
499-
clear,
500474
type,
501475
upload,
502476
tab,
503477
hover,
504478
unhover,
505479
}
480+
export {clear} from './clear'
506481

507482
/*
508483
eslint

src/user-event/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function isInputElement(element) {
2+
return element.tagName.toLowerCase() === 'input'
3+
}
4+
5+
export {isInputElement}

0 commit comments

Comments
 (0)