Skip to content
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

Support React 18 #655

Closed
Closed
Show file tree
Hide file tree
Changes from 11 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
4 changes: 4 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
strategy:
matrix:
node: [12.13, 12, 14, 16]
react: [^16.9.0, ^17.0.2, next]
mpeyper marked this conversation as resolved.
Show resolved Hide resolved
runs-on: ubuntu-latest
steps:
- name: 🛑 Cancel Previous Runs
Expand All @@ -37,6 +38,9 @@ jobs:
env:
HUSKY_SKIP_INSTALL: true

- name: Use React version
run: npm install --save-dev react@"${{ matrix.react }}" react-dom@"${{ matrix.react }}" react-test-renderer@"${{ matrix.react }}"

- name: ▶️ Run validate script
run: npm run validate

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"scripts": {
"setup": "npm install && npm run validate -s",
"validate": "kcd-scripts validate",
"prepare": "npm run build",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what else we'll need to do now that I've removed this because of https://docs.npmjs.com/cli/v7/commands/npm-install

If the package being installed contains a prepare script, its dependencies and devDependencies will be installed, and the prepare script will be run, before the package is packaged and installed.

"build": "kcd-scripts build --out-dir lib && npm run generate:submodules",
"generate:submodules": "ts-node scripts/generate-submodules.ts",
"test": "kcd-scripts test",
Expand All @@ -43,7 +42,9 @@
"coverage": "codecov",
"docs:dev": "docz dev",
"docs:build": "docz build",
"contributors:add": "all-contributors add"
"contributors:add": "all-contributors add",
"install-16": "npm install --save-dev react@\"^16.9.0\" react-dom@\"^16.9.0\" react-test-renderer@\"^16.9.0\"",
"install-next": "npm install --save-dev react@next react-dom@next react-test-renderer@next"
},
"dependencies": {
"@babel/runtime": "^7.12.5",
Expand All @@ -64,7 +65,7 @@
"kcd-scripts": "11.2.0",
"prettier": "^2.2.1",
"react": "17.0.2",
"react-dom": "^17.0.1",
"react-dom": "17.0.2",
"react-test-renderer": "17.0.2",
"ts-node": "^10.0.0",
"typescript": "4.3.5"
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/resultHistory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('result history tests', () => {
rerender(2)

expect(result.error).toEqual(Error('expected'))
// double error thrown for dom, but not default or native...
expect(result.all).toEqual([0, 1, Error('expected')])

rerender(3)
Expand Down
61 changes: 43 additions & 18 deletions src/dom/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,55 @@ import { RendererProps, RendererOptions } from '../types/react'
import { createRenderHook } from '../core'
import { createTestHarness } from '../helpers/createTestHarness'

// @ts-ignore
const isReactConcurrent = !!ReactDOM.createRoot;

function createDomRenderer<TProps, TResult>(
rendererProps: RendererProps<TProps, TResult>,
{ wrapper }: RendererOptions<TProps>
) {
const container = document.createElement('div')
const testHarness = createTestHarness(rendererProps, wrapper)

return {
render(props?: TProps) {
act(() => {
ReactDOM.render(testHarness(props), container)
})
},
rerender(props?: TProps) {
act(() => {
ReactDOM.render(testHarness(props), container)
})
},
unmount() {
act(() => {
ReactDOM.unmountComponentAtNode(container)
})
},
act
if (isReactConcurrent) {
// @ts-ignore
const root = ReactDOM.createRoot(container)
return {
render(props?: TProps) {
act(() => {
root.render(testHarness(props))
})
},
rerender(props?: TProps) {
act(() => {
root.render(testHarness(props))
})
},
unmount() {
act(() => {
root.unmount()
})
},
act
}
} else {
return {
render(props?: TProps) {
act(() => {
ReactDOM.render(testHarness(props), container)
})
},
rerender(props?: TProps) {
act(() => {
ReactDOM.render(testHarness(props), container)
})
},
unmount() {
act(() => {
ReactDOM.unmountComponentAtNode(container)
})
},
act
}
}
}

Expand Down
114 changes: 80 additions & 34 deletions src/server/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { RendererOptions, RendererProps } from '../types/react'
import { createRenderHook } from '../core'
import { createTestHarness } from '../helpers/createTestHarness'

// @ts-ignore
const isReactConcurrent = !!ReactDOM.createRoot;

function createServerRenderer<TProps, TResult>(
rendererProps: RendererProps<TProps, TResult>,
{ wrapper }: RendererOptions<TProps>
Expand All @@ -15,45 +18,88 @@ function createServerRenderer<TProps, TResult>(
let container: HTMLDivElement | undefined
let serverOutput: string = ''
const testHarness = createTestHarness(rendererProps, wrapper, false)

return {
render(props?: TProps) {
renderProps = props
act(() => {
try {
serverOutput = ReactDOMServer.renderToString(testHarness(props))
} catch (e: unknown) {
rendererProps.setError(e as Error)
if (isReactConcurrent) {
let root: any
return {
render(props?: TProps) {
renderProps = props
act(() => {
try {
serverOutput = ReactDOMServer.renderToString(testHarness(props))
} catch (e: unknown) {
rendererProps.setError(e as Error)
}
})
},
hydrate() {
if (container) {
throw new Error('The component can only be hydrated once')
} else {
container = document.createElement('div')
container.innerHTML = serverOutput
act(() => {
// @ts-ignore
root = ReactDOM.hydrateRoot(container, testHarness(renderProps))
})
}
},
rerender(props?: TProps) {
if (!container) {
throw new Error('You must hydrate the component before you can rerender')
}
})
},
hydrate() {
if (container) {
throw new Error('The component can only be hydrated once')
} else {
container = document.createElement('div')
container.innerHTML = serverOutput
act(() => {
ReactDOM.hydrate(testHarness(renderProps), container!)
root.render(testHarness(props))
})
}
},
rerender(props?: TProps) {
if (!container) {
throw new Error('You must hydrate the component before you can rerender')
}
act(() => {
ReactDOM.render(testHarness(props), container!)
})
},
unmount() {
if (container) {
},
unmount() {
if (container) {
act(() => {
root.unmount()
})
}
},
act
};
} else {
return {
render(props?: TProps) {
renderProps = props
act(() => {
ReactDOM.unmountComponentAtNode(container!)
try {
serverOutput = ReactDOMServer.renderToString(testHarness(props))
} catch (e: unknown) {
rendererProps.setError(e as Error)
}
})
}
},
act
},
hydrate() {
if (container) {
throw new Error('The component can only be hydrated once')
} else {
container = document.createElement('div')
container.innerHTML = serverOutput
act(() => {
ReactDOM.hydrate(testHarness(renderProps), container!)
})
}
},
rerender(props?: TProps) {
if (!container) {
throw new Error('You must hydrate the component before you can rerender')
}
act(() => {
ReactDOM.render(testHarness(props), container!)
})
},
unmount() {
if (container) {
act(() => {
ReactDOM.unmountComponentAtNode(container!)
})
}
},
act
};
}
}

Expand Down