-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement useMountEffect and useUnmountEffect hooks
- Loading branch information
Showing
7 changed files
with
106 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,13 @@ | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* Run effect only when component is first mounted. | ||
* | ||
* @param fn effector to run on nmount | ||
*/ | ||
export function useMountEffect(fn: () => void): void { | ||
useEffect(() => { | ||
fn(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
} |
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,16 @@ | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* Run effect only when component is unmounted. | ||
* | ||
* @param fn effector to run on unmount | ||
*/ | ||
export function useUnmountEffect(fn: () => void): void { | ||
useEffect( | ||
() => () => { | ||
fn(); | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[] | ||
); | ||
} |
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,24 @@ | ||
import { renderHook } from '@testing-library/react-hooks/dom'; | ||
import { useMountEffect } from '../../src/useMountEffect'; | ||
|
||
describe('useMountEffect', () => { | ||
it('should call effector only on first render', () => { | ||
const spy = jest.fn(); | ||
|
||
const { result, rerender, unmount } = renderHook(() => useMountEffect(spy)); | ||
|
||
expect(result.current).toBe(undefined); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
|
||
rerender(); | ||
rerender(); | ||
rerender(); | ||
rerender(); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
|
||
unmount(); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,24 @@ | ||
import { renderHook } from '@testing-library/react-hooks/dom'; | ||
import { useUnmountEffect } from '../../src/useUnmountEffect'; | ||
|
||
describe('useUnmountEffect', () => { | ||
it('should call effector only when component unmounted', () => { | ||
const spy = jest.fn(); | ||
|
||
const { result, rerender, unmount } = renderHook(() => useUnmountEffect(spy)); | ||
|
||
expect(result.current).toBe(undefined); | ||
expect(spy).toHaveBeenCalledTimes(0); | ||
|
||
rerender(); | ||
rerender(); | ||
rerender(); | ||
rerender(); | ||
|
||
expect(spy).toHaveBeenCalledTimes(0); | ||
|
||
unmount(); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,13 @@ | ||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useMountEffect } from '../../src/useMountEffect'; | ||
|
||
describe('useMountEffect', () => { | ||
it('should call effector only on first render', () => { | ||
const spy = jest.fn(); | ||
|
||
const { result } = renderHook(() => useMountEffect(spy)); | ||
|
||
expect(result.current).toBe(undefined); | ||
expect(spy).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |
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,13 @@ | ||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useUnmountEffect } from '../../src/useUnmountEffect'; | ||
|
||
describe('useUnmountEffect', () => { | ||
it('should call effector only when component unmounted', () => { | ||
const spy = jest.fn(); | ||
|
||
const { result } = renderHook(() => useUnmountEffect(spy)); | ||
|
||
expect(result.current).toBe(undefined); | ||
expect(spy).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |