Skip to content

Commit

Permalink
feat: add special keys to userEvent.type()
Browse files Browse the repository at this point in the history
  • Loading branch information
malwilley committed May 25, 2020
1 parent 6239c3a commit f6d606a
Show file tree
Hide file tree
Showing 7 changed files with 758 additions and 126 deletions.
24 changes: 22 additions & 2 deletions README.md
Expand Up @@ -158,8 +158,8 @@ import userEvent from '@testing-library/user-event'
test('type', async () => {
render(<textarea />)

await userEvent.type(screen.getByRole('textbox'), 'Hello, World!')
expect(screen.getByRole('textbox')).toHaveAttribute('value', 'Hello, World!')
await userEvent.type(screen.getByRole('textbox'), 'Hello{enter}World!')
expect(screen.getByRole('textbox')).toHaveValue('Hello\nWorld!')
})
```

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

#### Special characters

The following special character strings are supported:

| Text string | Key | Modifier | Notes |
| ------------- | --------- | ---------- | ------------------------------------------------------ |
| `{enter}` | Enter | N/A | Will insert a newline character (`<textarea />` only). |
| `{esc}` | Escape | N/A | |
| `{backspace}` | Backspace | N/A | Will delete the previous character. |
| `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
| `{ctrl}` | Control | `ctrlKey` | |
| `{alt}` | Alt | `altKey` | |
| `{meta}` | OS | `metaKey` | |

> **A note about modifiers:** Modifier keys (`{shift}`, `{ctrl}`, `{alt}`,
> `{meta}`) will activate their corresponding event modifiers for the duration
> of type command. Keys are released in the same sequence that they were invoked
> at the end of typing.
### `upload(element, file, [{ clickInit, changeInit }])`

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

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down
171 changes: 141 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -43,6 +43,7 @@
"@testing-library/dom": "^7.5.7",
"@testing-library/jest-dom": "^5.8.0",
"@testing-library/react": "^10.0.4",
"jest-diff": "^26.0.1",
"kcd-scripts": "^6.0.1",
"react": "^16.13.1",
"react-dom": "^16.13.1"
Expand Down

0 comments on commit f6d606a

Please sign in to comment.