Skip to content
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

fix: allow reassignment of focus and blur methods on `HTMLElement… #1265

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/document/patchFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ export function patchFocus(HTMLElement: typeof globalThis['HTMLElement']) {
// eslint-disable-next-line @typescript-eslint/unbound-method
const {focus, blur} = HTMLElement.prototype

let focusOverride: null | typeof patchedFocus = null
let blurOverride: null | typeof patchedBlur = null

jakeboone02 marked this conversation as resolved.
Show resolved Hide resolved
Object.defineProperties(HTMLElement.prototype, {
focus: {
configurable: true,
get: () => patchedFocus,
get: () => focusOverride ?? patchedFocus,
set: (f: typeof patchedFocus) => {
focusOverride = f
},
jakeboone02 marked this conversation as resolved.
Show resolved Hide resolved
},
blur: {
configurable: true,
get: () => patchedBlur,
get: () => blurOverride ?? patchedBlur,
set: (b: typeof patchedBlur) => {
blurOverride = b
},
jakeboone02 marked this conversation as resolved.
Show resolved Hide resolved
},
[patched]: {
configurable: true,
Expand Down
38 changes: 38 additions & 0 deletions tests/document/patchFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,44 @@ test('dispatch focus events', () => {
`)
})

describe('reassign focus and blur', () => {
let currentFocus: HTMLElement['focus'],
currentBlur: HTMLElement['blur']

beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/unbound-method
currentFocus = HTMLElement.prototype.focus
// eslint-disable-next-line @typescript-eslint/unbound-method
currentBlur = HTMLElement.prototype.blur
})

afterEach(() => {
HTMLElement.prototype.focus = currentFocus
HTMLElement.prototype.blur = currentBlur
})

test('set custom focus and blur', () => {
const {
elements: [a],
getEventSnapshot,
} = render(`<input id="a"/>`, {focus: false})

const mockFocus = mocks.fn()
HTMLElement.prototype.focus = mockFocus
const mockBlur = mocks.fn()
HTMLElement.prototype.blur = mockBlur

a.focus()
a.blur()

expect(getEventSnapshot()).toMatchInlineSnapshot(
`No events were fired on: input#a[value=""]`,
)
expect(mockFocus).toHaveBeenCalledTimes(1)
expect(mockBlur).toHaveBeenCalledTimes(1)
})
})

test('`focus` handler can prevent subsequent `focusin`', () => {
const {element, getEventSnapshot} = render(`<input/>`, {focus: false})

Expand Down