ssr.test.tsx
This file
Path
tests/ssr.test.tsx
113 lines · 2.9 KiB · TypeScript
Last changed by
test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
History of this fileTake it
The walk stopped after 200 commits: lines older than f85898f point at it.
chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 1–33why, one step back1import React, { useEffect } from 'react'2import { act, screen } from '@testing-library/react'3import { renderToString } from 'react-dom/server'4import { describe, expect, it, vi } from 'vitest'5import { create } from 'zustand'67interface BearStoreState {8 bears: number9}1011interface BearStoreAction {12 increasePopulation: () => void13}1415const initialState = { bears: 0 }16const useBearStore = create<BearStoreState & BearStoreAction>((set) => ({17 ...initialState,18 increasePopulation: () => set(({ bears }) => ({ bears: bears + 1 })),19}))2021function Counter() {22 const bears = useBearStore(({ bears }) => bears)23 const increasePopulation = useBearStore(24 ({ increasePopulation }) => increasePopulation,25 )2627 useEffect(() => {28 increasePopulation()29 }, [increasePopulation])3031 return <div>bears: {bears}</div>32}33test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
lines 34–39why, one step back34describe('ssr behavior with react 18+', () => {35 it('should handle different states between server and client correctly', async () => {36 const { hydrateRoot } =37 await vi.importActual<typeof import('react-dom/client')>(38 'react-dom/client',39 )chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 40why, one step back40test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
lines 41–55why, one step back41 const view = renderToString(42 <React.Suspense fallback={<div>Loading...</div>}>43 <Counter />44 </React.Suspense>,45 )4647 const container = document.createElement('div')48 document.body.appendChild(container)49 container.innerHTML = view5051 expect(container).toHaveTextContent(/bears: 0/)5253 await act(async () => {54 hydrateRoot(55 container,chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 56–59why, one step back56 <React.Suspense fallback={<div>Loading...</div>}>57 <Counter />58 </React.Suspense>,59 )test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
line 60why, one step back60 })chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 61why, one step back61test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
lines 62–68why, one step back62 expect(screen.getByText('bears: 1')).toBeInTheDocument()63 document.body.removeChild(container)64 })65 it('should not have hydration errors', async () => {66 const useStore = create(() => ({67 bears: 0,68 }))chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 69why, one step back69test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
lines 70–73why, one step back70 const { hydrateRoot } =71 await vi.importActual<typeof import('react-dom/client')>(72 'react-dom/client',73 )chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 74why, one step back74test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
lines 75–78why, one step back75 const Component = () => {76 const bears = useStore((state) => state.bears)77 return <div>bears: {bears}</div>78 }chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 79why, one step back79test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
lines 80–84why, one step back80 const view = renderToString(81 <React.Suspense fallback={<div>Loading...</div>}>82 <Component />83 </React.Suspense>,84 )chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 85why, one step back85test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
lines 86–88why, one step back86 const container = document.createElement('div')87 document.body.appendChild(container)88 container.innerHTML = viewchore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 89why, one step back89test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
line 90why, one step back90 expect(container).toHaveTextContent(/bears: 0/)chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 91why, one step back91test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
lines 92–96why, one step back92 const consoleMock = vi.spyOn(console, 'error')9394 const hydratePromise = act(async () => {95 hydrateRoot(96 container,chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 97–100why, one step back97 <React.Suspense fallback={<div>Loading...</div>}>98 <Component />99 </React.Suspense>,100 )test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
line 101why, one step back101 })chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 102why, one step back102test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
lines 103–104why, one step back103 // set state during hydration104 useStore.setState({ bears: 1 })chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 105why, one step back105test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
line 106why, one step back106 await hydratePromisechore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 107why, one step back107test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
line 108why, one step back108 expect(consoleMock).toHaveBeenCalledTimes(0)chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 109why, one step back109test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5
lines 110–113why, one step back110 expect(screen.getByText('bears: 1')).toBeInTheDocument()111 document.body.removeChild(container)112 })113})