|
| 1 | +import {userEvent} from '../../' |
| 2 | +import {setup} from './helpers/utils' |
| 3 | + |
| 4 | +test('blur a button', async () => { |
| 5 | + const {element, getEventSnapshot, clearEventCalls} = setup(`<button />`) |
| 6 | + await userEvent.focus(element) |
| 7 | + clearEventCalls() |
| 8 | + await userEvent.blur(element) |
| 9 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 10 | + Events fired on: button |
| 11 | +
|
| 12 | + button - blur |
| 13 | + button - focusout |
| 14 | + `) |
| 15 | + expect(element).not.toHaveFocus() |
| 16 | +}) |
| 17 | + |
| 18 | +test('no events fired on an unblurable input', async () => { |
| 19 | + const {element, getEventSnapshot, clearEventCalls} = setup(`<div />`) |
| 20 | + await userEvent.focus(element) |
| 21 | + clearEventCalls() |
| 22 | + await userEvent.blur(element) |
| 23 | + expect(getEventSnapshot()).toMatchInlineSnapshot( |
| 24 | + `No events were fired on: div`, |
| 25 | + ) |
| 26 | + expect(element).not.toHaveFocus() |
| 27 | +}) |
| 28 | + |
| 29 | +test('blur with tabindex', async () => { |
| 30 | + const {element, getEventSnapshot, clearEventCalls} = setup( |
| 31 | + `<div tabindex="0" />`, |
| 32 | + ) |
| 33 | + await userEvent.focus(element) |
| 34 | + clearEventCalls() |
| 35 | + await userEvent.blur(element) |
| 36 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 37 | + Events fired on: div |
| 38 | +
|
| 39 | + div - blur |
| 40 | + div - focusout |
| 41 | + `) |
| 42 | + expect(element).not.toHaveFocus() |
| 43 | +}) |
| 44 | + |
| 45 | +test('no events fired on a disabled blurable input', async () => { |
| 46 | + const {element, getEventSnapshot, clearEventCalls} = setup( |
| 47 | + `<button disabled />`, |
| 48 | + ) |
| 49 | + await userEvent.focus(element) |
| 50 | + clearEventCalls() |
| 51 | + await userEvent.blur(element) |
| 52 | + expect(getEventSnapshot()).toMatchInlineSnapshot( |
| 53 | + `No events were fired on: button`, |
| 54 | + ) |
| 55 | + expect(element).not.toHaveFocus() |
| 56 | +}) |
| 57 | + |
| 58 | +test('no events fired if the element is not focused', async () => { |
| 59 | + const {element, getEventSnapshot} = setup(`<button />`) |
| 60 | + await userEvent.blur(element) |
| 61 | + expect(getEventSnapshot()).toMatchInlineSnapshot( |
| 62 | + `No events were fired on: button`, |
| 63 | + ) |
| 64 | + expect(element).not.toHaveFocus() |
| 65 | +}) |
0 commit comments