Skip to content
Myr

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 file

Take 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 back
1import 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}33

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

lines 34–39why, one step back
34describe('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 back
40

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

lines 41–55why, one step back
41 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 back
56 <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 back
60 })

chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f

line 61why, one step back
61

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

lines 62–68why, one step back
62 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 back
69

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

lines 70–73why, one step back
70 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 back
74

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

lines 75–78why, one step back
75 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 back
79

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

lines 80–84why, one step back
80 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 back
85

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

lines 86–88why, one step back
86 const container = document.createElement('div')
87 document.body.appendChild(container)88 container.innerHTML = view

chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f

line 89why, one step back
89

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

line 90why, one step back
90 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 back
91

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

lines 92–96why, one step back
92 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 back
97 <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 back
101 })

chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f

line 102why, one step back
102

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

lines 103–104why, one step back
103 // set state during hydration
104 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 back
105

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

line 106why, one step back
106 await hydratePromise

chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f

line 107why, one step back
107

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

line 108why, one step back
108 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 back
109

test(ssr): remove 'skipIf' since all CI React versions are 18+ (#3356)Wonsuk Choibefore adoption3e329e5

lines 110–113why, one step back
110 expect(screen.getByText('bears: 1')).toBeInTheDocument()
111 document.body.removeChild(container)112 })113})