-
Notifications
You must be signed in to change notification settings - Fork 47.9k
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
Support onLoad and onError on <link> #11825
Changes from 3 commits
e07c7f1
a8b8a8c
f92bac0
cb09630
e5604bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1394,6 +1394,42 @@ describe('ReactDOMComponent', () => { | |
expect(console.log.calls.argsFor(1)[0]).toContain('onLoad called'); | ||
} | ||
}); | ||
|
||
it('should work load event on <link> component', () => { | ||
const container = document.createElement('div'); | ||
const onLoad = jest.fn(); | ||
|
||
ReactDOM.render( | ||
<link href="http://example.org/link" onLoad={onLoad} />, | ||
container, | ||
); | ||
|
||
const loadEvent = document.createEvent('Event'); | ||
const link = container.getElementsByTagName('link')[0]; | ||
|
||
loadEvent.initEvent('load', false, false); | ||
link.dispatchEvent(loadEvent); | ||
|
||
expect(onLoad).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should work error event on <link> component', () => { | ||
const container = document.createElement('div'); | ||
const onError = jest.fn(); | ||
|
||
ReactDOM.render( | ||
<link href="http://example.org/link" onError={onError} />, | ||
container, | ||
); | ||
|
||
const errorEvent = document.createEvent('Event'); | ||
const link = container.getElementsByTagName('link')[0]; | ||
|
||
errorEvent.initEvent('error', false, false); | ||
link.dispatchEvent(errorEvent); | ||
|
||
expect(onError).toHaveBeenCalledTimes(1); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use const onLoad = jest.fn();
ReactDOM.render(<link href="http://example.org/link" onLoad={onLoad} />)
// ... dispatch
expect(onLoad).toHaveBeenCalledTimes(1) Also, could you make the |
||
}); | ||
|
||
describe('updateComponent', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -484,6 +484,7 @@ export function setInitialProperties( | |
break; | ||
case 'img': | ||
case 'image': | ||
case 'link': | ||
trapBubbledEvent('topError', 'error', domElement); | ||
trapBubbledEvent('topLoad', 'load', domElement); | ||
props = rawProps; | ||
|
@@ -858,6 +859,7 @@ export function diffHydratedProperties( | |
break; | ||
case 'img': | ||
case 'image': | ||
case 'link': | ||
trapBubbledEvent('topError', 'error', domElement); | ||
trapBubbledEvent('topLoad', 'load', domElement); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some background: I imagine we'll hit this again in the future as new HTML elements support onLoad/onError (though that's probably rare). As far as I understand it, we need to do this eager attachment because <div onLoad={() => alert('load!')>
<img src="image.jpg" />
</div> I sort of wonder if we should just respect the existing DOM behavior and avoid needing to eagerly attach these listeners. I'll write up an RFC for it! |
||
break; | ||
|
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.
I am sorry for two final nits, but do you mind updating this to read:
Likewise for
load
. I'm also happy to make this change if you'd like.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.
absolutely, thanks for the feedback