Skip to content

Commit 39bac29

Browse files
committed
fix: add unit test cases, adjust useMemo function
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
1 parent baef896 commit 39bac29

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

src/components/mui/__tests__/search-input.test.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
* */
1313

1414
import React from "react";
15-
import { render, screen } from "@testing-library/react";
15+
import { render, screen, act } from "@testing-library/react";
1616
import userEvent from "@testing-library/user-event";
1717
import "@testing-library/jest-dom";
1818
import SearchInput from "../search-input";
19+
import { DEBOUNCE_WAIT } from "../../../utils/constants";
1920

2021
describe("SearchInput", () => {
2122
test("renders with custom placeholder", () => {
@@ -58,4 +59,39 @@ describe("SearchInput", () => {
5859
rerender(<SearchInput term="new" onSearch={jest.fn()} />);
5960
expect(screen.getByDisplayValue("new")).toBeInTheDocument();
6061
});
62+
63+
describe("debounced prop", () => {
64+
beforeEach(() => jest.useFakeTimers());
65+
afterEach(() => jest.useRealTimers());
66+
67+
test("calls onSearch after debounce delay when debounced is true", async () => {
68+
const onSearch = jest.fn();
69+
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
70+
render(<SearchInput term="" onSearch={onSearch} debounced />);
71+
const input = screen.getByPlaceholderText("Search...");
72+
await user.type(input, "something");
73+
expect(onSearch).not.toHaveBeenCalled();
74+
act(() => jest.advanceTimersByTime(DEBOUNCE_WAIT));
75+
expect(onSearch).toHaveBeenCalledWith("something");
76+
});
77+
78+
test("does not call onSearch on Enter when debounced is true", async () => {
79+
const onSearch = jest.fn();
80+
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
81+
render(<SearchInput term="" onSearch={onSearch} debounced />);
82+
const input = screen.getByPlaceholderText("Search...");
83+
await user.type(input, "something{Enter}");
84+
expect(onSearch).not.toHaveBeenCalled();
85+
});
86+
87+
test("does not call onSearch on typing without Enter when not debounced", async () => {
88+
const onSearch = jest.fn();
89+
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
90+
render(<SearchInput term="" onSearch={onSearch} />);
91+
const input = screen.getByPlaceholderText("Search...");
92+
await user.type(input, "something");
93+
act(() => jest.runAllTimers());
94+
expect(onSearch).not.toHaveBeenCalled();
95+
});
96+
});
6197
});

src/components/mui/search-input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const SearchInput = ({ term, onSearch, placeholder = "Search...", debounced }) =
3131
};
3232

3333
const onSearchDebounced = useMemo(
34-
debounced ? debounce((value) => onSearch(value), DEBOUNCE_WAIT) : null,
34+
() => debounced ? debounce((value) => onSearch(value), DEBOUNCE_WAIT) : null,
3535
[onSearch, debounced]
3636
);
3737

0 commit comments

Comments
 (0)