Skip to content

Conversation

@chorobin
Copy link
Contributor

@chorobin chorobin commented Apr 2, 2024

  • Tests were failing and were not type checking in ci
  • Added test cases for to being a union, params being optional if from and to contain the same params

@nx-cloud
Copy link

nx-cloud bot commented Apr 2, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit e58250f. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

params.parameter(0).toEqualTypeOf<{ invoiceId: string }>()
})

test('when navigating to a union of routes, params is optional', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this correct though? shouldn't params be { invoiceId: string } | { postId: string}`?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I was expecting this test:

https://github.com/TanStack/router/pull/1418/files#diff-921603b68301ae5e52534582aeb75f7defc2946a96ad3b268b28ad34ae5fec04R262

why would we need this test here? the test below already checks everything, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will double check but I believe this is slightly different as we're checking if it's optional. But it's a good point if this is intentional behaviour or not. Should it be optional for a union of different routes?

I find the unions a harder use case to think about if it should be optional or not. For example the default case string falls under the same code path and this makes sense to be optional

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string is basically considered to be any route and is therefore a union of everything

Copy link
Contributor Author

@chorobin chorobin Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I was expecting this test:

https://github.com/TanStack/router/pull/1418/files#diff-921603b68301ae5e52534582aeb75f7defc2946a96ad3b268b28ad34ae5fec04R262

why would we need this test here? the test below already checks everything, right?

Yep. I can explain why there is a seperate test here. The test you're referring too checks if the type of params can be the following (excluding the function and boolean types)

{ invoiceId: string } | { postId: string } | undefined

This is not the same as checking if the property is optional in typescript. A type can be undefined but still required. Therefore I think this check is necessary because we need to ensure its optional explicitly. I'd rather not assume undefined means optional because its not the same.

Please correct me if you think I'm wrong

string,
'/invoices/$invoiceId/' | '/posts/$postId/'
>
expectTypeOf(TestLink).parameter(0).not.toMatchTypeOf<{ params: unknown }>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the type check for "params is optional"? how? and is there a more explicit way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@chorobin chorobin Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to fix these comments tomorrow so don't merge. But you have me doubting myself so I will check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to think of a different way but it's difficult as the types can be quite complex for params

.toEqualTypeOf<{} | { invoiceId: string } | { postId: string }>()
})

test('when navigating to a union of routes including the route, params is optional', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"including the route" .. which one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah you meant "the root", right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Sorry, bad spelling

params.parameter(0).toEqualTypeOf<{} | { page: number }>()
})

test('when navigating to a union of routes including the route, search params is optional', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"the route" => "the root"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Same mistake!

Copy link
Member

@SeanCassiere SeanCassiere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but I believe we aren't testing for external links.

The implementation for the <Link> component checks for external links and sets the href property to whatever the input of to was.

Not sure if that's something that'll be covered by this PR or not, just putting it out there since the types for Link are being touched here.

@chorobin
Copy link
Contributor Author

chorobin commented Apr 3, 2024

Correct me if I'm wrong, but I believe we aren't testing for external links.

The implementation for the <Link> component checks for external links and sets the href property to whatever the input of to was.

Not sure if that's something that'll be covered by this PR or not, just putting it out there since the types for Link are being touched here.

Good point. Maybe we can do sanity check that we allow any string literal type in to. But bare in mind these are only type tests and doesn't test the runtime behaviour. It would be good to have a Link.test.tsx file which tests the runtime

We need both but since I'm touching the types most of the time, that's what I see break the most so I wanted to add some.

But yes. I agree we need tests for this scenario aswell

chorobin added 2 commits April 4, 2024 11:24
There was a lot of repetition in the tests, I think its simpler to group
the tests by use cases and have multiple expects for the use case
@schiller-manuel schiller-manuel merged commit 03a91d8 into TanStack:main Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants