-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Debounce filter transporter search (#729)
* add 'skip' state * remove empty epaId querystring when selecting transporter
- Loading branch information
1 parent
4442655
commit e57bb47
Showing
4 changed files
with
89 additions
and
5 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
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,63 @@ | ||
import '@testing-library/jest-dom'; | ||
import { renderHook, act } from '@testing-library/react'; | ||
import { useDebounce } from './useDebounce'; | ||
import { beforeAll, afterAll, afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
describe('useDebounce hook', () => { | ||
beforeAll(() => { | ||
vi.useFakeTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
vi.clearAllTimers(); | ||
}); | ||
|
||
it('should return the initial value immediately', () => { | ||
const { result } = renderHook(() => useDebounce('initial', 500)); | ||
expect(result.current).toBe('initial'); | ||
}); | ||
|
||
it('should update the debounced value after the specified delay', () => { | ||
const { result, rerender } = renderHook(({ value, delay }) => useDebounce(value, delay), { | ||
initialProps: { value: 'initial', delay: 500 }, | ||
}); | ||
|
||
rerender({ value: 'updated', delay: 500 }); | ||
|
||
expect(result.current).toBe('initial'); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(500); | ||
}); | ||
|
||
expect(result.current).toBe('updated'); | ||
}); | ||
|
||
it('should reset the timer when value changes within the delay period', () => { | ||
const { result, rerender } = renderHook(({ value, delay }) => useDebounce(value, delay), { | ||
initialProps: { value: 'initial', delay: 500 }, | ||
}); | ||
|
||
rerender({ value: 'updated1', delay: 500 }); | ||
act(() => { | ||
vi.advanceTimersByTime(300); | ||
}); | ||
rerender({ value: 'updated2', delay: 500 }); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(200); | ||
}); | ||
|
||
expect(result.current).toBe('initial'); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(300); | ||
}); | ||
|
||
expect(result.current).toBe('updated2'); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllTimers(); | ||
}); | ||
}); |
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,17 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
export const useDebounce = (value: string, milliSeconds: number) => { | ||
const [debouncedValue, setDebouncedValue] = useState(value); | ||
|
||
useEffect(() => { | ||
const handler = setTimeout(() => { | ||
setDebouncedValue(value); | ||
}, milliSeconds); | ||
|
||
return () => { | ||
clearTimeout(handler); | ||
}; | ||
}, [value, milliSeconds]); | ||
|
||
return debouncedValue; | ||
}; |