Skip to content

docs: userEvent/convenience Example Code #1283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 25, 2023
115 changes: 115 additions & 0 deletions docs/user-event/api-convenience.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ click(element: Element): Promise<void>
pointer([{target: element}, {keys: '[MouseLeft]', target: element}])
```

```js
test('click', async () => {
const onChange = jest.fn()
const user = userEvent.setup()

render(<input type="checkbox" onChange={onChange} />)

const checkbox = screen.getByRole('checkbox')

await user.click(checkbox)

expect(onChange).toHaveBeenCalledTimes(1)
expect(checkbox).toBeChecked()
})
```

The first action might be skipped per [`skipHover`](options.mdx#skiphover).

### dblClick()
Expand All @@ -30,6 +46,22 @@ dblClick(element: Element): Promise<void>
pointer([{target: element}, {keys: '[MouseLeft][MouseLeft]', target: element}])
```

```js
test('double click', async () => {
const onChange = jest.fn()
const user = userEvent.setup()

render(<input type="checkbox" onChange={onChange} />)

const checkbox = screen.getByRole('checkbox')

await user.dblClick(checkbox)

expect(onChange).toHaveBeenCalledTimes(2)
expect(checkbox).not.toBeChecked()
})
```

### tripleClick()

```ts
Expand All @@ -43,6 +75,22 @@ pointer([
])
```

```js
test('triple click', async () => {
const onChange = jest.fn()
const user = userEvent.setup()

render(<input type="checkbox" onChange={onChange} />)

const checkbox = screen.getByRole('checkbox')

await user.tripleClick(checkbox)

expect(onChange).toHaveBeenCalledTimes(3)
expect(checkbox).toBeChecked()
})
```

## Mouse movement

### hover()
Expand All @@ -55,6 +103,33 @@ hover(element: Element): Promise<void>
pointer({target: element})
```

```js
test('hover/unhover', async () => {
const user = userEvent.setup()
render(<div>Hover</div>)

const hoverBox = screen.getByText('Hover')
let isHover = false

hoverBox.addEventListener('mouseover', () => {
isHover = true
})
hoverBox.addEventListener('mouseout', () => {
isHover = false
})

expect(isHover).toBeFalsy()

await user.hover(hoverBox)

expect(isHover).toBeTruthy()

await user.unhover(hoverBox)

expect(isHover).toBeFalsy()
})
```

### unhover()

```ts
Expand All @@ -81,3 +156,43 @@ keyboard('{Shift>}{Tab}{/Shift}')
// with shift=false
keyboard('[/ShiftLeft][/ShiftRight]{Tab}')
```

```js
test('tab', async () => {
const user = userEvent.setup()
render(
<div>
<input type="checkbox" />
<input type="radio" />
<input type="number" />
</div>,
)

const checkbox = screen.getByRole('checkbox')
const radio = screen.getByRole('radio')
const number = screen.getByRole('spinbutton')

expect(document.body).toHaveFocus()

await user.tab()

expect(checkbox).toHaveFocus()

await user.tab()

expect(radio).toHaveFocus()

await user.tab()

expect(number).toHaveFocus()

await user.tab()

// cycle goes back to the body element
expect(document.body).toHaveFocus()

await user.tab()

expect(checkbox).toHaveFocus()
})
```