Skip to content

Commit

Permalink
Merge pull request #761 from shgusgh12/main
Browse files Browse the repository at this point in the history
[view] debounce, throttle 테스트 코드 작성
  • Loading branch information
shgusgh12 authored Oct 10, 2024
2 parents 8f28b4e + 584623c commit 155907d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/view/src/utils/debounce.test.ts
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);
});
});
41 changes: 41 additions & 0 deletions packages/view/src/utils/throttle.test.ts
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);
});
});

0 comments on commit 155907d

Please sign in to comment.