Skip to content

Commit b6c1bd6

Browse files
committed
feat(type): support special charaters (like {enter})
Closes testing-library#306 Closes testing-library#301 Closes testing-library#235 Closes testing-library#181 Closes testing-library#172 Closes testing-library#31
1 parent 5c4746b commit b6c1bd6

File tree

10 files changed

+895
-294
lines changed

10 files changed

+895
-294
lines changed

README.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ change the state of the checkbox.
5050
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
5151
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
5252

53+
5354
- [Installation](#installation)
5455
- [API](#api)
5556
- [`click(element)`](#clickelement)
@@ -158,8 +159,8 @@ import userEvent from '@testing-library/user-event'
158159
test('type', async () => {
159160
render(<textarea />)
160161

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

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

174+
#### Special characters
175+
176+
The following special character strings are supported:
177+
178+
| Text string | Key | Modifier | Notes |
179+
| ------------- | --------- | ---------- | ---------------------------------------------------------------------------------- |
180+
| `{enter}` | Enter | N/A | Will insert a newline character (`<textarea />` only). |
181+
| `{esc}` | Escape | N/A | |
182+
| `{backspace}` | Backspace | N/A | Will delete the previous character (or the characters within the `selectedRange`). |
183+
| `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
184+
| `{ctrl}` | Control | `ctrlKey` | |
185+
| `{alt}` | Alt | `altKey` | |
186+
| `{meta}` | OS | `metaKey` | |
187+
188+
> **A note about modifiers:** Modifier keys (`{shift}`, `{ctrl}`, `{alt}`,
189+
> `{meta}`) will activate their corresponding event modifiers for the duration
190+
> of type command or until they are closed (via `{/shift}`, `{/ctrl}`, etc.).
191+
192+
<!-- space out these notes -->
193+
194+
> We take the same
195+
> [stance as Cypress](https://docs.cypress.io/api/commands/type.html#Modifiers)
196+
> in that we do not simulate the behavior that happens with modifier key
197+
> combinations as different operating systems function differently in this
198+
> regard.
199+
173200
### `upload(element, file, [{ clickInit, changeInit }])`
174201

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

412439
<!-- markdownlint-enable -->
413440
<!-- prettier-ignore-end -->
441+
414442
<!-- ALL-CONTRIBUTORS-LIST:END -->
415443

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

0 commit comments

Comments
 (0)