Add a couple tests to react-instantsearch-core

This commit is contained in:
Gordon
2018-10-16 21:41:34 -05:00
parent 39aa9a3257
commit e23d84ead0
3 changed files with 80 additions and 1 deletions
@@ -0,0 +1,79 @@
import * as React from 'react';
import {
createInstantSearch,
createIndex,
createConnector
} from 'react-instantsearch-core';
() => {
const InstantSearch = createInstantSearch(() => {}, {Root: 'div', props: {className: `widget`}});
<InstantSearch>
<div></div>
</InstantSearch>;
};
() => {
const Index = createIndex({Root: 'div', props: {className: `widget`}});
<Index>
<div></div>
</Index>;
};
// https://community.algolia.com/react-instantsearch/guide/Custom_connectors.html
() => {
const CoolWidget = createConnector({
displayName: 'CoolWidget',
getProvidedProps(props: any, searchState: any) {
// Since the `queryAndPage` searchState entry isn't necessarily defined, we need
// to default its value.
const [query, page] = searchState.queryAndPage || ['', 0];
// Connect the underlying component to the `queryAndPage` searchState entry.
return {
query,
page,
};
},
refine(props: any, searchState: any, newQuery: any, newPage: any) {
// When the underlying component calls its `refine` prop, update the searchState
// with the new query and page.
return {
// `searchState` represents the search state of *all* widgets. We need to extend it
// instead of replacing it, otherwise other widgets will lose their
// respective state.
...searchState,
queryAndPage: [newQuery, newPage],
};
},
})(props =>
<div>
The query is {props.query}, the page is {props.page}.
{/*
Clicking on this button will update the searchState to:
{
...otherSearchState,
query: 'algolia',
page: 20,
}
*/}
<button onClick={() => props.refine('algolia', 20)} />
{/*
Clicking on this button will update the searchState to:
{
...otherSearchState,
query: 'instantsearch',
page: 15,
}
*/}
<button onClick={() => props.refine('instantsearch', 15)} />
</div>
);
<CoolWidget>
<div></div>
</CoolWidget>;
};
+1 -1
View File
@@ -18,6 +18,6 @@
"files": [
"index.d.ts",
"types.d.ts",
"react-instantsearch-core-tests.ts"
"react-instantsearch-core-tests.tsx"
]
}