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

feat(core): add an error event to monitor error from Algolia #2642

Merged
merged 1 commit into from
Dec 29, 2017
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
5 changes: 4 additions & 1 deletion docgen/layouts/instantsearch.pug
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ block content
a.anchor(href=`${navPath}#events`)
p InstantSearch is an EventEmitter and as such it emits events on specific parts of the lifecycle.
h3 render
p
p
| The `render` event is triggered when the rendering of all the widgets is done. This
| happens after a search result comes back from Algolia - which means that it is
| triggered for the first once everything after all the widgets went through all
| there lifecycle steps once (getConfiguration, init, render).
h3 error
p
| The `error` event is triggered when an error is reported when calling the API.
3 changes: 3 additions & 0 deletions src/lib/InstantSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ Usage: instantsearch({
this.helper = helper;
this._init(helper.state, this.helper);
this.helper.on('result', this._render.bind(this, this.helper));
this.helper.on('error', e => {
this.emit('error', e);
});

this._searchStalledTimer = null;
this._isSearchStalled = true;
Expand Down
38 changes: 38 additions & 0 deletions src/lib/__tests__/InstantSearch-test-integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// import algoliaSearchHelper from 'algoliasearch-helper';
import InstantSearch from '../InstantSearch';

describe('InstantSearch lifecycle', () => {
it('emits an error if the API returns an error', () => {
const search = new InstantSearch({
// correct credentials so that the client does not retry
appId: 'latency',
apiKey: '6be0576ff61c053d5f9a3225e2a90f76',
// the index name does not exist so that we get an error
indexName: 'DOESNOTEXIST',
});

let sendError;
let reject;
const waitForError = new Promise((resolve, r) => {
sendError = resolve;
reject = r;
});

search.on('error', e => {
try {
expect(e).toEqual(expect.any(Error));
} catch (err) {
reject(err);
}
sendError();
});

search.addWidget({
render: () => {},
});

search.start();

return waitForError;
});
});
6 changes: 4 additions & 2 deletions src/lib/__tests__/__snapshots__/InstantSearch-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ Array [
"helper": AlgoliaSearchHelper {
"_currentNbQueries": 0,
"_events": Object {
"error": [Function],
"result": [Function],
"search": [Function],
},
"_eventsCount": 2,
"_eventsCount": 3,
"_lastQueryIdReceived": -1,
"_queryId": 0,
"client": Object {
Expand Down Expand Up @@ -94,10 +95,11 @@ Array [
"helper": AlgoliaSearchHelper {
"_currentNbQueries": 0,
"_events": Object {
"error": [Function],
"result": [Function],
"search": [Function],
},
"_eventsCount": 2,
"_eventsCount": 3,
"_lastQueryIdReceived": -1,
"_queryId": 0,
"client": Object {
Expand Down