Skip to content

Commit

Permalink
tests: consolidate duplicate tests (#2022)
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel committed Jul 24, 2024
1 parent a729694 commit f7c74bc
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 141 deletions.
107 changes: 0 additions & 107 deletions packages/react-router/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -654,110 +654,3 @@ describe('ssr redirects', () => {
})
})
})

describe('resolvePath', () => {
;(
[
['/', '/', '/', '/'],
['/', '/', '/a', '/a'],
['/', '/', 'a/', '/a'],
['/', '/', '/a/b', '/a/b'],
['/', 'a', 'b', '/a/b'],
['/a/b', 'c', '/a/b/c', '/a/b/c'],
['/a/b', '/', 'c', '/a/b/c'],
['/a/b', '/', './c', '/a/b/c'],
['/', '/', 'a/b', '/a/b'],
['/', '/', './a/b', '/a/b'],
['/', '/a/b/c', 'd', '/a/b/c/d'],
['/', '/a/b/c', './d', '/a/b/c/d'],
['/', '/a/b/c', './../d', '/a/b/d'],
['/', '/a/b/c/d', './../d', '/a/b/c/d'],
['/', '/a/b/c', '../d', '/a/b/d'],
['/', '/a/b/c', '../../d', '/a/d'],
['/', '/a/b/c', '..', '/a/b'],
['/', '/a/b/c', '../..', '/a'],
['/', '/a/b/c', '../../..', '/'],
['/', '/a/b/c/', '../../..', '/'],
] as const
).forEach(([base, a, b, eq]) => {
test(`Base: ${base} - ${a} to ${b} === ${eq}`, () => {
expect(resolvePath({ basepath: base, base: a, to: b })).toEqual(eq)
})
test(`Base: ${base} - ${a}/ to ${b} === ${eq} (trailing slash)`, () => {
expect(resolvePath({ basepath: base, base: a + '/', to: b })).toEqual(eq)
})
test(`Base: ${base} - ${a}/ to ${b}/ === ${eq} (trailing slash + trailing slash)`, () => {
expect(
resolvePath({ basepath: base, base: a + '/', to: b + '/' }),
).toEqual(eq)
})
})
describe('trailingSlash', () => {
describe(`'always'`, () => {
test('keeps trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd/',
trailingSlash: 'always',
}),
).toBe('/a/b/c/d/')
})
test('adds trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd',
trailingSlash: 'always',
}),
).toBe('/a/b/c/d/')
})
})
describe(`'never'`, () => {
test('removes trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd/',
trailingSlash: 'never',
}),
).toBe('/a/b/c/d')
})
test('does not add trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd',
trailingSlash: 'never',
}),
).toBe('/a/b/c/d')
})
})
describe(`'preserve'`, () => {
test('keeps trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd/',
trailingSlash: 'preserve',
}),
).toBe('/a/b/c/d/')
})
test('does not add trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd',
trailingSlash: 'preserve',
}),
).toBe('/a/b/c/d')
})
})
})
})
136 changes: 102 additions & 34 deletions packages/react-router/tests/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import exp from 'node:constants'
import { describe, expect, it } from 'vitest'
import {
exactPathTest,
matchByPath,
removeBasepath,
removeTrailingSlash,
resolvePath,
Expand Down Expand Up @@ -120,40 +121,107 @@ describe.each([{ basepath: '/' }, { basepath: '/app' }, { basepath: '/app/' }])(
)

describe('resolvePath', () => {
it.each([
{ base: '/a/b/c', to: './d', expected: '/a/b/c/d' },
{ base: '/a/b/c', to: '../d', expected: '/a/b/d' },
{ base: '/a/b/c', to: './d/', expected: '/a/b/c/d' },
{ base: '/a/b/c', to: './', expected: '/a/b/c' },
])(
'should resolve relative paths: $base + $to = $expected',
({ base, to, expected }) => {
expect(resolvePath({ basepath: '/', base, to })).toBe(expected)
},
)

it.each([
{ base: '/a/b/c', to: '/d', expected: '/d' },
{ base: '/a/b/c', to: '/d/', expected: '/d' },
{ base: '/a/b/c', to: '/', expected: '/' },
])(
'should resolve absolute paths that start with `/`: $base + $to = $expected',
({ base, to, expected }) => {
expect(resolvePath({ basepath: '/', base, to })).toBe(expected)
},
)

it.each([
{ base: '/a/b/c', to: 'd', expected: '/a/b/c/d' },
{ base: '/a/b/c', to: 'd/', expected: '/a/b/c/d' },
{ base: '/a/b/c', to: 'd/e', expected: '/a/b/c/d/e' },
])('should resolve non-.-prefixed paths', ({ base, to, expected }) => {
expect(resolvePath({ basepath: '/', base, to })).toBe(expected)
describe.each([
['/', '/', '/', '/'],
['/', '/', '/a', '/a'],
['/', '/', 'a/', '/a'],
['/', '/', '/a/b', '/a/b'],
['/', 'a', 'b', '/a/b'],
['/a/b', 'c', '/a/b/c', '/a/b/c'],
['/a/b', '/', 'c', '/a/b/c'],
['/a/b', '/', './c', '/a/b/c'],
['/', '/', 'a/b', '/a/b'],
['/', '/', './a/b', '/a/b'],
['/', '/a/b/c', 'd', '/a/b/c/d'],
['/', '/a/b/c', './d', '/a/b/c/d'],
['/', '/a/b/c', './../d', '/a/b/d'],
['/', '/a/b/c/d', './../d', '/a/b/c/d'],
['/', '/a/b/c', '../d', '/a/b/d'],
['/', '/a/b/c', '../../d', '/a/d'],
['/', '/a/b/c', '..', '/a/b'],
['/', '/a/b/c', '../..', '/a'],
['/', '/a/b/c', '../../..', '/'],
['/', '/a/b/c/', '../../..', '/'],
['/products', '/', '/products-list', '/products/products-list'],
])('resolves correctly', (base, a, b, eq) => {
it(`Base: ${base} - ${a} to ${b} === ${eq}`, () => {
expect(resolvePath({ basepath: base, base: a, to: b })).toEqual(eq)
})
it(`Base: ${base} - ${a}/ to ${b} === ${eq} (trailing slash)`, () => {
expect(resolvePath({ basepath: base, base: a + '/', to: b })).toEqual(eq)
})
it(`Base: ${base} - ${a}/ to ${b}/ === ${eq} (trailing slash + trailing slash)`, () => {
expect(
resolvePath({ basepath: base, base: a + '/', to: b + '/' }),
).toEqual(eq)
})
})

it('should correctly resolve paths when basepath is set', () => {
expect(
resolvePath({ basepath: '/products', base: '/', to: '/products-list' }),
).toBe('/products/products-list')
describe('trailingSlash', () => {
describe(`'always'`, () => {
it('keeps trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd/',
trailingSlash: 'always',
}),
).toBe('/a/b/c/d/')
})
it('adds trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd',
trailingSlash: 'always',
}),
).toBe('/a/b/c/d/')
})
})
describe(`'never'`, () => {
it('removes trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd/',
trailingSlash: 'never',
}),
).toBe('/a/b/c/d')
})
it('does not add trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd',
trailingSlash: 'never',
}),
).toBe('/a/b/c/d')
})
})
describe(`'preserve'`, () => {
it('keeps trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd/',
trailingSlash: 'preserve',
}),
).toBe('/a/b/c/d/')
})
it('does not add trailing slash', () => {
expect(
resolvePath({
basepath: '/',
base: '/a/b/c',
to: 'd',
trailingSlash: 'preserve',
}),
).toBe('/a/b/c/d')
})
})
})
})

0 comments on commit f7c74bc

Please sign in to comment.