-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #761 from shgusgh12/main
[view] debounce, throttle 테스트 코드 작성
- Loading branch information
Showing
2 changed files
with
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { debounce } from "./debounce"; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe("debounce", () => { | ||
it("check debounce", () => { | ||
const mockFn = jest.fn(); | ||
const debouncedFunc = debounce(mockFn, 1000); | ||
|
||
debouncedFunc("test1"); | ||
debouncedFunc("test2"); | ||
|
||
expect(mockFn).not.toBeCalled(); | ||
|
||
jest.advanceTimersByTime(999); | ||
expect(mockFn).not.toBeCalled(); | ||
|
||
jest.advanceTimersByTime(1); | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
expect(mockFn).toHaveBeenCalledWith("test2"); | ||
}); | ||
|
||
it("check default delay", () => { | ||
const mockFn = jest.fn(); | ||
const debouncedFunc = debounce(mockFn); | ||
|
||
debouncedFunc("test"); | ||
|
||
jest.advanceTimersByTime(999); | ||
expect(mockFn).not.toBeCalled(); | ||
|
||
jest.advanceTimersByTime(1); | ||
expect(mockFn).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,41 @@ | ||
import { throttle } from "./throttle"; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe("throttle", () => { | ||
it("check throttle", () => { | ||
const mockFn = jest.fn(); | ||
const throttledFunc = throttle(mockFn, 1000); | ||
|
||
throttledFunc(); | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
throttledFunc(); | ||
throttledFunc(); | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
jest.advanceTimersByTime(999); | ||
throttledFunc(); | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
jest.advanceTimersByTime(1); | ||
throttledFunc(); | ||
expect(mockFn).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("check default delay", () => { | ||
const mockFn = jest.fn(); | ||
const throttledFunc = throttle(mockFn); | ||
|
||
throttledFunc(); | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
throttledFunc(); | ||
jest.advanceTimersByTime(999); | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
jest.advanceTimersByTime(1); | ||
throttledFunc(); | ||
expect(mockFn).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |