})
})
- it('saves forward information', () => {})
+ it('can replace a location', () => {
+ const history = new AbstractHistory()
+ // partial version
+ history.replace({ path: '/somewhere', hash: '#hey', query: { foo: 'foo' } })
+ expect(history.location).toEqual({
+ fullPath: '/somewhere?foo=foo#hey',
+ path: '/somewhere',
+ query: { foo: 'foo' },
+ hash: '#hey',
+ })
+ expect(history.position).toEqual(0)
+ expect(history.queue).toHaveLength(1)
+ history.push(loc)
+
+ // partial version
+ history.replace({ path: '/path', hash: '#ho' })
+ expect(history.location).toEqual({
+ fullPath: '/path#ho',
+ path: '/path',
+ query: {},
+ hash: '#ho',
+ })
+ expect(history.position).toEqual(1)
+ expect(history.queue).toHaveLength(2)
+ })
- it('can replace a location', () => {})
+ it('does not trigger listeners with push', () => {
+ const history = new AbstractHistory()
+ const spy = jest.fn()
+ history.listen(spy)
+ history.push(loc)
+ expect(spy).not.toHaveBeenCalled()
+ })
- it('can simulate a navigation', () => {})
+ it('does not trigger listeners with replace', () => {
+ const history = new AbstractHistory()
+ const spy = jest.fn()
+ history.listen(spy)
+ history.replace(loc)
+ expect(spy).not.toHaveBeenCalled()
+ })
it('add entries to the queue', () => {
const history = new AbstractHistory()
expect(spy).not.toHaveBeenCalled()
expect(spy2).toHaveBeenCalledTimes(1)
})
+
+ it('removing the same listener is a noop', () => {
+ const history = new AbstractHistory()
+ const spy = jest.fn()
+ const spy2 = jest.fn()
+ const rem = history.listen(spy)
+ const rem2 = history.listen(spy2)
+ rem()
+ rem()
+ history.push(loc)
+ history.back()
+ expect(spy).not.toHaveBeenCalled()
+ expect(spy2).toHaveBeenCalledTimes(1)
+ rem2()
+ rem2()
+ history.forward()
+ expect(spy).not.toHaveBeenCalled()
+ expect(spy2).toHaveBeenCalledTimes(1)
+ })
+
+ it('removes all listeners with destroy', () => {
+ const history = new AbstractHistory()
+ const spy = jest.fn()
+ history.listen(spy)
+ // @ts-ignore
+ expect(history.listeners).toHaveLength(1)
+ history.destroy()
+ // @ts-ignore
+ expect(history.listeners).toHaveLength(0)
+ })
})
// const cs = consola.withTag('abstract')
export class AbstractHistory extends BaseHistory {
- // private _listeners: NavigationCallback[] = []
- private teardowns: Array<() => void> = []
private listeners: NavigationCallback[] = []
public queue: HistoryLocationNormalized[] = [START]
public position: number = 0
// TODO: is this necessary
ensureLocation() {}
- replace(to: HistoryLocation) {}
+ replace(to: HistoryLocation) {
+ const toNormalized = this.utils.normalizeLocation(to)
+ // remove current entry and decrement position
+ this.queue.splice(this.position--, 1)
+ this.location = toNormalized
+ }
push(to: HistoryLocation, data?: HistoryState) {
const toNormalized = this.utils.normalizeLocation(to)
}
destroy() {
- for (const teardown of this.teardowns) teardown()
- this.teardowns = []
+ this.listeners = []
}
private triggerListeners(