|
1 | 1 | import React from 'react'
|
2 |
| -import { render } from 'react-testing-library' |
| 2 | +import { render, cleanup } from 'react-testing-library' |
3 | 3 | import 'jest-dom/extend-expect'
|
4 | 4 | import userEvent from '../src'
|
5 | 5 |
|
6 |
| -test('fireEvent.click simulates a user click', () => { |
7 |
| - const { getByTestId } = render( |
8 |
| - <React.Fragment> |
9 |
| - <input data-testid="A" /> |
10 |
| - <input data-testid="B" /> |
11 |
| - </React.Fragment> |
12 |
| - ) |
| 6 | +afterEach(cleanup) |
13 | 7 |
|
14 |
| - const a = getByTestId('A') |
15 |
| - const b = getByTestId('B') |
| 8 | +describe('fireEvent.click', () => { |
| 9 | + it('should fire the correct events for <input>', () => { |
| 10 | + const onMouseOver = jest.fn() |
| 11 | + const onMouseMove = jest.fn() |
| 12 | + const onMouseDown = jest.fn() |
| 13 | + const onFocus = jest.fn() |
| 14 | + const onMouseUp = jest.fn() |
| 15 | + const onClick = jest.fn() |
| 16 | + const { getByTestId } = render( |
| 17 | + <input |
| 18 | + data-testid="input" |
| 19 | + onMouseOver={onMouseOver} |
| 20 | + onMouseMove={onMouseMove} |
| 21 | + onMouseDown={onMouseDown} |
| 22 | + onFocus={onFocus} |
| 23 | + onMouseUp={onMouseUp} |
| 24 | + onClick={onClick} |
| 25 | + /> |
| 26 | + ) |
16 | 27 |
|
17 |
| - expect(a).not.toHaveFocus() |
18 |
| - expect(b).not.toHaveFocus() |
| 28 | + expect(onMouseOver).not.toHaveBeenCalled() |
| 29 | + expect(onMouseMove).not.toHaveBeenCalled() |
| 30 | + expect(onMouseDown).not.toHaveBeenCalled() |
| 31 | + expect(onFocus).not.toHaveBeenCalled() |
| 32 | + expect(onMouseUp).not.toHaveBeenCalled() |
| 33 | + expect(onClick).not.toHaveBeenCalled() |
19 | 34 |
|
20 |
| - userEvent.click(a) |
21 |
| - expect(a).toHaveFocus() |
22 |
| - expect(b).not.toHaveFocus() |
| 35 | + userEvent.click(getByTestId('input')) |
23 | 36 |
|
24 |
| - userEvent.click(b) |
25 |
| - expect(a).not.toHaveFocus() |
26 |
| - expect(a).not.toHaveFocus() |
| 37 | + expect(onMouseOver).toHaveBeenCalledTimes(1) |
| 38 | + expect(onMouseMove).toHaveBeenCalledTimes(1) |
| 39 | + expect(onMouseDown).toHaveBeenCalledTimes(1) |
| 40 | + expect(onFocus).toHaveBeenCalledTimes(1) |
| 41 | + expect(onMouseUp).toHaveBeenCalledTimes(1) |
| 42 | + expect(onClick).toHaveBeenCalledTimes(1) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should fire the correct events for <div>', () => { |
| 46 | + const onMouseOver = jest.fn() |
| 47 | + const onMouseMove = jest.fn() |
| 48 | + const onMouseDown = jest.fn() |
| 49 | + const onFocus = jest.fn() |
| 50 | + const onMouseUp = jest.fn() |
| 51 | + const onClick = jest.fn() |
| 52 | + const { getByTestId } = render( |
| 53 | + <div |
| 54 | + data-testid="div" |
| 55 | + onMouseOver={onMouseOver} |
| 56 | + onMouseMove={onMouseMove} |
| 57 | + onMouseDown={onMouseDown} |
| 58 | + onFocus={onFocus} |
| 59 | + onMouseUp={onMouseUp} |
| 60 | + onClick={onClick} |
| 61 | + /> |
| 62 | + ) |
| 63 | + |
| 64 | + expect(onMouseOver).not.toHaveBeenCalled() |
| 65 | + expect(onMouseMove).not.toHaveBeenCalled() |
| 66 | + expect(onMouseDown).not.toHaveBeenCalled() |
| 67 | + expect(onFocus).not.toHaveBeenCalled() |
| 68 | + expect(onMouseUp).not.toHaveBeenCalled() |
| 69 | + expect(onClick).not.toHaveBeenCalled() |
| 70 | + |
| 71 | + userEvent.click(getByTestId('div')) |
| 72 | + |
| 73 | + expect(onMouseOver).toHaveBeenCalledTimes(1) |
| 74 | + expect(onMouseMove).toHaveBeenCalledTimes(1) |
| 75 | + expect(onMouseDown).toHaveBeenCalledTimes(1) |
| 76 | + expect(onFocus).not.toHaveBeenCalled() |
| 77 | + expect(onMouseUp).toHaveBeenCalledTimes(1) |
| 78 | + expect(onClick).toHaveBeenCalledTimes(1) |
| 79 | + }) |
| 80 | + |
| 81 | + it('toggles the focus', () => { |
| 82 | + const { getByTestId } = render( |
| 83 | + <React.Fragment> |
| 84 | + <input data-testid="A" /> |
| 85 | + <input data-testid="B" /> |
| 86 | + </React.Fragment> |
| 87 | + ) |
| 88 | + |
| 89 | + const a = getByTestId('A') |
| 90 | + const b = getByTestId('B') |
| 91 | + |
| 92 | + expect(a).not.toHaveFocus() |
| 93 | + expect(b).not.toHaveFocus() |
| 94 | + |
| 95 | + userEvent.click(a) |
| 96 | + expect(a).toHaveFocus() |
| 97 | + expect(b).not.toHaveFocus() |
| 98 | + |
| 99 | + userEvent.click(b) |
| 100 | + expect(a).not.toHaveFocus() |
| 101 | + expect(a).not.toHaveFocus() |
| 102 | + }) |
| 103 | + |
| 104 | + it('gives focus when clicking a <label> with htmlFor', () => { |
| 105 | + const { getByTestId } = render( |
| 106 | + <React.Fragment> |
| 107 | + <label htmlFor="input" data-testid="label"> |
| 108 | + Label |
| 109 | + </label> |
| 110 | + <input id="input" data-testid="input" /> |
| 111 | + </React.Fragment> |
| 112 | + ) |
| 113 | + userEvent.click(getByTestId('label')) |
| 114 | + expect(getByTestId('input')).toHaveFocus() |
| 115 | + }) |
| 116 | + |
| 117 | + it('gives focus when clicking a <label> without htmlFor', () => { |
| 118 | + const { getByTestId } = render( |
| 119 | + <React.Fragment> |
| 120 | + <label data-testid="label"> |
| 121 | + <span>Label</span> |
| 122 | + <input data-testid="input" /> |
| 123 | + </label> |
| 124 | + </React.Fragment> |
| 125 | + ) |
| 126 | + userEvent.click(getByTestId('label')) |
| 127 | + expect(getByTestId('input')).toHaveFocus() |
| 128 | + }) |
27 | 129 | })
|
0 commit comments