Skip to content

Commit

Permalink
Support onLoad and onError on <link> (facebook#11825)
Browse files Browse the repository at this point in the history
* Support link event on Fiber component

* Update unit test

* prettier format

* Update test description

* Update ReactDOMComponent-test.js
  • Loading branch information
roderickhsiao authored and yenshih committed Jan 6, 2018
1 parent faa1f40 commit 9665119
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,42 @@ describe('ReactDOMComponent', () => {
expect(console.log.calls.argsFor(1)[0]).toContain('onLoad called');
}
});

it('should receive a load event on <link> elements', () => {
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 receive an error event on <link> elements', () => {
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);
});
});

describe('updateComponent', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/react-dom/src/client/ReactDOMFiberComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ export function setInitialProperties(
break;
case 'img':
case 'image':
case 'link':
trapBubbledEvent('topError', 'error', domElement);
trapBubbledEvent('topLoad', 'load', domElement);
props = rawProps;
Expand Down Expand Up @@ -870,6 +871,7 @@ export function diffHydratedProperties(
break;
case 'img':
case 'image':
case 'link':
trapBubbledEvent('topError', 'error', domElement);
trapBubbledEvent('topLoad', 'load', domElement);
break;
Expand Down

0 comments on commit 9665119

Please sign in to comment.