-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
test(ssr): Validate state synchronization between server and client in React 18 using Zustand (#903) #2088
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
Merged
Merged
test(ssr): Validate state synchronization between server and client in React 18 using Zustand (#903) #2088
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1cda729
test(ssr): Validate state synchronization between server and client i…
NaamuKim 73f4d6d
test(ssr): dynamically import hydrateRoot for React 18 and skip if ve…
NaamuKim 8f0bc40
test(ssr): implement conditional describe based on React version
NaamuKim 2d89e10
test(ssr): Refactor SSR code using skipIf and importActual
NaamuKim d2bfd2a
test(ssr): Refactor code using 'importActual' with a more specific ty…
NaamuKim d1b1060
test(ssr): remove async from the main test function
NaamuKim 24da89c
Merge branch 'main' into test/#903-add-spec-with-ssr
dai-shi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import React, { useEffect } from 'react' | ||
| import { act, screen } from '@testing-library/react' | ||
| import { renderToString } from 'react-dom/server' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { create } from 'zustand' | ||
|
|
||
| interface BearStoreState { | ||
| bears: number | ||
| } | ||
|
|
||
| interface BearStoreAction { | ||
| increasePopulation: () => void | ||
| } | ||
|
|
||
| const initialState = { bears: 0 } | ||
| const useBearStore = create<BearStoreState & BearStoreAction>((set) => ({ | ||
| ...initialState, | ||
| increasePopulation: () => set(({ bears }) => ({ bears: bears + 1 })), | ||
| })) | ||
|
|
||
| function Counter() { | ||
| const { bears, increasePopulation } = useBearStore( | ||
| ({ bears, increasePopulation }) => ({ | ||
| bears, | ||
| increasePopulation, | ||
| }) | ||
| ) | ||
|
|
||
| useEffect(() => { | ||
| increasePopulation() | ||
| }, [increasePopulation]) | ||
|
|
||
| return <div>bears: {bears}</div> | ||
| } | ||
|
|
||
| describe.skipIf(!React.version.startsWith('18'))( | ||
| 'ssr behavior with react 18', | ||
| () => { | ||
| it('should handle different states between server and client correctly', async () => { | ||
| const { hydrateRoot } = await vi.importActual< | ||
| typeof import('react-dom/client') | ||
| >('react-dom/client') | ||
|
|
||
| const markup = renderToString( | ||
| <React.Suspense fallback={<div>Loading...</div>}> | ||
| <Counter /> | ||
| </React.Suspense> | ||
| ) | ||
|
|
||
| const container = document.createElement('div') | ||
| document.body.appendChild(container) | ||
| container.innerHTML = markup | ||
|
|
||
| expect(container.textContent).toContain('bears: 0') | ||
|
|
||
| await act(async () => { | ||
| hydrateRoot( | ||
| container, | ||
| <React.Suspense fallback={<div>Loading...</div>}> | ||
| <Counter /> | ||
| </React.Suspense> | ||
| ) | ||
| }) | ||
|
|
||
| const bearCountText = await screen.findByText('bears: 1') | ||
| expect(bearCountText).not.toBeNull() | ||
| document.body.removeChild(container) | ||
| }) | ||
| } | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Oh, I didn't know about
skipIf!We should use it in
jotaitoo:https://github.com/pmndrs/jotai/blob/d6afb777705eb3a90385817fd6db30db101f6caa/tests/react/transition.test.tsx#L12C1-L16
Free free to open a PR there.
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 am also glad to have learned about skipIf, and I will apply refactoring accordingly in Jotai as well
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 guess we are good to merge the changes :D. BTW, good job.
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.
Thank you so much for your review.
I'm delighted to learn about the useful functions of vitest. I'll continue to check if there are any opportunities for further contributions.