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 { SearchBarView, DEFAULT_SEARCH_DEBOUNCE } from "./search-bar-view";
9 import * as Adapter from 'enzyme-adapter-react-16';
12 configure({ adapter: new Adapter() });
14 describe("<SearchBarView />", () => {
18 let onSearch: () => void;
24 describe("on input value change", () => {
25 it("calls onSearch after default timeout", () => {
26 const searchBar = mount(<SearchBarView onSearch={onSearch} value="current value" {...mockSearchProps()} />);
27 searchBar.find("input").simulate("change", { target: { value: "current value" } });
28 expect(onSearch).not.toBeCalled();
29 jest.runTimersToTime(DEFAULT_SEARCH_DEBOUNCE);
30 expect(onSearch).toBeCalledWith("current value");
33 it("calls onSearch after the time specified in props has passed", () => {
34 const searchBar = mount(<SearchBarView onSearch={onSearch} value="current value" debounce={2000} {...mockSearchProps()} />);
35 searchBar.find("input").simulate("change", { target: { value: "current value" } });
36 jest.runTimersToTime(1000);
37 expect(onSearch).not.toBeCalled();
38 jest.runTimersToTime(1000);
39 expect(onSearch).toBeCalledWith("current value");
42 it("calls onSearch only once after no change happened during the specified time", () => {
43 const searchBar = mount(<SearchBarView onSearch={onSearch} value="current value" debounce={1000} {...mockSearchProps()} />);
44 searchBar.find("input").simulate("change", { target: { value: "current value" } });
45 jest.runTimersToTime(500);
46 searchBar.find("input").simulate("change", { target: { value: "changed value" } });
47 jest.runTimersToTime(1000);
48 expect(onSearch).toHaveBeenCalledTimes(1);
51 it("calls onSearch again after the specified time has passed since previous call", () => {
52 const searchBar = mount(<SearchBarView onSearch={onSearch} value="latest value" debounce={1000} {...mockSearchProps()} />);
53 searchBar.find("input").simulate("change", { target: { value: "current value" } });
54 jest.runTimersToTime(500);
55 searchBar.find("input").simulate("change", { target: { value: "intermediate value" } });
56 jest.runTimersToTime(1000);
57 expect(onSearch).toBeCalledWith("intermediate value");
58 searchBar.find("input").simulate("change", { target: { value: "latest value" } });
59 jest.runTimersToTime(1000);
60 expect(onSearch).toBeCalledWith("latest value");
61 expect(onSearch).toHaveBeenCalledTimes(2);
67 const mockSearchProps = () => ({
74 saveRecentQuery: jest.fn(),
75 loadRecentQueries: () => ['test'],
77 deleteSavedQuery: jest.fn(),
78 openSearchView: jest.fn(),
79 editSavedQuery: jest.fn(),
80 navigateTo: jest.fn(),
81 searchDataOnEnter: jest.fn()