-
Notifications
You must be signed in to change notification settings - Fork 232
Pr/cleanup renderhooks #106
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
afterEach(require('./src').cleanup) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import React, { Suspense } from 'react' | ||
import { act, create } from 'react-test-renderer' | ||
|
||
const unmounts = [] | ||
|
||
function TestHook({ callback, hookProps, onError, children }) { | ||
try { | ||
children(callback(hookProps)) | ||
|
@@ -51,6 +53,11 @@ function resultContainer() { | |
} | ||
} | ||
|
||
function cleanup() { | ||
unmounts.forEach((unmount) => unmount()) | ||
unmounts.length = 0 | ||
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. is there significant difference between this and 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. I was thinking to use either mutable or immutable approach. Personally, i prefer the immutable way as i suggested in the issue.
but i saw the way that you are using the
and i tried to follow the same pattern for consistency (mutable). I don't have a strong opinion on this, it's up to you. |
||
} | ||
|
||
function renderHook(callback, { initialProps, wrapper } = {}) { | ||
const { result, setValue, setError, addResolver } = resultContainer() | ||
const hookProps = { current: initialProps } | ||
|
@@ -73,6 +80,14 @@ function renderHook(callback, { initialProps, wrapper } = {}) { | |
}) | ||
const { unmount, update } = testRenderer | ||
|
||
function unmountHook() { | ||
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. what happens if someone calls this then then 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. I see there is a test around this case. I personally feel that having 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. I agree with you, i will fix it. |
||
act(() => { | ||
unmount() | ||
}) | ||
} | ||
|
||
unmounts.push(unmountHook) | ||
|
||
return { | ||
result, | ||
waitForNextUpdate: () => new Promise((resolve) => addResolver(resolve)), | ||
|
@@ -82,12 +97,8 @@ function renderHook(callback, { initialProps, wrapper } = {}) { | |
update(toRender()) | ||
}) | ||
}, | ||
unmount: () => { | ||
act(() => { | ||
unmount() | ||
}) | ||
} | ||
unmount: unmountHook | ||
} | ||
} | ||
|
||
export { renderHook, act } | ||
export { renderHook, cleanup, act } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useEffect } from 'react' | ||
import { renderHook, cleanup } from 'src' | ||
|
||
describe('cleanup tests', () => { | ||
let sideEffect = {} | ||
|
||
function useEffectsCounter({ initialProps }) { | ||
return renderHook( | ||
({ id }) => { | ||
useEffect(() => { | ||
sideEffect[id] = 0 | ||
return () => { | ||
sideEffect[id] = sideEffect[id] + 1 | ||
} | ||
}, [id]) | ||
}, | ||
{ initialProps } | ||
) | ||
} | ||
|
||
afterEach(() => (sideEffect = {})) | ||
|
||
test('should unmount the tests', () => { | ||
useEffectsCounter({ initialProps: { id: 1 } }) | ||
useEffectsCounter({ initialProps: { id: 10 } }) | ||
useEffectsCounter({ initialProps: { id: 100 } }) | ||
|
||
cleanup() | ||
|
||
expect(sideEffect).toEqual({ 1: 1, 10: 1, 100: 1 }) | ||
}) | ||
|
||
test('should not cleanup a hook that have already unmounted', () => { | ||
const { unmount } = useEffectsCounter({ initialProps: { id: 1 } }) | ||
|
||
unmount() | ||
cleanup() | ||
|
||
expect(sideEffect).toEqual({ 1: 1 }) | ||
}) | ||
|
||
test('should not unmount a hook that have already cleaned up', () => { | ||
const { unmount } = useEffectsCounter({ initialProps: { id: 1 } }) | ||
|
||
cleanup() | ||
unmount() | ||
|
||
expect(sideEffect).toEqual({ 1: 1 }) | ||
}) | ||
}) |
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.
I think this should be requireing
./lib
so that it picks up the compiled version after publishing.