-
Notifications
You must be signed in to change notification settings - Fork 881
chore(site): update and refactor all custom hook tests that rely on React Router #12219
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
Conversation
Bumping this to prevent it from getting stale – had to put it off to get the clipboard fixes/tests done faster, but I will be getting back to this |
Nearly got this thing done. The tests should be stable now, but one thing I do want to do is remove the (The React Router router objects have a ton of properties that you can freely access, but they're all marked as 'for internal use only'. Not sure if those comments got added after I made the initial version of the helper, or if I missed them) |
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 20 line comment about how cursed this has to be because we're using the undocumented RouterProvider
component has me really concerned about this. I know we were already using that component, but I don't understand why we're using an undocumented component over a component like MemoryRouter
😅
@@ -45,28 +49,23 @@ describe(`${useWorkspaceDuplication.name}`, () => { | |||
expect(result.current.isDuplicationReady).toBe(false); | |||
|
|||
for (let i = 0; i < 10; i++) { | |||
rerender({ workspace: undefined }); | |||
void rerender({ workspace: undefined }); |
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.
this really isn't supposed to be an await
?
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.
Typo I forgot to fix – good catch
|
||
const parsedParams = new URLSearchParams(router.state.location.search); | ||
const extraMetadataEntries = [ | ||
const extraMetadataEntries: readonly [string, 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.
why the switch from as const
?
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 felt like it would surface any accidental errors in a better way, and that it does a better job of communicating intent
With the previous code, I could do something like this:
const extraMetadataEntries = [
["mode", "duplicate"],
["name", `${MockWorkspace.name}-copy`],
["version", MockWorkspace.template_active_version_id],
function obviouslyWrong() {},
null,
["forgotToAddTheSecondTupleElement"]
] as const;
And this would be valid still – the underlying type would get broadened. So while the type mismatch would get caught down the line, redefining the type to be an array of string tuples tells you what types should go in, and is more defensive against typos
site/src/testHelpers/hooks.tsx
Outdated
* we're not relying on internal React Router implementation details that | ||
* could break at a moment's notice | ||
*/ | ||
let escapedLocation!: Location; // Definite assignment via ! |
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.
well that's a bit of typescript I've never seen before. I'm wary of comments that just say "this line of code means x" tho.
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.
It's less a "this line of code means x", and more a "here's a bigger warning sign for something that silences the compiler and is only a single, easy-to-miss character", but I get what you mean
Wish there were a more obvious syntax, but because of grammar rules, you can't even insert type assertions
our testing code seems to differ substantially from the sort of stuff https://v5.reactrouter.com/web/guides/testing recommends |
@aslilac So there's a couple of things this is trying to get around. Sorry for the walls of text, and I want to be very clear: this isn't directed at you, but I'm also trying to get this put into writing before I forget anything:
Now all that said, I can go back to the drawing board on this. What are your main concerns, either from an interface, or an implementation standpoint? |
gotta love that the top google search result was the docs for a 3 year old version of the library 🙃 sorry about that. let me go read the right docs and rereview with that in mind. |
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 gotta admit, I'm still very unhappy about this code, but I at least sort of get why it has to be this nasty. 😅
) { | ||
await waitFor(() => expect(result.current.isDuplicationReady).toBe(true)); | ||
result.current.duplicateWorkspace(); | ||
void act(() => result.current.duplicateWorkspace()); |
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.
another void
/await
?
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.
Yup – will fix
@aslilac Yeah, believe me – I would throw this into a fire if I could |
Part 4 of the hooks milestone
Changes made
renderHookWithAuth
test helper functionrenderHook
'srerender
function does not cause state to get lost or incorrectly refreshedrenderHelpers
file doesn't have a bunch of noiseuseTab
intouseSearchParamsKey
usePaginatedQuery
to new test helper, and verified testsuseWorkspaceDuplication
to the new test helper and verified testsNotes
renderHookWithAuth
took a lot of trial and error. I refactored the code several times to make it as clear as I could, but if there's anything that still isn't clear, please flag that, so I can update the code (or at least add a comment)createMemoryRouter
andrenderHook
wired up and overcome their incompatible APIs