-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(createInstantSearchManager): drop outdated response (#1765)
The helper is implemented in a way that it will drop outdated requests. This change makes react-instantsearch use the event model of the helper that let it implement this mechanism that ensures reliability in the algolia answers. It also results in some fewer instanciation of objects for callbacks and the base search parameters.
- Loading branch information
Showing
4 changed files
with
323 additions
and
236 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
packages/react-instantsearch/src/core/createInstantSearchManager.error.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* eslint-env jest, jasmine */ | ||
/* eslint-disable no-console */ | ||
|
||
import createInstantSearchManager from './createInstantSearchManager'; | ||
|
||
import algoliaClient from 'algoliasearch'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
jest.mock('algoliasearch-helper/src/algoliasearch.helper.js', () => { | ||
let count = 0; | ||
console.log('setup'); | ||
const Helper = require.requireActual('algoliasearch-helper/src/algoliasearch.helper.js'); | ||
Helper.prototype._handleResponse = function(state) { | ||
this.emit('error', {count: count++}, state); | ||
}; | ||
return Helper; | ||
}); | ||
|
||
const client = algoliaClient('latency', '249078a3d4337a8231f1665ec5a44966'); | ||
client.search = jest.fn((queries, cb) => { | ||
if (cb) { | ||
setImmediate(() => { | ||
// We do not care about the returned values because we also control how | ||
// it will handle in the helper | ||
cb(null, null); | ||
}); | ||
return undefined; | ||
} | ||
|
||
return new Promise(resolve => { | ||
// cf comment above | ||
resolve(null); | ||
}); | ||
}); | ||
|
||
describe('createInstantSearchManager', () => { | ||
describe('with error from algolia', () => { | ||
describe('on widget lifecycle', () => { | ||
it('updates the store and searches', () => { | ||
const ism = createInstantSearchManager({ | ||
indexName: 'index', | ||
initialState: {}, | ||
searchParameters: {}, | ||
algoliaClient: client, | ||
}); | ||
|
||
ism.widgetsManager.registerWidget({ | ||
getSearchParameters: params => params.setQuery('search'), | ||
}); | ||
|
||
expect(ism.store.getState().error).toBe(null); | ||
|
||
jest.runAllTimers(); | ||
|
||
const store = ism.store.getState(); | ||
expect(store.error).toEqual({count: 0}); | ||
expect(store.results).toBe(null); | ||
|
||
ism.widgetsManager.update(); | ||
|
||
jest.runAllTimers(); | ||
|
||
const store1 = ism.store.getState(); | ||
expect(store1.error).toEqual({count: 1}); | ||
expect(store1.results).toBe(null); | ||
}); | ||
}); | ||
describe('on external updates', () => { | ||
it('updates the store and searches', () => { | ||
const ism = createInstantSearchManager({ | ||
indexName: 'index', | ||
initialState: {}, | ||
searchParameters: {}, | ||
algoliaClient: client, | ||
}); | ||
|
||
ism.onExternalStateUpdate({}); | ||
|
||
expect(ism.store.getState().error).toBe(null); | ||
|
||
jest.runAllTimers(); | ||
|
||
const store = ism.store.getState(); | ||
expect(store.error).toEqual({count: 2}); | ||
expect(store.results).toBe(null); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
packages/react-instantsearch/src/core/createInstantSearchManager.result.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* eslint-env jest, jasmine */ | ||
/* eslint-disable no-console */ | ||
|
||
import createInstantSearchManager from './createInstantSearchManager'; | ||
|
||
import algoliaClient from 'algoliasearch'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
jest.mock('algoliasearch-helper/src/algoliasearch.helper.js', () => { | ||
let count = 0; | ||
const Helper = require.requireActual('algoliasearch-helper/src/algoliasearch.helper.js'); | ||
Helper.prototype._handleResponse = function(state) { | ||
this.emit('result', {count: count++}, state); | ||
}; | ||
return Helper; | ||
}); | ||
|
||
const client = algoliaClient('latency', '249078a3d4337a8231f1665ec5a44966'); | ||
client.search = jest.fn((queries, cb) => { | ||
if (cb) { | ||
setImmediate(() => { | ||
// We do not care about the returned values because we also control how | ||
// it will handle in the helper | ||
cb(null, null); | ||
}); | ||
return undefined; | ||
} | ||
|
||
return new Promise(resolve => { | ||
// cf comment above | ||
resolve(null); | ||
}); | ||
}); | ||
|
||
describe('createInstantSearchManager', () => { | ||
describe('with correct result from algolia', () => { | ||
describe('on widget lifecycle', () => { | ||
it('updates the store and searches', () => { | ||
const ism = createInstantSearchManager({ | ||
indexName: 'index', | ||
initialState: {}, | ||
searchParameters: {}, | ||
algoliaClient: client, | ||
}); | ||
|
||
ism.widgetsManager.registerWidget({ | ||
getSearchParameters: params => params.setQuery('search'), | ||
}); | ||
|
||
expect(ism.store.getState().results).toBe(null); | ||
|
||
jest.runAllTimers(); | ||
|
||
const store = ism.store.getState(); | ||
expect(store.results).toEqual({count: 0}); | ||
expect(store.error).toBe(null); | ||
|
||
ism.widgetsManager.update(); | ||
|
||
jest.runAllTimers(); | ||
|
||
const store1 = ism.store.getState(); | ||
expect(store1.results).toEqual({count: 1}); | ||
expect(store1.error).toBe(null); | ||
}); | ||
}); | ||
describe('on external updates', () => { | ||
it('updates the store and searches', () => { | ||
const ism = createInstantSearchManager({ | ||
indexName: 'index', | ||
initialState: {}, | ||
searchParameters: {}, | ||
algoliaClient: client, | ||
}); | ||
|
||
ism.onExternalStateUpdate({}); | ||
|
||
expect(ism.store.getState().results).toBe(null); | ||
|
||
jest.runAllTimers(); | ||
|
||
const store = ism.store.getState(); | ||
expect(store.results).toEqual({count: 2}); | ||
expect(store.error).toBe(null); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.