From e7b9854e5d2503c7980e538eecf577188311e42e Mon Sep 17 00:00:00 2001 From: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> Date: Mon, 21 Apr 2025 19:25:27 +1200 Subject: [PATCH 1/4] test(router-core): group the basepath tests for `matchPathname` --- packages/router-core/tests/path.test.ts | 93 +++++++++++++------------ 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/packages/router-core/tests/path.test.ts b/packages/router-core/tests/path.test.ts index 8c86975b1d0..515a08d4392 100644 --- a/packages/router-core/tests/path.test.ts +++ b/packages/router-core/tests/path.test.ts @@ -337,55 +337,60 @@ describe('interpolatePath', () => { }) describe('matchPathname', () => { - it.each([ - { - name: 'should match the root path that start with the basepath', - basepath: '/basepath', - pathname: '/basepath', - matchLocation: { - to: '/', + describe('basepath matching', () => { + it.each([ + { + name: 'should match when the input is the same as the basepath', + basepath: '/basepath', + input: '/basepath', + matchingOptions: { + to: '/', + }, + expectedMatchedParams: {}, }, - expected: {}, - }, - { - name: 'should match the path that start with the basepath', - basepath: '/basepath', - pathname: '/basepath/abc', - matchLocation: { - to: '/abc', + { + name: 'should match when the input starts with the basepath and `to` is set to the remaining', + basepath: '/basepath', + input: '/basepath/abc', + matchingOptions: { + to: '/abc', + }, + expectedMatchedParams: {}, }, - expected: {}, - }, - { - name: 'should not match the root path that does not start with the basepath', - basepath: '/basepath', - pathname: '/', - matchLocation: { - to: '/', + { + name: 'should not match when the input is `/` and does not start with the basepath', + basepath: '/basepath', + input: '/', + matchingOptions: { + to: '/', + }, + expectedMatchedParams: undefined, }, - expected: undefined, - }, - { - name: 'should not match the path that does not start with the basepath', - basepath: '/basepath', - pathname: '/abc', - matchLocation: { - to: '/abc', + { + name: 'should not match when the input completely does not start with the basepath', + basepath: '/basepath', + input: '/abc', + matchingOptions: { + to: '/abc', + }, + expectedMatchedParams: undefined, }, - expected: undefined, - }, - { - name: 'should not match the path that match partial of the basepath', - basepath: '/base', - pathname: '/basepath/abc', - matchLocation: { - to: '/abc', + { + name: 'should not match when the input only partially matches the basepath', + basepath: '/base', + input: '/basepath/abc', + matchingOptions: { + to: '/abc', + }, + expectedMatchedParams: undefined, + }, + ])( + '$name', + ({ basepath, input, matchingOptions, expectedMatchedParams }) => { + expect(matchPathname(basepath, input, matchingOptions)).toStrictEqual( + expectedMatchedParams, + ) }, - expected: undefined, - }, - ])('$name', ({ basepath, pathname, matchLocation, expected }) => { - expect(matchPathname(basepath, pathname, matchLocation)).toStrictEqual( - expected, ) }) }) From 6e8af24a607b0a22991e71ec50c9f26193477ce0 Mon Sep 17 00:00:00 2001 From: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> Date: Mon, 21 Apr 2025 19:34:40 +1200 Subject: [PATCH 2/4] test(router-core): params matching expectations for `matchPathname` --- packages/router-core/tests/path.test.ts | 70 +++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/packages/router-core/tests/path.test.ts b/packages/router-core/tests/path.test.ts index 515a08d4392..3a73beb37bf 100644 --- a/packages/router-core/tests/path.test.ts +++ b/packages/router-core/tests/path.test.ts @@ -393,4 +393,74 @@ describe('matchPathname', () => { }, ) }) + + describe('path param(s) matching', () => { + it.each([ + { + name: 'should not match since `to` does not match the input', + input: '/', + matchingOptions: { + to: '/users', + }, + expectedMatchedParams: undefined, + }, + { + name: 'should match since `to` matches the input', + input: '/users', + matchingOptions: { + to: '/users', + }, + expectedMatchedParams: {}, + }, + { + name: 'should match and return the named path params', + input: '/users/123', + matchingOptions: { + to: '/users/$id', + }, + expectedMatchedParams: { id: '123' }, + }, + { + name: 'should match and return the the splat param', + input: '/users/123', + matchingOptions: { + to: '/users/$', + }, + expectedMatchedParams: { + '*': '123', + _splat: '123', + }, + }, + { + name: 'should match and return the named path and splat params', + input: '/users/123/456', + matchingOptions: { + to: '/users/$id/$', + }, + expectedMatchedParams: { + id: '123', + '*': '456', + _splat: '456', + }, + }, + { + name: 'should match and return the multiple named path params and splat param', + input: '/sean-cassiere/settings/my-repo/123/456', + matchingOptions: { + to: '/$username/settings/$repo/$id/$', + }, + expectedMatchedParams: { + username: 'sean-cassiere', + repo: 'my-repo', + id: '123', + '*': '456', + _splat: '456', + }, + }, + ])('$name', ({ input, matchingOptions, expectedMatchedParams }) => { + expect(matchPathname('/', input, matchingOptions)).toStrictEqual( + expectedMatchedParams, + ) + }) + }) }) From 9a44742e027321d454657e916038482ee7a1b78c Mon Sep 17 00:00:00 2001 From: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> Date: Mon, 21 Apr 2025 19:40:52 +1200 Subject: [PATCH 3/4] test(router-core): correctly group the `interpolatePath` tests --- packages/router-core/tests/path.test.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/router-core/tests/path.test.ts b/packages/router-core/tests/path.test.ts index 3a73beb37bf..475526b4c16 100644 --- a/packages/router-core/tests/path.test.ts +++ b/packages/router-core/tests/path.test.ts @@ -266,7 +266,7 @@ describe('resolvePath', () => { }) describe('interpolatePath', () => { - ;[ + it.each([ { name: 'should interpolate the path', path: '/users/$id', @@ -324,15 +324,14 @@ describe('interpolatePath', () => { ['@', '+'].map((char) => [encodeURIComponent(char), char]), ), }, - ].forEach((exp) => { - it(exp.name, () => { - const result = interpolatePath({ - path: exp.path, - params: exp.params, - decodeCharMap: exp.decodeCharMap, - }).interpolatedPath - expect(result).toBe(exp.result) - }) + ])('$name', ({ path, params, decodeCharMap, result }) => { + expect( + interpolatePath({ + path, + params, + decodeCharMap, + }).interpolatedPath, + ).toBe(result) }) }) From f187b6fd5754e8fd2c60fbddd786669989b012ec Mon Sep 17 00:00:00 2001 From: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> Date: Mon, 21 Apr 2025 19:42:43 +1200 Subject: [PATCH 4/4] test(router-core): add more for `interpolatePath` --- packages/router-core/tests/path.test.ts | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/router-core/tests/path.test.ts b/packages/router-core/tests/path.test.ts index 475526b4c16..0b709d24539 100644 --- a/packages/router-core/tests/path.test.ts +++ b/packages/router-core/tests/path.test.ts @@ -324,6 +324,41 @@ describe('interpolatePath', () => { ['@', '+'].map((char) => [encodeURIComponent(char), char]), ), }, + { + name: 'should interpolate the path with the splat param at the end', + path: '/users/$', + params: { _splat: '123' }, + result: '/users/123', + }, + { + name: 'should interpolate the path with a single named path param and the splat param at the end', + path: '/users/$username/$', + params: { username: 'seancassiere', _splat: '123' }, + result: '/users/seancassiere/123', + }, + { + name: 'should interpolate the path with 2 named path params with the splat param at the end', + path: '/users/$username/$id/$', + params: { username: 'seancassiere', id: '123', _splat: '456' }, + result: '/users/seancassiere/123/456', + }, + { + name: 'should interpolate the path with multiple named path params with the splat param at the end', + path: '/$username/settings/$repo/$id/$', + params: { + username: 'sean-cassiere', + repo: 'my-repo', + id: '123', + _splat: '456', + }, + result: '/sean-cassiere/settings/my-repo/123/456', + }, + { + name: 'should interpolate the path with the splat param containing slashes', + path: '/users/$', + params: { _splat: 'sean/cassiere' }, + result: '/users/sean/cassiere', + }, ])('$name', ({ path, params, decodeCharMap, result }) => { expect( interpolatePath({