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

Handle async rejection #149

Merged
merged 1 commit into from
Dec 19, 2019
Merged
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
50 changes: 50 additions & 0 deletions src/__tests__/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,4 +816,54 @@ describe('e2e', () => {
...message,
});
});

it('handles rejected async function', async () => {
const message = { value: 'error' };

@track()
class Page extends React.Component {
constructor() {
super();
this.message = message;
this.state = {};
}

async executeAction() {
try {
const data = await this.handleAsyncAction();
this.setState({ data });
} catch (error) {
this.setState({ data: error });
}
}

// eslint-disable-next-line no-unused-vars
@track((props, state, methodArgs, [{ value }, err]) => {
return (
err && {
label: 'async action',
status: 'failed',
}
);
})
handleAsyncAction() {
return Promise.reject(this.message);
}

render() {
return <div>{this.state.data && this.state.data.value}</div>;
}
}

// Get the first child since the page is wrapped with the WithTracking component.
const page = await mount(<Page />).childAt(0);
await page.instance().executeAction();

expect(page.state().data).toEqual(message);
expect(dispatchTrackingEvent).toHaveBeenCalledTimes(1);
expect(dispatchTrackingEvent).toHaveBeenCalledWith({
label: 'async action',
status: 'failed',
});
});
});
2 changes: 1 addition & 1 deletion src/__tests__/trackEventMethodDecorator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describe('trackEventMethodDecorator', () => {
expect(trackEvent).toHaveBeenCalledWith(dummyData);
const trackingDataArguments = trackingData.mock.calls[0];
// the resulting error should be passed to the tracking data
expect(trackingDataArguments[3]).toEqual([null, error]);
expect(trackingDataArguments[3]).toEqual([{}, error]);
expect(error).toBeInstanceOf(Error);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/trackEventMethodDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function trackEventMethodDecorator(trackingData = {}) {
.then(trackEvent.bind(this))
.then(() => fn)
.catch(error => {
trackEvent(null, error);
trackEvent({}, error);
throw error;
});
}
Expand Down