Skip to content

Conversation

@JJosephttg
Copy link
Contributor

Added the ability to specify an initial route to render with. As someone who wanted to use my original routes in my tests, I found that even though I do initial navigation without detecting changes in my tests using navigate, I would still run into issues where my resolver would get triggered despite the fact I was switching before detecting changes.

This allows navigation to happen before the location listener is set up so that you may perform an initial route navigation without causing any resolvers/guards to run and additionally having to specify await navigate(...) in your tests

Copy link
Member

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

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

Thanks, I like it!

}

it('allows initially rendering a specific route to avoid triggering a resolver for the default route', async () => {
const expectedRoute = 'correct-route';
Copy link
Member

Choose a reason for hiding this comment

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

Could you rename this to the following please.
I do think it makes it intention clearer.

Suggested change
const expectedRoute = 'correct-route';
const initialRoute = 'initial-route';

expect(resolver.isResolved).toBe(false);
expect(screen.queryByText('Secondary Component')).not.toBeInTheDocument();
expect(await screen.findByText('button')).toBeInTheDocument();
Copy link
Member

Choose a reason for hiding this comment

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

There's no need to await here.

Suggested change
expect(await screen.findByText('button')).toBeInTheDocument();
expect(screen.getByText('button')).toBeInTheDocument();
await render(RouterFixtureComponent, {
initialRoute: expectedRoute,
routes,
detectChangesOnRender: false,
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why detect changes is set to false?

Suggested change
detectChangesOnRender: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mainly just to isolate the test from any extra logic, but I can remove it since it doesn't add any value here

@JJosephttg
Copy link
Contributor Author

Just updated the tests based on your suggestions!

@timdeschryver timdeschryver merged commit d3407ca into testing-library:main Feb 18, 2023
@timdeschryver
Copy link
Member

@all-contributors please add @JJosephttg for code, tests

@allcontributors
Copy link
Contributor

@timdeschryver

@JJosephttg already contributed before to code, test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants