1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
6 import { mount, configure } from "enzyme";
7 import { SearchInput, DEFAULT_SEARCH_DEBOUNCE } from "./search-input";
9 import * as Adapter from 'enzyme-adapter-react-16';
11 configure({ adapter: new Adapter() });
13 describe("<SearchInput />", () => {
17 let onSearch: () => void;
23 describe("on submit", () => {
24 it("calls onSearch with initial value passed via props", () => {
25 const searchInput = mount(<SearchInput value="initial value" onSearch={onSearch} />);
26 searchInput.find("form").simulate("submit");
27 expect(onSearch).toBeCalledWith("initial value");
30 it("calls onSearch with current value", () => {
31 const searchInput = mount(<SearchInput value="" onSearch={onSearch} />);
32 searchInput.find("input").simulate("change", { target: { value: "current value" } });
33 searchInput.find("form").simulate("submit");
34 expect(onSearch).toBeCalledWith("current value");
37 it("calls onSearch with new value passed via props", () => {
38 const searchInput = mount(<SearchInput value="" onSearch={onSearch} />);
39 searchInput.find("input").simulate("change", { target: { value: "current value" } });
40 searchInput.setProps({value: "new value"});
41 searchInput.find("form").simulate("submit");
42 expect(onSearch).toBeCalledWith("new value");
45 it("cancels timeout set on input value change", () => {
46 const searchInput = mount(<SearchInput value="" onSearch={onSearch} debounce={1000} />);
47 searchInput.find("input").simulate("change", { target: { value: "current value" } });
48 searchInput.find("form").simulate("submit");
49 jest.runTimersToTime(1000);
50 expect(onSearch).toHaveBeenCalledTimes(1);
51 expect(onSearch).toBeCalledWith("current value");
56 describe("on input value change", () => {
57 it("calls onSearch after default timeout", () => {
58 const searchInput = mount(<SearchInput value="" onSearch={onSearch} />);
59 searchInput.find("input").simulate("change", { target: { value: "current value" } });
60 expect(onSearch).not.toBeCalled();
61 jest.runTimersToTime(DEFAULT_SEARCH_DEBOUNCE);
62 expect(onSearch).toBeCalledWith("current value");
65 it("calls onSearch after the time specified in props has passed", () => {
66 const searchInput = mount(<SearchInput value="" onSearch={onSearch} debounce={2000}/>);
67 searchInput.find("input").simulate("change", { target: { value: "current value" } });
68 jest.runTimersToTime(1000);
69 expect(onSearch).not.toBeCalled();
70 jest.runTimersToTime(1000);
71 expect(onSearch).toBeCalledWith("current value");
74 it("calls onSearch only once after no change happened during the specified time", () => {
75 const searchInput = mount(<SearchInput value="" onSearch={onSearch} debounce={1000}/>);
76 searchInput.find("input").simulate("change", { target: { value: "current value" } });
77 jest.runTimersToTime(500);
78 searchInput.find("input").simulate("change", { target: { value: "changed value" } });
79 jest.runTimersToTime(1000);
80 expect(onSearch).toHaveBeenCalledTimes(1);
83 it("calls onSearch again after the specified time has passed since previous call", () => {
84 const searchInput = mount(<SearchInput value="" onSearch={onSearch} debounce={1000}/>);
85 searchInput.find("input").simulate("change", { target: { value: "current value" } });
86 jest.runTimersToTime(500);
87 searchInput.find("input").simulate("change", { target: { value: "intermediate value" } });
88 jest.runTimersToTime(1000);
89 expect(onSearch).toBeCalledWith("intermediate value");
90 searchInput.find("input").simulate("change", { target: { value: "latest value" } });
91 jest.runTimersToTime(1000);
92 expect(onSearch).toBeCalledWith("latest value");
93 expect(onSearch).toHaveBeenCalledTimes(2);