import { JSDOM } from 'jsdom'
import { createWebHistory } from '../../src/history/html5'
-import { createDom } from '../utils'
+import { createDom, tick } from '../utils'
// override the value of isBrowser because the variable is created before JSDOM
// is created
expect(createWebHistory('#other').base).toBe('#other')
})
+ it('grecefully handles an invalid state', async () => {
+ const history = createWebHistory()
+ const spy = jest.fn()
+ history.listen(spy)
+
+ // null state
+ window.history.pushState(null, '')
+ window.history.back()
+ await tick(2)
+ expect(spy).toHaveBeenCalled()
+ expect(spy).toHaveBeenCalledWith('', '', 'https://example.com/foo')
+
+ // invalid state but not empty
+ window.history.pushState({}, '')
+ window.history.back()
+ await tick()
+ expect(spy).toHaveBeenCalled()
+ expect(spy).toHaveBeenCalledWith('', '', 'https://example.com/foo')
+ })
+
it('handles a base tag', () => {
const baseEl = document.createElement('base')
baseEl.href = '/foo/'
const popStateHandler: PopStateListener = ({
state,
}: {
- state: StateEntry | null
+ // use an empty object to accomodate for other kind of invalid states
+ // https://github.com/vuejs/vue-router-next/pull/1219
+ state: StateEntry | null | { current: never }
}) => {
const to = createCurrentLocation(base, location)
const from: HistoryLocation = currentLocation.value
let delta = 0
if (state) {
+ if (!state.current) {
+ state = assign({}, historyState.value, {
+ back: historyState.value.current,
+ current: to,
+ position: historyState.value.position + 1,
+ })
+ }
+
currentLocation.value = to
historyState.value = state