Skip to content

Commit

Permalink
refactor(error): use object vs list of arguments (algolia/algoliasear…
Browse files Browse the repository at this point in the history
  • Loading branch information
samouss authored and Haroenv committed Nov 18, 2019
1 parent f5e93fb commit 88d43d8
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 17 deletions.
12 changes: 8 additions & 4 deletions packages/algoliasearch-helper/src/algoliasearch.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1267,9 +1267,11 @@ AlgoliaSearchHelper.prototype._search = function() {
this.client.search(queries)
.then(this._dispatchAlgoliaResponse.bind(this, states, queryId))
.catch(this._dispatchAlgoliaError.bind(this, queryId));
} catch (err) {
} catch (error) {
// If we reach this part, we're in an internal error state
this.emit('error', err);
this.emit('error', {
error: error
});
}
};

Expand Down Expand Up @@ -1314,7 +1316,7 @@ AlgoliaSearchHelper.prototype._dispatchAlgoliaResponse = function(states, queryI
});
};

AlgoliaSearchHelper.prototype._dispatchAlgoliaError = function(queryId, err) {
AlgoliaSearchHelper.prototype._dispatchAlgoliaError = function(queryId, error) {
if (queryId < this._lastQueryIdReceived) {
// Outdated answer
return;
Expand All @@ -1323,7 +1325,9 @@ AlgoliaSearchHelper.prototype._dispatchAlgoliaError = function(queryId, err) {
this._currentNbQueries -= queryId - this._lastQueryIdReceived;
this._lastQueryIdReceived = queryId;

this.emit('error', err);
this.emit('error', {
error: error
});

if (this._currentNbQueries === 0) this.emit('searchQueueEmpty');
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ test('[INT][FILTERS] Using distinct should let me retrieve all facet without dis
facets: ['colors']
});

helper.on('error', function(err) {
done.fail(err);
helper.on('error', function(event) {
done.fail(event.error);
});

helper.on('result', function(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ test('[INT][FILTERS] Should retrieve different values for multi facetted records

var calls = 0;

helper.on('error', function(err) {
done.fail(err);
helper.on('error', function(event) {
done.fail(event.error);
});

helper.on('result', function(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ test('[INT][NUMERICS][RAW-API]Test numeric operations on the helper and their re

var calls = 0;

helper.on('error', function(err) {
done.fail(err);
helper.on('error', function(event) {
done.fail(event.error);
});

helper.on('result', function(event) {
Expand Down Expand Up @@ -106,8 +106,8 @@ test('[INT][NUMERICS][MANAGED-API]Test numeric operations on the helper and thei

var calls = 0;

helper.on('error', function(err) {
done.fail(err);
helper.on('error', function(event) {
done.fail(event.error);
});

helper.on('result', function(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ test(
var state0 = helper.state;

var calls = 1;
helper.on('error', function(err) {
done.fail(err);
helper.on('error', function(event) {
done.fail(event.error);
});

helper.on('result', function(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ test('[INT][TAGS]Test tags operations on the helper and their results on the alg

var calls = 0;

helper.on('error', function(err) {
done.fail(err);
helper.on('error', function(event) {
done.fail(event.error);
});

helper.on('result', function(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function makeFakeClient() {
};
}

function runAllMicroTasks() {
return new Promise(setImmediate);
}

test('Change events should be emitted with reset page to true on implicit reset methods', function() {
var changed = jest.fn();
var fakeClient = makeFakeClient();
Expand Down Expand Up @@ -321,11 +325,61 @@ test('result event should be emitted once the request is complete', function() {

helper.search();

return Promise.resolve().then(function() {
return runAllMicroTasks().then(function() {
expect(resulted).toHaveBeenCalledTimes(1);
expect(resulted).toHaveBeenLastCalledWith({
results: expect.any(algoliaSearchHelper.SearchResults),
state: helper.state
});
});
});

test('error event should be emitted once the request is complete with errors', function() {
var errored = jest.fn();
var fakeClient = makeFakeClient();
var helper = algoliaSearchHelper(fakeClient, 'Index', {
disjunctiveFacets: ['city'],
facets: ['tower']
});

fakeClient.search.mockImplementationOnce(function() {
return Promise.reject(new Error('Abort'));
});

helper.on('error', errored);

expect(errored).toHaveBeenCalledTimes(0);

helper.search();

return runAllMicroTasks().then(function() {
expect(errored).toHaveBeenCalledTimes(1);
expect(errored).toHaveBeenLastCalledWith({
error: expect.any(Error)
});
});
});

test('error event should be emitted if an error happens at request time', function() {
var errored = jest.fn();
var fakeClient = makeFakeClient();
var helper = algoliaSearchHelper(fakeClient, 'Index', {
disjunctiveFacets: ['city'],
facets: ['tower']
});

fakeClient.search.mockImplementationOnce(function() {
throw new Error('Unexpected error');
});

helper.on('error', errored);

expect(errored).toHaveBeenCalledTimes(0);

helper.search();

expect(errored).toHaveBeenCalledTimes(1);
expect(errored).toHaveBeenLastCalledWith({
error: expect.any(Error)
});
});

0 comments on commit 88d43d8

Please sign in to comment.