This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(patients): added live search to the patients search (#1970)
- Loading branch information
Showing
4 changed files
with
102 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks' | ||
import useDebounce from 'hooks/debounce' | ||
|
||
describe('useDebounce', () => { | ||
beforeAll(() => jest.useFakeTimers()) | ||
|
||
afterAll(() => jest.useRealTimers()) | ||
|
||
it('should set the next value after the input value has not changed for the specified amount of time', () => { | ||
const initialValue = 'initialValue' | ||
const expectedValue = 'someValue' | ||
const debounceDelay = 500 | ||
|
||
let currentValue = initialValue | ||
|
||
const { rerender, result } = renderHook(() => useDebounce(currentValue, debounceDelay)) | ||
|
||
currentValue = expectedValue | ||
|
||
act(() => { | ||
rerender() | ||
jest.advanceTimersByTime(debounceDelay) | ||
}) | ||
|
||
expect(result.current).toBe(expectedValue) | ||
}) | ||
|
||
it('should not set a new value before the specified delay has elapsed', () => { | ||
const initialValue = 'initialValue' | ||
const nextValue = 'someValue' | ||
const debounceDelay = 500 | ||
|
||
let currentValue = initialValue | ||
|
||
const { rerender, result } = renderHook(() => useDebounce(currentValue, debounceDelay)) | ||
|
||
currentValue = nextValue | ||
|
||
act(() => { | ||
rerender() | ||
jest.advanceTimersByTime(debounceDelay - 1) | ||
}) | ||
|
||
expect(result.current).toBe(initialValue) | ||
}) | ||
}) |
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 { useState, useEffect } from 'react' | ||
|
||
export default function <T>(value: T, delayInMilliseconds: number): T { | ||
const [debouncedValue, setDebouncedValue] = useState(value) | ||
|
||
useEffect(() => { | ||
const debounceHandler = setTimeout(() => setDebouncedValue(value), delayInMilliseconds) | ||
|
||
return () => clearTimeout(debounceHandler) | ||
}, [value, delayInMilliseconds]) | ||
|
||
return debouncedValue | ||
} |
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
3ba741c
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.
Failed to assign a domain to your deployment due to the following error:
(Learn more or visit the non-aliased deployment)