-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
useToggle
hook (#220)
- Loading branch information
Showing
7 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
export { useToggle } from './useToggle'; |
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,52 @@ | ||
import { act, cleanup, renderHook } from '@testing-library/react-hooks'; | ||
|
||
import { useToggle } from '../useToggle'; | ||
|
||
describe('useToggle hook', () => { | ||
afterEach(cleanup); | ||
|
||
it('should return boolean and a function inside the Array', () => { | ||
const { result } = renderHook(() => useToggle()); | ||
const [value, toggle] = result.current; | ||
|
||
expect(typeof value).toBe('boolean'); | ||
expect(typeof toggle).toBe('function'); | ||
}); | ||
|
||
it('should toggle the value', () => { | ||
const { result } = renderHook(() => useToggle()); | ||
const [, toggle] = result.current; | ||
|
||
expect(result.current[0]).toBe(false); | ||
|
||
act(() => { | ||
toggle(); | ||
}); | ||
expect(result.current[0]).toBe(true); | ||
|
||
act(() => { | ||
toggle(); | ||
}); | ||
expect(result.current[0]).toBe(false); | ||
}); | ||
|
||
it('should set the boolean passed as argument', () => { | ||
const { result } = renderHook(() => useToggle()); | ||
const [, toggle] = result.current; | ||
|
||
act(() => { | ||
toggle(true); | ||
}); | ||
expect(result.current[0]).toBe(true); | ||
|
||
act(() => { | ||
toggle(true); | ||
}); | ||
expect(result.current[0]).toBe(true); | ||
|
||
act(() => { | ||
toggle(false); | ||
}); | ||
expect(result.current[0]).toBe(false); | ||
}); | ||
}); |
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,15 @@ | ||
import { useReducer } from 'react'; | ||
|
||
export type DispatchWithOptionalAction<Type> = (_arg?: Type | unknown) => void; | ||
|
||
export type UseToggleType<Type> = [Type, DispatchWithOptionalAction<Type>]; | ||
|
||
function toggle(currentValue: boolean, newValue: boolean | undefined) { | ||
return typeof newValue === 'boolean' ? newValue : !currentValue; | ||
} | ||
|
||
function useToggle(initialValue = false): UseToggleType<boolean> { | ||
return useReducer(toggle, initialValue); | ||
} | ||
|
||
export { useToggle }; |
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,3 @@ | ||
export { useControlledState } from './useControlledState'; | ||
export { useGeneratedId } from './useGeneratedId'; | ||
export { useIsEscKeyPressed } from './useIsEscKeyPressed'; |