Skip to content

feat: add test to reproduce #605 #1

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"docz-theme-default": "1.2.0",
"docz-utils": "2.3.0",
"eslint": "7.24.0",
"eslint-plugin-import": "^2.26.0",
"kcd-scripts": "9.1.0",
"prettier": "^2.2.1",
"react": "17.0.2",
Expand Down
22 changes: 22 additions & 0 deletions src/server/__tests__/ssr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @jest-environment node
*/
import {useState} from 'react'
import {renderHook, act} from '..'

// This verifies that renderHook can be called in
// a SSR-like environment.
describe('renderHook', () => {
function useLoading() {
if (typeof window !== 'undefined') {
const [loading, setLoading] = useState(false)
return {loading, setLoading}
}
}

test('should not throw in SSR environment', () => {
expect(() => renderHook(() => useLoading())).not.toThrowError(
'document is not defined',
)
})
})
25 changes: 13 additions & 12 deletions src/server/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ReactDOMServer from 'react-dom/server'
import ReactDOM from 'react-dom'
import { act } from 'react-dom/test-utils'

import { RendererProps, RendererOptions } from '../types/react'
import { RendererOptions, RendererProps } from '../types/react'

import { createRenderHook } from '../core'
import { createTestHarness } from '../helpers/createTestHarness'
Expand All @@ -12,44 +12,45 @@ function createServerRenderer<TProps, TResult>(
{ wrapper }: RendererOptions<TProps>
) {
let renderProps: TProps | undefined
let hydrated = false
const container = document.createElement('div')
let container: HTMLDivElement | undefined;
let serverOutput = ''
const testHarness = createTestHarness(rendererProps, wrapper, false)

return {
render(props?: TProps) {
renderProps = props
act(() => {
try {
const serverOutput = ReactDOMServer.renderToString(testHarness(props))
container.innerHTML = serverOutput
serverOutput = ReactDOMServer.renderToString(testHarness(props))
} catch (e: unknown) {
rendererProps.setError(e as Error)
}
})
},
hydrate() {
if (hydrated) {
if (container) {
throw new Error('The component can only be hydrated once')
} else {
container = document.createElement('div')
act(() => {
ReactDOM.hydrate(testHarness(renderProps), container)
ReactDOM.hydrate(testHarness(renderProps), container || null)
Copy link
Owner Author

Choose a reason for hiding this comment

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

✍🏼 If container is undefined, then we pass null to hydrate because undefined is not a valid container, but null is

})
hydrated = true
}
},
rerender(props?: TProps) {
if (!hydrated) {
if (!container) {
throw new Error('You must hydrate the component before you can rerender')
}
act(() => {
ReactDOM.render(testHarness(props), container)
ReactDOM.render(testHarness(props), container || null)
})
},
unmount() {
if (hydrated) {
if (container) {
act(() => {
ReactDOM.unmountComponentAtNode(container)
if (typeof container !== 'undefined') {
Copy link
Owner Author

Choose a reason for hiding this comment

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

✍🏼 Because act returns a function, it introduces a new scope which means we need to check again that container is not undefined before using it. This means we could remove the outer if (container) { check if we're okay that unmount always returns act}

ReactDOM.unmountComponentAtNode(container)
}
})
}
},
Expand Down
Loading