// @ts-check
+
require('./helper')
const expect = require('expect')
const { AbstractHistory } = require('../src/history/abstract')
describe('Abstract/in memory history', () => {
it('starts at /', () => {
const history = new AbstractHistory()
+ expect(history.location).toEqual(START)
expect(history.location).toEqual({
fullPath: '/',
path: '/',
query: {},
hash: '',
})
+ expect(history.queue).toHaveLength(1)
})
it('can push a location', () => {
const history = new AbstractHistory()
- // normalized version
+ // partial version
history.push({ path: '/somewhere', hash: '#hey', query: { foo: 'foo' } })
expect(history.location).toEqual({
fullPath: '/somewhere?foo=foo#hey',
it('add entries to the queue', () => {
const history = new AbstractHistory()
- expect(history.queue).toHaveLength(0)
history.push(loc)
- expect(history.queue).toHaveLength(1)
- expect(history.queue[0]).toEqual(normaliezedLoc)
- history.push(loc2)
expect(history.queue).toHaveLength(2)
- expect(history.queue[1]).toEqual(normaliezedLoc2)
+ expect(history.queue[1]).toEqual(normaliezedLoc)
+ history.push(loc2)
+ expect(history.queue).toHaveLength(3)
+ expect(history.queue[2]).toEqual(normaliezedLoc2)
})
it('can go back', () => {
history.push(loc)
history.push(loc2)
history.back()
- expect(history.queue).toHaveLength(1)
+ expect(history.queue).toHaveLength(3)
expect(history.location).toEqual(normaliezedLoc)
history.back()
- expect(history.queue).toHaveLength(0)
+ expect(history.queue).toHaveLength(3)
+ expect(history.location).toEqual(START)
+ })
+
+ it('does nothing with back if queue contains only one element', () => {
+ const history = new AbstractHistory()
+ history.back()
+ expect(history.location).toEqual(START)
+ })
+
+ it('does nothing with forward if at end of log', () => {
+ const history = new AbstractHistory()
+ history.forward()
+ expect(history.location).toEqual(START)
+ })
+
+ it('can moves back and forth in history queue', () => {
+ const history = new AbstractHistory()
+ history.push(loc)
+ history.push(loc2)
+ history.back()
+ history.back()
expect(history.location).toEqual(START)
+ history.forward()
+ expect(history.location).toEqual(normaliezedLoc)
+ history.forward()
+ expect(history.location).toEqual(normaliezedLoc2)
})
- it('does nothing with back if queue is empty', () => {
+ it('can push in the middle of the history', () => {
const history = new AbstractHistory()
+ history.push(loc)
+ history.push(loc2)
+ history.back()
history.back()
expect(history.location).toEqual(START)
+ history.push(loc2)
+ expect(history.queue).toHaveLength(2)
+ expect(history.location).toEqual(normaliezedLoc2)
+ // does nothing
+ history.forward()
+ expect(history.queue).toHaveLength(2)
+ expect(history.location).toEqual(normaliezedLoc2)
})
- it('does nothing with forward if at end of log', () => {})
})
// private _listeners: NavigationCallback[] = []
private teardowns: Array<() => void> = []
public queue: HistoryLocationNormalized[] = [START]
+ public position: number = 0
constructor() {
super()
- debugger
}
// TODO: is this necessary
}
get location() {
- console.log('read location', this.queue)
- return this.queue[this.queue.length - 1]
+ return this.queue[this.position]
}
set location(location: HistoryLocationNormalized) {
// super() call tries to push before the array is created
- console.log('set location', location)
if (!this.queue) this.queue = []
- // TODO: handle in the middle
- this.queue.push(location)
+ // move the queue cursor forward
+ this.position++
+ if (this.position === this.queue.length) {
+ // we are at the end, we can simply append a new entry
+ this.queue.push(location)
+ } else {
+ // we are in the middle, we remove everything from here in the queue
+ this.queue.splice(this.position)
+ this.queue.push(location)
+ }
}
back() {
- this.queue.pop()
+ if (this.position > 0) this.position--
+ }
+
+ forward() {
+ if (this.position < this.queue.length - 1) this.position++
}
destroy() {