From f7c74bc2d9fb8a587d300482e1d658a021792439 Mon Sep 17 00:00:00 2001 From: Manuel Schiller Date: Thu, 25 Jul 2024 00:13:43 +0200 Subject: [PATCH] tests: consolidate duplicate tests (#2022) --- packages/react-router/tests/index.test.tsx | 107 ---------------- packages/react-router/tests/path.test.ts | 136 +++++++++++++++------ 2 files changed, 102 insertions(+), 141 deletions(-) diff --git a/packages/react-router/tests/index.test.tsx b/packages/react-router/tests/index.test.tsx index 5fcdf95632..efc6ccd75a 100644 --- a/packages/react-router/tests/index.test.tsx +++ b/packages/react-router/tests/index.test.tsx @@ -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') - }) - }) - }) -}) diff --git a/packages/react-router/tests/path.test.ts b/packages/react-router/tests/path.test.ts index b43710313c..d018a4a2db 100644 --- a/packages/react-router/tests/path.test.ts +++ b/packages/react-router/tests/path.test.ts @@ -2,6 +2,7 @@ import exp from 'node:constants' import { describe, expect, it } from 'vitest' import { exactPathTest, + matchByPath, removeBasepath, removeTrailingSlash, resolvePath, @@ -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') + }) + }) }) })