-
Notifications
You must be signed in to change notification settings - Fork 43
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
Added Custom 404 Page. #1753
Added Custom 404 Page. #1753
Conversation
Thanks. Any chance you know how to add a test for this? Something which just directly browses to a route that doesn't exist and then asserts the component being shown is the not found component? |
I have not previously worked on writing test cases in React, but I am willing to study and implement them as needed. If the implementation does not go as expected, I will remove the test cases and provide a report accordingly. |
for more information, see https://pre-commit.ci
}, | ||
]; | ||
|
||
describe.each(testCases)("NotFound Component", ({ path, expectedText }) => { |
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 not sure I see a reason for the describe.each
-- I can't think of a reason we'd need a second test here, there's one single behavior (visiting a non-existing route) that we're testing.
So I'd just inline the "array" and create a single normal test.
const testName = `Render correctly when navigating to '${path}'`; | ||
test(testName, () => { | ||
render( | ||
<MemoryRouter initialEntries={[path]}> |
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 don't think this test is good. If I understand it correctly, what you've done is set up a new router which always renders the NotFound
component, and then browsed to some path (/random-path
-- but even /
would have the same behavior!) and asserted the 404 text is shown.
That isn't useful.
You should test our application renders the NotFound
component on a route not known by our application, otherwise we're testing something completely unconnected to our application, which could be entirely broken (say if you forgot to make the change to index.tsx
-- this test would still pass).
Thank you for guidance. Should I make following changes - Approach - Source - https://testing-library.com/docs/example-react-router/ |
That looks like it has the same issue as your current test, you create a new (memory) router instead of using our real one. |
I have imported the router from index.tsx (line no. 4) and used the spread operator to copy all paths from there, setting the entry point as /random-path. So new (memory) router is used to set initialEntry point of website when all routes are present in route. Are you telling that we should test app on all other existing paths as well ? |
I see, sorry, it's hard to review screenshots. I don't yet understand what the point of that is though, what goes wrong if you literally pass |
If we render without specifying an initial path, the test framework will not know which route to check. To set the initial route, we need to use createMemoryRouter, which allows us to define initialEntries. |
I don't follow why the test framework needs an initial route? The route we check should be one you browse to, i.e. |
If after reading that page it isn't obvious how to write this test I'll make some time in the next few days to figure it out. |
I explored alternative ways to test routing in React but found that createMemoryRouter is the most suitable approach. However, I couldn't find an equivalent method similar to Vue. Given this, should I remove the test implementation and proceed with committing only the addition of the 404 Not Found page? Also, could you please merge only this change? |
I don't plan to merge this without tests (all PRs merged should have tests!). But I'm happy to try to make time to figure out how to best test this if you think what you have is the best you've seen. |
Thanks, I will also try to find a better solution. |
@Julian just wanted to ask the status of the tests for the above. Let me know if you need any help or input from my side. |
I made some time to do the work to add support for Playwright tests and moved your test over to that. Thanks for the PR, appreciated. |
Closes: #1752
📚 Documentation preview 📚: https://bowtie-json-schema--1753.org.readthedocs.build/en/1753/