Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into feat/useSyncExternalStore
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour authored Jun 1, 2022
2 parents 381be56 + 2cf0ebe commit 6e755ce
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { act, render, waitFor } from '@testing-library/react';
import React, { version as ReactVersion } from 'react';
import React, { Suspense, version as ReactVersion } from 'react';

import { createSearchClient } from '../../../../../test/mock';
import { wait } from '../../../../../test/utils';
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('InstantSearch', () => {
expect(searchClient.search).toHaveBeenCalledTimes(1);
});

test('renders components and their lifecycles in StrictMode', async () => {
test('renders components in Strict Mode', async () => {
const searchClient = createSearchClient({});

act(() => {
Expand All @@ -180,6 +180,95 @@ describe('InstantSearch', () => {

await waitFor(() => {
expect(searchClient.search).toHaveBeenCalledTimes(1);
expect(searchClient.search).toHaveBeenCalledWith([
{
indexName: 'indexName',
params: { facets: [], query: '', tagFilters: '' },
},
{
indexName: 'subIndexName',
params: {
facets: ['brand'],
maxValuesPerFacet: 10,
query: '',
tagFilters: '',
},
},
]);
});
});

test('renders components in Strict Mode with a Suspense boundary', async () => {
const searchClient = createSearchClient({});

act(() => {
render(
<React.StrictMode>
<InstantSearch indexName="indexName" searchClient={searchClient}>
<SearchBox />
<Suspense fallback={null}>
<Index indexName="subIndexName">
<RefinementList attribute="brand" />
</Index>
</Suspense>
</InstantSearch>
</React.StrictMode>
);
});

await waitFor(() => {
expect(searchClient.search).toHaveBeenCalledTimes(1);
expect(searchClient.search).toHaveBeenCalledWith([
{
indexName: 'indexName',
params: { facets: [], query: '', tagFilters: '' },
},
{
indexName: 'subIndexName',
params: {
facets: ['brand'],
maxValuesPerFacet: 10,
query: '',
tagFilters: '',
},
},
]);
});
});

test('renders components with a Suspense boundary', async () => {
const searchClient = createSearchClient({});

act(() => {
render(
<InstantSearch indexName="indexName" searchClient={searchClient}>
<SearchBox />
<Suspense fallback={null}>
<Index indexName="subIndexName">
<RefinementList attribute="brand" />
</Index>
</Suspense>
</InstantSearch>
);
});

await waitFor(() => {
expect(searchClient.search).toHaveBeenCalledTimes(1);
expect(searchClient.search).toHaveBeenCalledWith([
{
indexName: 'indexName',
params: { facets: [], query: '', tagFilters: '' },
},
{
indexName: 'subIndexName',
params: {
facets: ['brand'],
maxValuesPerFacet: 10,
query: '',
tagFilters: '',
},
},
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render, screen, waitFor } from '@testing-library/react';
import { history } from 'instantsearch.js/es/lib/routers';
import { simple } from 'instantsearch.js/es/lib/stateMappings';
import React from 'react';
import { Hits, SearchBox } from 'react-instantsearch-hooks-web';
import { Hits, RefinementList, SearchBox } from 'react-instantsearch-hooks-web';

import { createSearchClient } from '../../../../../test/mock';
import { wait } from '../../../../../test/utils';
Expand Down Expand Up @@ -178,6 +178,96 @@ describe('InstantSearchSSRProvider', () => {
});
});

test('renders refinements from initial results state', async () => {
const searchClient = createSearchClient({});
const initialResults = {
indexName: {
state: {
facets: [],
disjunctiveFacets: ['brand'],
hierarchicalFacets: [],
facetsRefinements: {},
facetsExcludes: {},
disjunctiveFacetsRefinements: { brand: ['Apple'] },
numericRefinements: {},
tagRefinements: [],
hierarchicalFacetsRefinements: {},
index: 'indexName',
query: '',
},
results: [
{
hits: [
{
name: 'Apple - MacBook Air® (Latest Model) - 13.3" Display - Intel Core i5 - 8GB Memory - 128GB Flash Storage - Silver',
objectID: '6443034',
},
{
name: 'Apple - EarPods™ with Remote and Mic - White',
objectID: '6848136',
},
],
nbHits: 442,
page: 0,
nbPages: 23,
hitsPerPage: 2,
facets: { brand: { Apple: 442, Samsung: 633 } },
exhaustiveFacetsCount: true,
exhaustiveNbHits: true,
exhaustiveTypo: true,
query: '',
queryAfterRemoval: '',
params: '',
index: 'indexName',
processingTimeMS: 1,
},
{
hits: [
{
name: 'Amazon - Fire TV Stick with Alexa Voice Remote - Black',
objectID: '5477500',
},
],
nbHits: 21469,
page: 0,
nbPages: 1000,
hitsPerPage: 1,
facets: {
brand: { Samsung: 633, Apple: 442 },
},
exhaustiveFacetsCount: true,
exhaustiveNbHits: true,
exhaustiveTypo: true,
query: '',
queryAfterRemoval: '',
params: '',
index: 'indexName',
processingTimeMS: 1,
},
],
},
};

function App() {
return (
<InstantSearchSSRProvider initialResults={initialResults}>
<InstantSearch searchClient={searchClient} indexName="indexName">
<RefinementList attribute="brand" />
</InstantSearch>
</InstantSearchSSRProvider>
);
}

render(<App />);

await waitFor(() => {
expect(screen.getByRole('checkbox', { name: 'Apple 442' })).toBeChecked();
expect(
screen.getByRole('checkbox', { name: 'Samsung 633' })
).not.toBeChecked();
});
});

test('without server state renders children', async () => {
const searchClient = createSearchClient({});

Expand Down

0 comments on commit 6e755ce

Please sign in to comment.