|
| 1 | +import userEvent from '../../index' |
| 2 | +import {addListeners, setup} from '../helpers/utils' |
| 3 | + |
| 4 | +it('type without focus', () => { |
| 5 | + const {element} = setup('<input/>') |
| 6 | + const {getEventSnapshot} = addListeners(document.body) |
| 7 | + |
| 8 | + userEvent.keyboard('foo') |
| 9 | + |
| 10 | + expect(element).toHaveValue('') |
| 11 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 12 | + Events fired on: body |
| 13 | +
|
| 14 | + body - keydown: f (102) |
| 15 | + body - keypress: f (102) |
| 16 | + body - keyup: f (102) |
| 17 | + body - keydown: o (111) |
| 18 | + body - keypress: o (111) |
| 19 | + body - keyup: o (111) |
| 20 | + body - keydown: o (111) |
| 21 | + body - keypress: o (111) |
| 22 | + body - keyup: o (111) |
| 23 | + `) |
| 24 | +}) |
| 25 | + |
| 26 | +it('type with focus', () => { |
| 27 | + const {element} = setup('<input/>') |
| 28 | + const {getEventSnapshot} = addListeners(document.body) |
| 29 | + ;(element as HTMLInputElement).focus() |
| 30 | + |
| 31 | + userEvent.keyboard('foo') |
| 32 | + |
| 33 | + expect(element).toHaveValue('foo') |
| 34 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 35 | + Events fired on: body |
| 36 | +
|
| 37 | + input[value=""] - focusin |
| 38 | + input[value=""] - keydown: f (102) |
| 39 | + input[value=""] - keypress: f (102) |
| 40 | + input[value="f"] - input |
| 41 | + input[value="f"] - keyup: f (102) |
| 42 | + input[value="f"] - keydown: o (111) |
| 43 | + input[value="f"] - keypress: o (111) |
| 44 | + input[value="fo"] - input |
| 45 | + input[value="fo"] - keyup: o (111) |
| 46 | + input[value="fo"] - keydown: o (111) |
| 47 | + input[value="fo"] - keypress: o (111) |
| 48 | + input[value="foo"] - input |
| 49 | + input[value="foo"] - keyup: o (111) |
| 50 | + `) |
| 51 | +}) |
| 52 | + |
| 53 | +it('type asynchronous', async () => { |
| 54 | + const {element} = setup('<input/>') |
| 55 | + const {getEventSnapshot} = addListeners(document.body) |
| 56 | + ;(element as HTMLInputElement).focus() |
| 57 | + |
| 58 | + // eslint-disable-next-line testing-library/no-await-sync-events |
| 59 | + await userEvent.keyboard('foo', {delay: 1}) |
| 60 | + |
| 61 | + expect(element).toHaveValue('foo') |
| 62 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 63 | + Events fired on: body |
| 64 | +
|
| 65 | + input[value=""] - focusin |
| 66 | + input[value=""] - keydown: f (102) |
| 67 | + input[value=""] - keypress: f (102) |
| 68 | + input[value="f"] - input |
| 69 | + input[value="f"] - keyup: f (102) |
| 70 | + input[value="f"] - keydown: o (111) |
| 71 | + input[value="f"] - keypress: o (111) |
| 72 | + input[value="fo"] - input |
| 73 | + input[value="fo"] - keyup: o (111) |
| 74 | + input[value="fo"] - keydown: o (111) |
| 75 | + input[value="fo"] - keypress: o (111) |
| 76 | + input[value="foo"] - input |
| 77 | + input[value="foo"] - keyup: o (111) |
| 78 | + `) |
| 79 | +}) |
| 80 | + |
| 81 | +describe('error', () => { |
| 82 | + afterEach(() => { |
| 83 | + ;(console.error as jest.MockedFunction<typeof console.error>).mockClear() |
| 84 | + }) |
| 85 | + |
| 86 | + it('error in sync', async () => { |
| 87 | + const err = jest.spyOn(console, 'error') |
| 88 | + err.mockImplementation(() => {}) |
| 89 | + |
| 90 | + userEvent.keyboard('{!') |
| 91 | + |
| 92 | + // the catch will be asynchronous |
| 93 | + await Promise.resolve() |
| 94 | + |
| 95 | + expect(err).toHaveBeenCalledWith(expect.any(Error)) |
| 96 | + expect(err.mock.calls[0][0]).toHaveProperty( |
| 97 | + 'message', |
| 98 | + 'Expected key descriptor but found "!" in "{!"', |
| 99 | + ) |
| 100 | + }) |
| 101 | + |
| 102 | + it('error in async', async () => { |
| 103 | + const promise = userEvent.keyboard('{!', {delay: 1}) |
| 104 | + |
| 105 | + return expect(promise).rejects.toThrowError( |
| 106 | + 'Expected key descriptor but found "!" in "{!"', |
| 107 | + ) |
| 108 | + }) |
| 109 | +}) |
| 110 | + |
| 111 | +it('continue typing with state', () => { |
| 112 | + const {element, getEventSnapshot, clearEventCalls} = setup('<input/>') |
| 113 | + ;(element as HTMLInputElement).focus() |
| 114 | + clearEventCalls() |
| 115 | + |
| 116 | + const state = userEvent.keyboard('[ShiftRight>]') |
| 117 | + |
| 118 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 119 | + Events fired on: input[value=""] |
| 120 | +
|
| 121 | + input[value=""] - keydown: Shift (16) {shift} |
| 122 | + `) |
| 123 | + clearEventCalls() |
| 124 | + |
| 125 | + userEvent.keyboard('F[/ShiftRight]', {keyboardState: state}) |
| 126 | + |
| 127 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 128 | + Events fired on: input[value="F"] |
| 129 | +
|
| 130 | + input[value=""] - keydown: F (70) {shift} |
| 131 | + input[value=""] - keypress: F (70) {shift} |
| 132 | + input[value="F"] - input |
| 133 | + "{CURSOR}" -> "F{CURSOR}" |
| 134 | + input[value="F"] - keyup: F (70) {shift} |
| 135 | + input[value="F"] - keyup: Shift (16) |
| 136 | + `) |
| 137 | +}) |
0 commit comments