-
Notifications
You must be signed in to change notification settings - Fork 530
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
fix(createInstantSearchManager): drop outdated response #1765
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid having several files, maybe something you could do is emitting a different event depending of the query present in the state parameter?