forked from testing-library/react-hooks-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
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
stephenkilbourn
merged 4 commits into
stephenkilbourn--fix-ssr-before
from
stephenkilbourn-fix-ssr-after
Nov 15, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
489496c
feat: add test to reproduce #605
stephenkilbourn ba21513
feat: add ssr test to repro #605
stephenkilbourn 5552c43
refactor(server/pure): create hydrate container on client
stephenkilbourn bc73113
refactor: improve type-safety in src/server/pure
stephenkilbourn 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
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,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', | ||
) | ||
}) | ||
}) |
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 |
---|---|---|
|
@@ -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' | ||
|
@@ -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) | ||
}) | ||
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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
}) | ||
} | ||
}, | ||
|
Oops, something went wrong.
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.
✍🏼 If container is undefined, then we pass null to hydrate because undefined is not a valid container, but null is