Skip to content

Conversation

@sirbully
Copy link
Contributor

@sirbully sirbully commented Jun 29, 2025

Resolves #99

What changed 🧐

Tests for BaseLayout and Router

How did you test it? 🧪

Mocked child components and assert component behavior

@sirbully sirbully self-assigned this Jun 29, 2025
@sirbully sirbully requested a review from ann-kilzer as a code owner June 29, 2025 12:19
@sirbully sirbully changed the base branch from main to image-card-test June 29, 2025 12:19
)
})

it('should render Suspense with fallback loader', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's be careful here that we're not writing tests for React / React Router core features. That is outside the scope of our project

Copy link
Contributor Author

@sirbully sirbully Aug 11, 2025

Choose a reason for hiding this comment

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

I wanted to test if the "loading..." text is rendered when suspended (mostly to get 100 coverage for BaseLayout) but we can remove it if unneeded

Copy link
Collaborator

@ann-kilzer ann-kilzer Aug 13, 2025

Choose a reason for hiding this comment

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

Oh, 100% coverage isn't something that necessarily brings better quality. Usually the best benefit is 80-90%.

Anyway, the test is easier to read understand now so I think it's good to leave in.

Copy link
Collaborator

@ann-kilzer ann-kilzer left a comment

Choose a reason for hiding this comment

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

Thanks for working on this, it's a more complex component to test, and I think the mocking is a good call.

</TestWrapper>
)

// Should render without errors
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's good we're ensuring the base layout is working. Can we add assertions on the other parts of the router?

If it's cumbersome, feel free to pause and we can discuss if it's better to focus on E2E tests to verify the router works well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

looking at it now, i think you are right, we should just focus on testing routing behavior with E2E tests 🙏

const data = mockFetchData()
if (data instanceof Promise) {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw data
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we throwing a Promise? This seems odd

Copy link
Contributor Author

@sirbully sirbully Aug 11, 2025

Choose a reason for hiding this comment

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

this is a "hack" that I learned from my coworker and the old data fetching strategy we used for our products in the past. basically, throwing the Promise (pending data) triggers the suspense and shows the fallback, then re-renders the component when the Promise has resolved.

i've changed it to use lazy instead as it can also trigger the suspense.

in other news, the "throw a Promise" hack to trigger the suspense will be deprecated in a future version of react

@github-actions
Copy link

This PR is stale. Consider a pair-programming session to help move it forward.

Copy link
Contributor Author

@sirbully sirbully Aug 11, 2025

Choose a reason for hiding this comment

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

kept this unit test lean, i know coverage isn't everything but having this test satisfies 100% coverage for Router.tsx, definitely better to test routing behavior in E2E

</CustomThemeProvider>
// Create a lazy component for testing loading state
const MockLazyChildComponent = lazy(() =>
new Promise(() => {}) // Never resolves, so Suspense will show fallback
Copy link
Collaborator

Choose a reason for hiding this comment

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

cool

Copy link
Collaborator

@ann-kilzer ann-kilzer left a comment

Choose a reason for hiding this comment

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

This has really improved. I like the way you test that the lazy loading shows up in this version, it's much more readable and we are ensuring our base layout contains the element.

Thanks for working on it!

Base automatically changed from image-card-test to main August 13, 2025 12:52
@sirbully sirbully merged commit be4dc0f into main Aug 13, 2025
3 checks passed
@sirbully sirbully deleted the router-test branch August 13, 2025 12:52
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.

Unit tests for Router

3 participants