|
1 | 1 | import {userEvent} from '../../'
|
2 |
| -import {setup} from './helpers/utils' |
| 2 | +import {setup, addListeners} from './helpers/utils' |
| 3 | + |
| 4 | +test('fires events when tabbing between two elements', async () => { |
| 5 | + const {element, getEventSnapshot, clearEventCalls} = setup( |
| 6 | + `<div><input id="a" /><input id="b" /></div>`, |
| 7 | + ) |
| 8 | + const a = element.children[0] |
| 9 | + const b = element.children[1] |
| 10 | + await userEvent.focus(a) |
| 11 | + clearEventCalls() |
| 12 | + |
| 13 | + const aListeners = addListeners(a) |
| 14 | + const bListeners = addListeners(b) |
| 15 | + |
| 16 | + await userEvent.tab() |
| 17 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 18 | + Events fired on: div |
| 19 | + |
| 20 | + input#a[value=""] - keydown: Tab (9) |
| 21 | + input#a[value=""] - focusout |
| 22 | + input#b[value=""] - focusin |
| 23 | + input#b[value=""] - keyup: Tab (9) |
| 24 | + `) |
| 25 | + // blur/focus do not bubble |
| 26 | + expect(aListeners.eventWasFired('blur')).toBe(true) |
| 27 | + expect(bListeners.eventWasFired('focus')).toBe(true) |
| 28 | +}) |
| 29 | + |
| 30 | +test('does not change focus if default prevented on keydown', async () => { |
| 31 | + const {element, getEventSnapshot, clearEventCalls} = setup( |
| 32 | + `<div><input id="a" /><input id="b" /></div>`, |
| 33 | + ) |
| 34 | + const a = element.children[0] |
| 35 | + const b = element.children[1] |
| 36 | + await userEvent.focus(a) |
| 37 | + clearEventCalls() |
| 38 | + |
| 39 | + const aListeners = addListeners(a, { |
| 40 | + eventHandlers: {keyDown: e => e.preventDefault()}, |
| 41 | + }) |
| 42 | + const bListeners = addListeners(b) |
| 43 | + |
| 44 | + await userEvent.tab() |
| 45 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 46 | + Events fired on: div |
| 47 | + |
| 48 | + input#a[value=""] - keydown: Tab (9) |
| 49 | + input#a[value=""] - keyup: Tab (9) |
| 50 | + `) |
| 51 | + // blur/focus do not bubble |
| 52 | + expect(aListeners.eventWasFired('blur')).toBe(false) |
| 53 | + expect(bListeners.eventWasFired('focus')).toBe(false) |
| 54 | +}) |
| 55 | + |
| 56 | +test('fires correct events with shift key', async () => { |
| 57 | + const {element, getEventSnapshot, clearEventCalls} = setup( |
| 58 | + `<div><input id="a" /><input id="b" /></div>`, |
| 59 | + ) |
| 60 | + const a = element.children[0] |
| 61 | + const b = element.children[1] |
| 62 | + await userEvent.focus(b) |
| 63 | + clearEventCalls() |
| 64 | + |
| 65 | + const aListeners = addListeners(a) |
| 66 | + const bListeners = addListeners(b) |
| 67 | + |
| 68 | + await userEvent.tab({shift: true}) |
| 69 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 70 | + Events fired on: div |
| 71 | +
|
| 72 | + input#b[value=""] - keydown: Shift (16) {shift} |
| 73 | + input#b[value=""] - keydown: Tab (9) {shift} |
| 74 | + input#b[value=""] - focusout |
| 75 | + input#a[value=""] - focusin |
| 76 | + input#a[value=""] - keyup: Tab (9) {shift} |
| 77 | + input#a[value=""] - keyup: Shift (16) |
| 78 | + `) |
| 79 | + // blur/focus do not bubble |
| 80 | + expect(aListeners.eventWasFired('focus')).toBe(true) |
| 81 | + expect(bListeners.eventWasFired('blur')).toBe(true) |
| 82 | +}) |
3 | 83 |
|
4 | 84 | test('should cycle elements in document tab order', async () => {
|
5 | 85 | setup(`
|
|
0 commit comments