Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
refactor(searchOnce): use object vs list of arguments (#681)
Browse files Browse the repository at this point in the history
  • Loading branch information
samouss authored and Haroenv committed May 7, 2019
1 parent fa57274 commit 36066f3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/algoliasearch.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ AlgoliaSearchHelper.prototype.searchOnce = function(options, cb) {

this._currentNbQueries++;

this.emit('searchOnce', tempState);
this.emit('searchOnce', {
state: tempState
});

if (cb) {
this.client
Expand Down
16 changes: 9 additions & 7 deletions test/spec/algoliasearch.helper/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,23 +254,25 @@ test('search event should be emitted once when the search is triggered and befor
});

test('searchOnce event should be emitted once when the search is triggered using searchOnce and before the request is sent', function() {
var searchedOnce = jest.fn();
var fakeClient = makeFakeClient();
var helper = algoliaSearchHelper(fakeClient, 'Index', {
disjunctiveFacets: ['city'],
facets: ['tower']
});

var count = 0;
helper.on('searchOnce', searchedOnce);

helper.on('searchOnce', function() {
count++;
});

expect(count).toBe(0);
expect(searchedOnce).toHaveBeenCalledTimes(0);
expect(fakeClient.search).toHaveBeenCalledTimes(0);

helper.searchOnce({}, function() {});
expect(count).toBe(1);

expect(searchedOnce).toHaveBeenCalledTimes(1);
expect(searchedOnce).toHaveBeenLastCalledWith({
state: helper.state
});

expect(fakeClient.search).toHaveBeenCalledTimes(1);
});

Expand Down

0 comments on commit 36066f3

Please sign in to comment.