Skip to content

Commit

Permalink
feat(core): add an error event to monitor error from Algolia (#2642)
Browse files Browse the repository at this point in the history
Fix #1585
  • Loading branch information
bobylito committed Dec 29, 2017
1 parent 08a8747 commit 71c2d68
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
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

0 comments on commit 71c2d68

Please sign in to comment.