-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,46 @@ | ||
import type { RenderHookResult } from '@testing-library/react'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { useUnmount } from './useUnmount'; | ||
|
||
describe('useUnmount', () => { | ||
it('Does not call the function on first render', () => { | ||
const fun = jest.fn(); | ||
renderUseUnmount(fun); | ||
expect(fun).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('Does not call the function on rerender', () => { | ||
const fun = jest.fn(); | ||
const { rerender } = renderUseUnmount(fun); | ||
rerender({ fun }); | ||
expect(fun).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('Calls the function on unmount', () => { | ||
const fun = jest.fn(); | ||
const { unmount } = renderUseUnmount(fun); | ||
unmount(); | ||
expect(fun).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('Calls the most recent function on unmount when it has rendered with different functions', () => { | ||
const firstFun = jest.fn(); | ||
const secondFun = jest.fn(); | ||
const { rerender, unmount } = renderUseUnmount(firstFun); | ||
rerender({ fun: secondFun }); | ||
unmount(); | ||
expect(firstFun).not.toHaveBeenCalled(); | ||
expect(secondFun).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
type RenderUseUnmountProps = { | ||
fun: () => void; | ||
}; | ||
|
||
function renderUseUnmount(fun: () => void): RenderHookResult<void, RenderUseUnmountProps> { | ||
const props: RenderUseUnmountProps = { fun }; | ||
return renderHook<void, RenderUseUnmountProps>((props) => useUnmount(props.fun), { | ||
initialProps: props, | ||
}); | ||
} |
This file contains 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,11 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
export function useUnmount(fun: () => void): void { | ||
const functionRef = useRef<() => void>(fun); | ||
|
||
useEffect(() => { | ||
functionRef.current = fun; | ||
}, [fun]); | ||
|
||
useEffect(() => () => functionRef.current(), []); | ||
} |