-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
tests: fix type tests for Link, make sure type tests fail in ci and add type tests for union to and difference in params
#1418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests: fix type tests for Link, make sure type tests fail in ci and add type tests for union to and difference in params
#1418
Conversation
…dd type tests for union `to` and difference in params
☁️ Nx Cloud ReportCI 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 targetsSent with 💌 from NxCloud. |
| params.parameter(0).toEqualTypeOf<{ invoiceId: string }>() | ||
| }) | ||
|
|
||
| test('when navigating to a union of routes, params is optional', () => { |
There was a problem hiding this comment.
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}`?
There was a problem hiding this comment.
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:
why would we need this test here? the test below already checks everything, right?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
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 }>() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs cover this
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"the route" => "the root"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. Same mistake!
SeanCassiere
left a comment
There was a problem hiding this 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.
Good point. Maybe we can do sanity check that we allow any string literal type in 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 |
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
tobeing a union, params being optional iffromandtocontain the same params