-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(next-app-router): prevent client-side search when rerendering (#6452
) * fix(next-app-router): prevent client-side search when rerendering * ensure search is performed when remounted on client-side route change
- Loading branch information
Showing
2 changed files
with
93 additions
and
2 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
73 changes: 73 additions & 0 deletions
73
packages/react-instantsearch-nextjs/src/__tests__/InstantSearchNext.test.tsx
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,73 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { createSearchClient } from '@instantsearch/mocks'; | ||
import { wait } from '@instantsearch/testutils'; | ||
import { act, render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { SearchBox } from 'react-instantsearch'; | ||
|
||
import { InstantSearchNext } from '../InstantSearchNext'; | ||
|
||
const mockPathname = jest.fn(); | ||
jest.mock('next/navigation', () => ({ | ||
...jest.requireActual('next/navigation'), | ||
usePathname() { | ||
return mockPathname(); | ||
}, | ||
})); | ||
|
||
describe('rerendering', () => { | ||
const client = createSearchClient(); | ||
|
||
function Component() { | ||
return ( | ||
<InstantSearchNext searchClient={client} indexName="indexName"> | ||
<SearchBox /> | ||
</InstantSearchNext> | ||
); | ||
} | ||
|
||
beforeEach(() => { | ||
(client.search as jest.Mock).mockClear(); | ||
}); | ||
|
||
it('does not trigger a client-side search by default', async () => { | ||
const { rerender } = render(<Component />); | ||
|
||
await act(async () => { | ||
await wait(0); | ||
}); | ||
|
||
rerender(<Component />); | ||
|
||
await act(async () => { | ||
await wait(0); | ||
}); | ||
|
||
expect(client.search).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('triggers a client-side search on route change', async () => { | ||
mockPathname.mockImplementation(() => '/a'); | ||
const { rerender } = render(<Component />); | ||
|
||
await act(async () => { | ||
await wait(0); | ||
}); | ||
|
||
mockPathname.mockImplementation(() => '/b'); | ||
rerender(<Component />); | ||
|
||
await act(async () => { | ||
await wait(0); | ||
}); | ||
|
||
expect(client.search).not.toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.resetAllMocks(); | ||
}); |