const history = createMemoryHistory()
history.push(loc)
history.push(loc2)
- history.back()
+ history.go(-1)
expect(history.location).toEqual(normalizedLoc)
- history.back()
+ history.go(-1)
expect(history.location).toEqual(START)
})
it('does nothing with back if queue contains only one element', () => {
const history = createMemoryHistory()
- history.back()
+ history.go(-1)
expect(history.location).toEqual(START)
})
it('does nothing with forward if at end of log', () => {
const history = createMemoryHistory()
- history.forward()
+ history.go(1)
expect(history.location).toEqual(START)
})
const history = createMemoryHistory()
history.push(loc)
history.push(loc2)
- history.back()
- history.back()
+ history.go(-1)
+ history.go(-1)
expect(history.location).toEqual(START)
- history.forward()
+ history.go(1)
expect(history.location).toEqual(normalizedLoc)
- history.forward()
+ history.go(1)
expect(history.location).toEqual(normalizedLoc2)
})
const history = createMemoryHistory()
history.push(loc)
history.push(loc2)
- history.back()
- history.back()
+ history.go(-1)
+ history.go(-1)
expect(history.location).toEqual(START)
history.push(loc2)
expect(history.location).toEqual(normalizedLoc2)
// does nothing
- history.forward()
+ history.go(1)
expect(history.location).toEqual(normalizedLoc2)
})
const spy = jest.fn()
history.listen(spy)
history.push(loc)
- history.back()
+ history.go(-1)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith(START, normalizedLoc, {
direction: 'back',
distance: -1,
type: 'pop',
})
- history.forward()
+ history.go(1)
expect(spy).toHaveBeenCalledTimes(2)
expect(spy).toHaveBeenLastCalledWith(normalizedLoc, START, {
direction: 'forward',
history.listen(spy)()
const remove = history.listen(spy2)
history.push(loc)
- history.back()
+ history.go(-1)
expect(spy).not.toHaveBeenCalled()
expect(spy2).toHaveBeenCalledTimes(1)
remove()
- history.forward()
+ history.go(1)
expect(spy).not.toHaveBeenCalled()
expect(spy2).toHaveBeenCalledTimes(1)
})
rem()
rem()
history.push(loc)
- history.back()
+ history.go(-1)
expect(spy).not.toHaveBeenCalled()
expect(spy2).toHaveBeenCalledTimes(1)
rem2()
rem2()
- history.forward()
+ history.go(1)
expect(spy).not.toHaveBeenCalled()
expect(spy2).toHaveBeenCalledTimes(1)
})
const spy = jest.fn()
history.listen(spy)
history.destroy()
- history.back()
+ history.go(-1)
expect(spy).not.toHaveBeenCalled()
})
const spy = jest.fn()
history.listen(spy)
history.push(loc)
- history.back(false)
+ history.go(-1, false)
expect(spy).not.toHaveBeenCalled()
- history.forward(false)
+ history.go(1, false)
expect(spy).not.toHaveBeenCalled()
})
})
replace(to: RawHistoryLocation, data?: HistoryState): void
/**
- * Goes back in history
+ * Traverses history in a given direction.
*
- * @param triggerListeners - whether this should trigger listeners attached to
- * the history
- */
- back(triggerListeners?: boolean): void
- /**
- * Goes forward in history
- *
- * @param triggerListeners - whether this should trigger listeners attached to
- * the history
- */
- forward(triggerListeners?: boolean): void
- /**
- * Traverses history in a given direction
+ * @example
+ * ```js
+ * myHistory.go(-1) // equivalent to window.history.back()
+ * myHistory.go(1) // equivalent to window.history.forward()
+ * ```
*
* @param distance - distance to travel. If distance is \< 0, it will go back,
* if it's \> 0, it will go forward
historyNavigation.location,
historyNavigation.replace
)
- function back(triggerListeners = true) {
- go(-1, triggerListeners)
- }
- function forward(triggerListeners = true) {
- go(1, triggerListeners)
- }
function go(distance: number, triggerListeners = true) {
if (!triggerListeners) historyListeners.pauseListeners()
history.go(distance)
// @ts-ignore
location: '',
base,
- back,
- forward,
go,
...historyNavigation,
listeners = []
},
- back(shouldTrigger = true) {
- this.go(-1, shouldTrigger)
- },
-
- forward(shouldTrigger = true) {
- this.go(1, shouldTrigger)
- },
-
go(distance, shouldTrigger = true) {
const from = this.location
const direction: NavigationDirection =