-
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(history): cleanUrlOnDispose also prevents write after dispose (#6305
) In real situations, it seems like #5966 forgot to account for `write` being called through `onInternalStateChange`, which is deferred and thus called after the dispose of the middleware/router. This PR solves that situation by ensuring the router never writes after dispose if this option is set to `false`. ref: #6304
- Loading branch information
Showing
2 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
packages/instantsearch.js/src/lib/routers/__tests__/history-integration.test.ts
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,140 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { createSearchClient } from '@instantsearch/mocks'; | ||
import { wait } from '@instantsearch/testutils/wait'; | ||
|
||
import { connectPagination, connectSearchBox } from '../../../connectors'; | ||
import instantsearch from '../../../index.es'; | ||
import { index } from '../../../widgets'; | ||
import historyRouter from '../history'; | ||
|
||
beforeEach(() => { | ||
window.history.pushState({}, '', '/'); | ||
}); | ||
|
||
const writeDelay = 10; | ||
const writeWait = 10 * writeDelay; | ||
|
||
test('keeps url with cleanUrlOnDispose: false', async () => { | ||
const router = historyRouter({ writeDelay, cleanUrlOnDispose: false }); | ||
|
||
const indexName = 'indexName'; | ||
const search = instantsearch({ | ||
indexName, | ||
searchClient: createSearchClient(), | ||
routing: { | ||
router, | ||
}, | ||
}); | ||
|
||
search.addWidgets([ | ||
connectPagination(() => {})({}), | ||
index({ indexName }).addWidgets([connectSearchBox(() => {})({})]), | ||
]); | ||
|
||
search.start(); | ||
|
||
expect(window.location.search).toBe(''); | ||
|
||
// on nested index | ||
search.renderState[indexName].searchBox!.refine('test'); | ||
// on main index | ||
search.renderState[indexName].pagination!.refine(39); | ||
|
||
await wait(writeWait); | ||
|
||
expect(window.location.search).toBe( | ||
`?${encodeURI('indexName[page]=40&indexName[query]=test')}` | ||
); | ||
|
||
search.dispose(); | ||
|
||
await wait(writeWait); | ||
|
||
// URL has not been cleaned | ||
expect(window.location.search).toBe( | ||
`?${encodeURI('indexName[page]=40&indexName[query]=test')}` | ||
); | ||
}); | ||
|
||
test('clears url with cleanUrlOnDispose: true', async () => { | ||
const router = historyRouter({ writeDelay, cleanUrlOnDispose: true }); | ||
|
||
const indexName = 'indexName'; | ||
const search = instantsearch({ | ||
indexName, | ||
searchClient: createSearchClient(), | ||
routing: { | ||
router, | ||
}, | ||
}); | ||
|
||
search.addWidgets([ | ||
connectPagination(() => {})({}), | ||
index({ indexName }).addWidgets([connectSearchBox(() => {})({})]), | ||
]); | ||
|
||
search.start(); | ||
|
||
expect(window.location.search).toBe(''); | ||
|
||
// on nested index | ||
search.renderState[indexName].searchBox!.refine('test'); | ||
// on main index | ||
search.renderState[indexName].pagination!.refine(39); | ||
|
||
await wait(writeWait); | ||
|
||
expect(window.location.search).toBe( | ||
`?${encodeURI('indexName[page]=40&indexName[query]=test')}` | ||
); | ||
|
||
search.dispose(); | ||
|
||
await wait(writeWait); | ||
|
||
// URL has been cleaned | ||
expect(window.location.search).toBe(''); | ||
}); | ||
|
||
test('clears url with cleanUrlOnDispose: undefined', async () => { | ||
const router = historyRouter({ writeDelay }); | ||
|
||
const indexName = 'indexName'; | ||
const search = instantsearch({ | ||
indexName, | ||
searchClient: createSearchClient(), | ||
routing: { | ||
router, | ||
}, | ||
}); | ||
|
||
search.addWidgets([ | ||
connectPagination(() => {})({}), | ||
index({ indexName }).addWidgets([connectSearchBox(() => {})({})]), | ||
]); | ||
|
||
search.start(); | ||
|
||
expect(window.location.search).toBe(''); | ||
|
||
// on nested index | ||
search.renderState[indexName].searchBox!.refine('test'); | ||
// on main index | ||
search.renderState[indexName].pagination!.refine(39); | ||
|
||
await wait(writeWait); | ||
|
||
expect(window.location.search).toBe( | ||
`?${encodeURI('indexName[page]=40&indexName[query]=test')}` | ||
); | ||
|
||
search.dispose(); | ||
|
||
await wait(writeWait); | ||
|
||
// URL has been cleaned | ||
expect(window.location.search).toBe(''); | ||
}); |
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