Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The package exports a single component with the following props:
Called as soon as the script tag is created.

### `onError` (required)
Called in case of an error with the script.
Called in case of an error with the script. Will receive the generated error event as an argument.

### `onLoad` (required)
Called when the requested script is fully loaded.
Expand Down Expand Up @@ -52,8 +52,8 @@ handleScriptCreate() {
this.setState({ scriptLoaded: false })
}

handleScriptError() {
this.setState({ scriptError: true })
handleScriptError(errorEvent) {
this.setState({ scriptError: true, scriptErrorEvent: errorEvent })
}

handleScriptLoad() {
Expand Down
9 changes: 5 additions & 4 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export default class Script extends React.Component {
}

if (this.constructor.erroredScripts[url]) {
onError();
const errorEvent = this.constructor.erroredScripts[url];
onError(errorEvent);
return;
}

Expand Down Expand Up @@ -111,10 +112,10 @@ export default class Script extends React.Component {
});
};

script.onerror = () => {
this.constructor.erroredScripts[url] = true;
script.onerror = (errorEvent) => {
this.constructor.erroredScripts[url] = errorEvent;
callObserverFuncAndRemoveObserver((observer) => {
observer.onError();
observer.onError(errorEvent);
return true;
});
};
Expand Down
5 changes: 4 additions & 1 deletion src/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ test('componentDidMount should not run onLoad callback if script not yet loaded'
});

test('componentDidMount should run onError callback if script has errored', () => {
wrapper.instance().constructor.erroredScripts[props.url] = true;
const errorEvent = { type: 'error' };
wrapper.instance().constructor.erroredScripts[props.url] = errorEvent;
wrapper.instance().componentDidMount();
expect(props.onError.mock.calls.length).toBe(1);
expect(props.onError.mock.calls[0].length).toBe(1);
expect(props.onError.mock.calls[0][0]).toBe(errorEvent);
});

test('componentDidMount should not run onError callback if script has not errored', () => {
Expand Down