Skip to content

Commit

Permalink
Fix empty node bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JAForbes committed Feb 15, 2024
1 parent 7690425 commit b708279
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 46 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
@@ -1,4 +1,7 @@
{
"javascript.format.enable": false,
"typescript.format.enable": false
"typescript.format.enable": false,
"editor.tabSize": 4,
"editor.insertSpaces": false,
"editor.detectIndentation": false
}
17 changes: 15 additions & 2 deletions lib/index.ts
Expand Up @@ -42,6 +42,19 @@ function normalizePath(str: string): string {
return str
}

function joinPath(a:string,b: string): string {
a = normalizePath(a)
b = normalizePath(b)

if (a === '/' ) {
return b
} else if (b === '/') {
return a
} else {
return a + b
}
}

function Superhistory({
_window = globalThis.window,
onChange = () => {},
Expand Down Expand Up @@ -142,7 +155,7 @@ function SuperhistoryChild({
_window.history[`${options.replace ? 'replace' : 'push'}State`](
null,
'',
normalizePath(_prefix) + normalizePath(path),
joinPath(_prefix, path)
)
reportChanges()
}
Expand All @@ -167,7 +180,7 @@ function SuperhistoryChild({
}) {
const child = SuperhistoryChild({
_window,
prefix: _prefix + prefix,
prefix: joinPath(_prefix, prefix),
onChange,
children: parentChildren,
reportChanges,
Expand Down
93 changes: 50 additions & 43 deletions test/index.ts
Expand Up @@ -3,61 +3,68 @@ import test from 'node:test'
import assert from 'node:assert'
import jsdom from 'jsdom'

function setup(){
const {window} = new jsdom.JSDOM(`<html><body></body></html>`, {
url: 'https://example.com'
});
const A = superhistory({ _window: window as any })

return { superhistory: A, window: window as any as Window }
function setup() {
const { window } = new jsdom.JSDOM(`<html><body></body></html>`, {
url: 'https://example.com',
})

const A = superhistory({ _window: window as any })

return { superhistory: A, window: window as any as Window }
}

test('Basics', () => {
const { superhistory: A, window } = setup()
const { superhistory: A, window } = setup()

A.go('/a/b/c/d/e/f/g', { replace: true })
const a = A.get()
A.go('/a/b/c/d/e/f/g/', { replace: true })
const b = A.get()

A.go('/a/b/c/d/e/f/g', { replace: true })
const a = A.get()
A.go('/a/b/c/d/e/f/g/', { replace: true })
const b = A.get()
assert.deepEqual(a, { path: '/a/b/c/d/e/f/g' })
assert.deepEqual(a, b, 'path normalization')

assert.deepEqual(a, { path: '/a/b/c/d/e/f/g' })
assert.deepEqual(a, b, 'path normalization')
const B1 = A.child({ prefix: '/a/b' })
const B2 = A.child({ prefix: '/a/b/' })

const B1 = A.child({ prefix: '/a/b' })
const B2 = A.child({ prefix: '/a/b/'})
assert.deepEqual(B1.get(), { path: '/c/d/e/f/g' })
assert.deepEqual(B1.get(), B2.get())

assert.deepEqual(B1.get(), { path: '/c/d/e/f/g' })
assert.deepEqual(B1.get(), B2.get())
A.go('/a/c/d')
assert.strictEqual(
B1.get().path,
undefined,
`If child path isn't applicable, it has an undefined path`,
)

A.go('/a/c/d')
assert.strictEqual(
B1.get().path
, undefined
, `If child path isn't applicable, it has an undefined path`
)
assert.deepEqual(A.get(), { path: '/a/c/d' })
B1.go('/d')
assert.deepEqual(
A.get(),
{ path: '/a/b/d' },
'Writing to subroute reactivated its original prefix',
)

assert.deepEqual(A.get(), { path: '/a/c/d' })
B1.go('/d')
assert.deepEqual(A.get(), { path: '/a/b/d' }, 'Writing to subroute reactivated its original prefix')
assert.deepEqual(B1.get(), { path: '/d' }, 'And path is no longer undefined')
assert.deepEqual(B1.get(), B2.get(), 'B1 and B2 are in sync')

assert.deepEqual(B1.get(), { path: '/d'}, 'And path is no longer undefined')
assert.deepEqual(B1.get(), B2.get(), 'B1 and B2 are in sync')
B2.go('/c')

B2.go('/c')

const C1 = B1.child({ prefix: '/c' })
const C2 = B2.child({ prefix: '/c' })
const C1 = B1.child({ prefix: '/c' })
const C2 = B2.child({ prefix: '/c' })

assert.deepEqual(C1.get(), { path: '/' })
assert.deepEqual(C1.get(), { path: '/' })
})

test('Child with empty prefix', () => {

const {superhistory} = setup()
test('Children of empty prefix', () => {
const { superhistory } = setup()

superhistory.go('/financials/edit/1')
const A = superhistory.child({ prefix: ''})

assert.equal('/financials/edit/1', A.get().path)
})
superhistory.go('/financials/edit/1')
const A = superhistory.child({ prefix: '' })
const B = A.child({ prefix: '/financials' })
const C = B.child({ prefix: '/edit' })

assert.equal('/financials/edit/1', A.get().path)
assert.equal(B.get().path, '/edit/1')
assert.equal(C.get().path, '/1')
})

0 comments on commit b708279

Please sign in to comment.