Skip to content
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(react-router): add some basic runtime tests for Link #1590

Merged
merged 1 commit into from
May 21, 2024

Conversation

chorobin
Copy link
Contributor

Again quite high level to test navigation and not internals. I will add some more but wanted to know if you guys are ok with basic high level tests like this

Copy link

nx-cloud bot commented May 13, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit e73be4c. 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.

history: createMemoryHistory({ initialEntries: ['/'] }),
})

await router.load()
Copy link
Contributor

Choose a reason for hiding this comment

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

For real-life testing, should we be doing the router. load() stuff? Because that seems like a departure from what's really happening at runtime

Copy link
Contributor

Choose a reason for hiding this comment

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

How about rendering the router provider and then just waiting for something to be visible on the screen? Shouldn't the loaders be triggered automatically?

Copy link
Contributor Author

@chorobin chorobin May 14, 2024

Choose a reason for hiding this comment

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

Yeah. I just thought I can do this. Stuff like loader, validateSearch and beforeLoad should be ran automatically so I just want to test if they did something. Like redirect or show data returned from the loader etc

@SeanCassiere SeanCassiere changed the title tests: add some basic runtime tests for Link tests(react-router): add some basic runtime tests for Link May 16, 2024
@chorobin chorobin marked this pull request as ready for review May 16, 2024 18:09
@chorobin chorobin force-pushed the link-runtime-tests branch 4 times, most recently from 3c09f4a to 7ab1283 Compare May 16, 2024 20:41
cleanup()
})

describe('Link', () => {
it('should NOT pass the "disabled" prop to the rendered Link component', async () => {
test('when a Link is disabled', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason test is being used instead of it?

Copy link
Contributor Author

@chorobin chorobin May 17, 2024

Choose a reason for hiding this comment

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

I personally prefer actually no nesting in tests. And to just use test. I like my tests super simple

})

expect(postLink).toHaveAttribute('href', '/posts/id1')
expect(postLink).toHaveAttribute('from', '/posts/')
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 checking the <Link> component as React node? or the actual HTML that's printed to the document?

If it is the <Link> component, then cool 👍🏼.

But if it is the HTML anchor element being printed to the document, then maybe that's actually a bug/unintended behaviour? Since from is not a valid HTML Anchor attribute, then it's probably not something we should then be expecting to available in the DOM.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I guess this is a bug!. It's the a tag

expect(onError).toHaveBeenCalledOnce()
})

test('when navigating to /posts with a beforeLoad that redirects', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

In the future perhaps, this should be moved to redirects.tests.tsx and be tested at runtime using both <Link> and a button wired up to useNavigate().

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 guess its hard to know where it truly lives. But I wanted to test the user pressing a link and it redirecting them because I think that's how it's actually used

const postsLink = await screen.findByRole('link', { name: 'Posts' })

expect(postsLink).toHaveClass('inactive')
expect(postsLink).toHaveAttribute('href', '/posts')
Copy link
Contributor

Choose a reason for hiding this comment

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

Could a test also be added in, testing the href value when a basePath value is set on the router.

const router = createRouter({
  basePath: '/app'
})

Seems like a good runtime check to guarantee.

@chorobin chorobin merged commit 022e725 into main May 21, 2024
7 checks passed
@chorobin chorobin deleted the link-runtime-tests branch May 21, 2024 14:30
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.

None yet

3 participants