This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(indexId): avoid to rely on the results.index [PART-1] (#1833)
* feat(indexId): rely on indexId rather than indexName [PART-2] (#1835) * feat(indexId): refactor createInstantSearchManager [PART-3] (#1840) * feat(indexId): rely on indexId rather than indexName in SSR [PART-4] (#1842) * feat(indexId): refactor createInstantSearchServer [PART-5] (#1843) * feat(indexId): rename getIndex -> getIndexId [PART-6] (#1851)
- Loading branch information
Showing
25 changed files
with
1,285 additions
and
461 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
184 changes: 122 additions & 62 deletions
184
packages/react-instantsearch-core/src/components/__tests__/Index.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 |
---|---|---|
@@ -1,100 +1,160 @@ | ||
import React from 'react'; | ||
import Enzyme, { mount } from 'enzyme'; | ||
import Enzyme, { shallow } from 'enzyme'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
import { SearchParameters } from 'algoliasearch-helper'; | ||
import Index from '../Index'; | ||
|
||
Enzyme.configure({ adapter: new Adapter() }); | ||
|
||
describe('Index', () => { | ||
const DEFAULT_PROPS = { | ||
indexName: 'foobar', | ||
const createContext = () => ({ | ||
ais: { | ||
onSearchParameters: jest.fn(), | ||
widgetsManager: { | ||
registerWidget: jest.fn(), | ||
update: jest.fn(), | ||
}, | ||
}, | ||
}); | ||
|
||
const requiredProps = { | ||
indexName: 'indexName', | ||
indexId: 'indexId', | ||
root: { | ||
Root: 'div', | ||
}, | ||
}; | ||
|
||
const registerWidget = jest.fn(); | ||
const update = jest.fn(); | ||
const widgetsManager = { registerWidget, update }; | ||
it('registers itself on mount', () => { | ||
const context = createContext(); | ||
|
||
let context = { | ||
ais: { | ||
widgetsManager, | ||
onSearchParameters: () => {}, | ||
update, | ||
}, | ||
}; | ||
const wrapper = shallow( | ||
<Index {...requiredProps}> | ||
<div /> | ||
</Index>, | ||
{ | ||
context, | ||
} | ||
); | ||
|
||
expect(context.ais.widgetsManager.registerWidget).toHaveBeenCalledTimes(1); | ||
expect(context.ais.widgetsManager.registerWidget).toHaveBeenCalledWith( | ||
wrapper.instance() | ||
); | ||
}); | ||
|
||
it('calls onSearchParameters on mount', () => { | ||
const context = createContext(); | ||
|
||
shallow( | ||
<Index {...requiredProps}> | ||
<div /> | ||
</Index>, | ||
{ | ||
context, | ||
} | ||
); | ||
|
||
it('validates its props', () => { | ||
expect(() => { | ||
mount( | ||
<Index {...DEFAULT_PROPS}> | ||
<div /> | ||
</Index>, | ||
{ context } | ||
); | ||
}).not.toThrow(); | ||
|
||
expect(() => { | ||
mount(<Index {...DEFAULT_PROPS} />, { context }); | ||
}).not.toThrow(); | ||
|
||
expect(() => { | ||
mount( | ||
<Index {...DEFAULT_PROPS}> | ||
<div /> | ||
<div /> | ||
</Index>, | ||
{ context } | ||
); | ||
}).not.toThrow(); | ||
|
||
expect(registerWidget.mock.calls).toHaveLength(3); | ||
expect(context.ais.onSearchParameters).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('calls update if indexName prop changes', () => { | ||
const context = createContext(); | ||
|
||
const wrapper = shallow( | ||
<Index {...requiredProps}> | ||
<div /> | ||
</Index>, | ||
{ | ||
context, | ||
} | ||
); | ||
|
||
expect(context.ais.widgetsManager.update).toHaveBeenCalledTimes(0); | ||
|
||
wrapper.setProps({ indexName: 'newIndexName' }); | ||
|
||
expect(context.ais.widgetsManager.update).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('unregisters itself on unmount', () => { | ||
const unregister = jest.fn(); | ||
const context = createContext(); | ||
|
||
context.ais.widgetsManager.registerWidget.mockImplementation( | ||
() => unregister | ||
); | ||
|
||
const wrapper = shallow( | ||
<Index {...requiredProps}> | ||
<div /> | ||
</Index>, | ||
{ | ||
context, | ||
} | ||
); | ||
|
||
expect(unregister).toHaveBeenCalledTimes(0); | ||
|
||
wrapper.unmount(); | ||
|
||
expect(unregister).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('exposes multi index context', () => { | ||
const wrapper = mount( | ||
<Index {...DEFAULT_PROPS}> | ||
const context = createContext(); | ||
|
||
const wrapper = shallow( | ||
<Index {...requiredProps}> | ||
<div /> | ||
</Index>, | ||
{ context } | ||
{ | ||
context, | ||
} | ||
); | ||
|
||
const childContext = wrapper.instance().getChildContext(); | ||
expect(childContext.multiIndexContext.targetedIndex).toBe( | ||
DEFAULT_PROPS.indexName | ||
); | ||
|
||
expect(childContext.multiIndexContext.targetedIndex).toBe('indexId'); | ||
}); | ||
|
||
it('update search if indexName prop change', () => { | ||
const wrapper = mount( | ||
<Index {...DEFAULT_PROPS}> | ||
it('provides search parameters from instance props', () => { | ||
const context = createContext(); | ||
|
||
const wrapper = shallow( | ||
<Index {...requiredProps}> | ||
<div /> | ||
</Index>, | ||
{ context } | ||
{ | ||
context, | ||
} | ||
); | ||
|
||
wrapper.setProps({ indexName: 'newIndexName' }); | ||
const parameters = wrapper | ||
.instance() | ||
.getSearchParameters(new SearchParameters()); | ||
|
||
expect(update.mock.calls).toHaveLength(1); | ||
expect(parameters.index).toBe('indexName'); | ||
}); | ||
|
||
it('calls onSearchParameters when mounted', () => { | ||
const onSearchParameters = jest.fn(); | ||
context = { | ||
ais: { | ||
widgetsManager, | ||
onSearchParameters, | ||
}, | ||
}; | ||
it('provides search parameters from argument props when instance props are not available', () => { | ||
const context = createContext(); | ||
|
||
mount( | ||
<Index {...DEFAULT_PROPS}> | ||
const wrapper = shallow( | ||
<Index {...requiredProps}> | ||
<div /> | ||
</Index>, | ||
{ context } | ||
{ | ||
context, | ||
} | ||
); | ||
|
||
expect(onSearchParameters.mock.calls).toHaveLength(1); | ||
const parameters = wrapper | ||
.instance() | ||
.getSearchParameters.call({}, new SearchParameters(), { | ||
indexName: 'otherIndexName', | ||
}); | ||
|
||
expect(parameters.index).toBe('otherIndexName'); | ||
}); | ||
}); |
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
Oops, something went wrong.