Skip to content

Commit 1d59b8e

Browse files
committed
feat: add special keys to userEvent.clear
1 parent 6239c3a commit 1d59b8e

File tree

7 files changed

+767
-126
lines changed

7 files changed

+767
-126
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ import userEvent from '@testing-library/user-event'
158158
test('type', async () => {
159159
render(<textarea />)
160160

161-
await userEvent.type(screen.getByRole('textbox'), 'Hello, World!')
162-
expect(screen.getByRole('textbox')).toHaveAttribute('value', 'Hello, World!')
161+
await userEvent.type(screen.getByRole('textbox'), 'Hello{enter}World!')
162+
expect(screen.getByRole('textbox')).toHaveValue('Hello\nWorld!')
163163
})
164164
```
165165

@@ -170,6 +170,25 @@ one character at the time. `false` is the default value.
170170
are typed. By default it's 0. You can use this option if your component has a
171171
different behavior for fast or slow users.
172172

173+
#### Special characters
174+
175+
The following special character strings are supported:
176+
177+
| Text string | Key | Modifier | Notes |
178+
| ------------- | --------- | ---------- | ------------------------------------------------------ |
179+
| `{enter}` | Enter | N/A | Will insert a newline character (`<textarea />` only). |
180+
| `{esc}` | Escape | N/A | |
181+
| `{backspace}` | Backspace | N/A | Will delete the previous character. |
182+
| `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
183+
| `{ctrl}` | Control | `ctrlKey` | |
184+
| `{alt}` | Alt | `altKey` | |
185+
| `{meta}` | OS | `metaKey` | |
186+
187+
> **A note about modifiers:** Modifier keys (`{shift}`, `{ctrl}`, `{alt}`,
188+
> `{meta}`) will activate their corresponding event modifiers for the duration
189+
> of type command. Keys are released in the same sequence that they were invoked
190+
> at the end of typing.
191+
173192
### `upload(element, file, [{ clickInit, changeInit }])`
174193

175194
Uploads file to an `<input>`. For uploading multiple files use `<input>` with
@@ -400,6 +419,7 @@ Thanks goes to these people ([emoji key][emojis]):
400419

401420
<!-- markdownlint-enable -->
402421
<!-- prettier-ignore-end -->
422+
403423
<!-- ALL-CONTRIBUTORS-LIST:END -->
404424

405425
This project follows the [all-contributors][all-contributors] specification.

package-lock.json

Lines changed: 141 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@testing-library/dom": "^7.5.7",
4444
"@testing-library/jest-dom": "^5.8.0",
4545
"@testing-library/react": "^10.0.4",
46+
"jest-diff": "^26.0.1",
4647
"kcd-scripts": "^6.0.1",
4748
"react": "^16.13.1",
4849
"react-dom": "^16.13.1"

0 commit comments

Comments
 (0)