Repository navigation
fix: copy non-enumerable Event fields into synthetic events #113
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
Open
BleedingDev
wants to merge
1
commit into
TanStack:main
Choose a base branch
from
BleedingDev:fix/synthetic-event-inherited-fields
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/redact': patch | ||
| --- | ||
|
|
||
| Copy non-enumerable Event fields into synthetic events. DOM implementations such as happy-dom define `type`, `target`, `currentTarget` and `composedPath` as non-enumerable accessors, so handlers saw them as `undefined` and controlled inputs never read `e.target.value` in happy-dom test suites. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // @vitest-environment happy-dom | ||
| /** | ||
| * happy-dom defines Event fields such as `type`, `target` and `currentTarget` | ||
| * as non-enumerable class accessors. Synthetic events must still expose them, | ||
| * or controlled inputs never see `e.target.value` in happy-dom test suites. | ||
| */ | ||
| import { describe, it, expect } from 'vitest' | ||
| import * as React from 'react' | ||
| import { createRoot } from 'react-dom/client' | ||
| import { flushSync } from 'react-dom' | ||
|
|
||
| function setup() { | ||
| const c = document.createElement('div') | ||
| document.body.appendChild(c) | ||
| return c | ||
| } | ||
|
|
||
| describe('synthetic events in happy-dom', () => { | ||
| it('keeps the base Event fields happy-dom does not enumerate', () => { | ||
| expect(Object.keys(Event.prototype)).not.toContain('target') | ||
| const container = setup() | ||
| let captured: any = null | ||
| function App() { | ||
| const [v, setV] = React.useState('') | ||
| return ( | ||
| <input | ||
| value={v} | ||
| onChange={(e: any) => { | ||
| captured = e | ||
| setV(e.target.value) | ||
| }} | ||
| /> | ||
| ) | ||
| } | ||
| flushSync(() => createRoot(container).render(<App />)) | ||
| const input = container.querySelector('input')! | ||
| flushSync(() => { | ||
| input.value = 'a' | ||
| input.dispatchEvent(new Event('input', { bubbles: true })) | ||
| }) | ||
| expect(captured.type).toBe('input') | ||
| expect(captured.target).toBe(input) | ||
| expect(captured.bubbles).toBe(true) | ||
| expect(captured.composedPath()[0]).toBe(input) | ||
| expect(input.value).toBe('a') | ||
| }) | ||
|
|
||
| it('reads subclass fields and currentTarget on a bubbled click', () => { | ||
| const container = setup() | ||
| let captured: any = null | ||
| let currentTarget: unknown = null | ||
| function App() { | ||
| return ( | ||
| <div | ||
| onClick={(e: any) => { | ||
| captured = e | ||
| currentTarget = e.currentTarget | ||
| }} | ||
| > | ||
| <button>go</button> | ||
| </div> | ||
| ) | ||
| } | ||
| flushSync(() => createRoot(container).render(<App />)) | ||
| const button = container.querySelector('button')! | ||
| flushSync(() => { | ||
| button.dispatchEvent(new MouseEvent('click', { bubbles: true, shiftKey: true })) | ||
| }) | ||
| expect(captured.type).toBe('click') | ||
| expect(captured.target).toBe(button) | ||
| expect(currentTarget).toBe(container.firstChild) | ||
| expect(captured.shiftKey).toBe(true) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not let an unrelated getter prevent event delivery.
If an
Eventsubclass definesget metadata() { throw new Error('unavailable') }, this loop readsmetadatabefore the handler runs. Class getters are non-enumerable, so the previousfor…inloop did not read it. Skip optional properties whose getters throw, or limit eager reads to the required event fields. (raw.githubusercontent.com)