-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Instantsearch): Update all props on InstantSearch (#1828)
* fix(createInstantSearch): new credentials creates a new client. Previously was creating a new client at each render. * fix(isManager): implement update of client * feat(SearchParameters): Move InstantSearch config to <Configure/> Also: - fix the indexName property to be able to update it. - small refactoring to explicitely define the props of createInstantSearch - update the doc accordingly
- Loading branch information
Showing
16 changed files
with
619 additions
and
448 deletions.
There are no files selected for viewing
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
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
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
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
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
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 @@ | ||
export default () => null; |
11 changes: 11 additions & 0 deletions
11
packages/react-instantsearch/src/connectors/connectConfigure.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,11 @@ | ||
import createConnector from '../core/createConnector.js'; | ||
import omit from 'lodash/omit'; | ||
|
||
export default createConnector({ | ||
displayName: 'AlgoliaConfigure', | ||
getProvidedProps() { return {}; }, | ||
getSearchParameters(searchParameters, props) { | ||
const configuration = omit(props, 'children'); | ||
return searchParameters.setQueryParameters(configuration); | ||
}, | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/react-instantsearch/src/connectors/connectConfigure.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,19 @@ | ||
/* eslint-env jest, jasmine */ | ||
import {SearchParameters} from 'algoliasearch-helper'; | ||
|
||
import connect from './connectConfigure.js'; | ||
jest.mock('../core/createConnector'); | ||
|
||
const {getSearchParameters} = connect; | ||
|
||
describe('connectConfigure', () => { | ||
it('it propagates the props to the SearchParameters without children', () => { | ||
const searchParameters = getSearchParameters( | ||
new SearchParameters(), | ||
{distinct: 1, whatever: 'please', children: 'whatever'} | ||
); | ||
expect(searchParameters.getQueryParameter('distinct')).toEqual(1); | ||
expect(searchParameters.getQueryParameter('whatever')).toEqual('please'); | ||
expect(searchParameters.getQueryParameter.bind(searchParameters, 'children')).toThrow(); | ||
}); | ||
}); |
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
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
100 changes: 100 additions & 0 deletions
100
packages/react-instantsearch/src/core/createInstantSearch.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,100 @@ | ||
/* eslint-env jest, jasmine */ | ||
/* eslint-disable no-console */ | ||
import React from 'react'; | ||
import {mount} from 'enzyme'; | ||
|
||
import algoliaClient from 'algoliasearch'; | ||
|
||
import createInstantSearch from './createInstantSearch'; | ||
|
||
describe('createInstantSearch', () => { | ||
it('Instanciate the client when no algoliaClient provided', () => { | ||
const apiKey = '332335235235325'; | ||
const appId = 'myApp'; | ||
|
||
const algoliaClientFactory = jest.fn(algoliaClient); | ||
const InstantSearch = createInstantSearch( | ||
algoliaClientFactory, { | ||
Root: 'div', | ||
props: {className: 'ais-InstantSearch__root'}, | ||
}); | ||
|
||
const wrapper = mount(<InstantSearch appId={apiKey} apiKey={appId} indexName=""/>); | ||
|
||
const otherApiKey = '50943204984230498'; | ||
const otherAppId = 'otherApp'; | ||
wrapper.setProps({ | ||
apiKey: otherApiKey, | ||
appId: otherAppId, | ||
}); | ||
|
||
wrapper.setProps({ | ||
apiKey: otherApiKey, | ||
appId: otherAppId, | ||
}); | ||
|
||
expect(algoliaClientFactory).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('Never call the client factory when algoliaClient is provided', () => { | ||
const algoliaClientFactory = jest.fn(algoliaClient); | ||
const InstantSearch = createInstantSearch( | ||
algoliaClientFactory, { | ||
Root: 'div', | ||
props: {className: 'ais-InstantSearch__root'}, | ||
}); | ||
|
||
const fakeClient = { | ||
addAlgoliaAgent: () => {}, | ||
search: () => {}, | ||
}; | ||
|
||
const wrapper = mount(<InstantSearch algoliaClient={fakeClient} indexName=""/>); | ||
|
||
wrapper.setProps({ | ||
algoliaClient: { | ||
search: () => {}, | ||
}, | ||
}); | ||
|
||
expect(algoliaClientFactory).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('Can switch from provided to non-provided algoliaClient, and instanciate accordingly', () => { | ||
const apiKey = '332335235235325'; | ||
const appId = 'myApp'; | ||
|
||
const algoliaClientFactory = jest.fn(algoliaClient); | ||
const InstantSearch = createInstantSearch( | ||
algoliaClientFactory, { | ||
Root: 'div', | ||
props: {className: 'ais-InstantSearch__root'}, | ||
}); | ||
|
||
const wrapper = mount(<InstantSearch appId={apiKey} apiKey={appId} indexName=""/>); | ||
|
||
const otherApiKey = '50943204984230498'; | ||
const otherAppId = 'otherApp'; | ||
wrapper.setProps({ | ||
apiKey: otherApiKey, | ||
appId: otherAppId, | ||
}); | ||
|
||
wrapper.setProps({ | ||
algoliaClient: { | ||
search: () => {}, | ||
addAlgoliaAgent: () => {}, | ||
}, | ||
apiKey: undefined, | ||
appId: undefined, | ||
}); | ||
|
||
wrapper.setProps({ | ||
algoliaClient: undefined, | ||
apiKey: otherApiKey, | ||
appId: otherAppId, | ||
}); | ||
|
||
expect(algoliaClientFactory).toHaveBeenCalledTimes(3); | ||
}); | ||
}); |
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
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
Oops, something went wrong.
Aha, is this the problem with #1849 ? @vvo