merge master
[arvados-workbench2.git] / src / views-components / search-bar / search-bar-view.test.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from "react";
6 import { mount, configure } from "enzyme";
7 import { SearchBarView, DEFAULT_SEARCH_DEBOUNCE } from "./search-bar-view";
8
9 import * as Adapter from 'enzyme-adapter-react-16';
10
11
12 configure({ adapter: new Adapter() });
13
14 describe("<SearchBar />", () => {
15
16     jest.useFakeTimers();
17
18     let onSearch: () => void;
19
20     beforeEach(() => {
21         onSearch = jest.fn();
22     });
23
24     describe("on submit", () => {
25         it("calls onSearch with initial value passed via props", () => {
26             const searchBar = mount(<SearchBarView {...mockSearchProps()} />);
27             searchBar.find("form").simulate("submit");
28             expect(onSearch).toBeCalledWith("initial value");
29         });
30
31         it("calls onSearch with current value", () => {
32             const searchBar = mount(<SearchBarView {...mockSearchProps()} />);
33             searchBar.find("input").simulate("change", { target: { value: "current value" } });
34             searchBar.find("form").simulate("submit");
35             expect(onSearch).toBeCalledWith("current value");
36         });
37
38         it("calls onSearch with new value passed via props", () => {
39             const searchBar = mount(<SearchBarView {...mockSearchProps()} />);
40             searchBar.find("input").simulate("change", { target: { value: "current value" } });
41             searchBar.setProps({ value: "new value" });
42             searchBar.find("form").simulate("submit");
43             expect(onSearch).toBeCalledWith("new value");
44         });
45
46         it("cancels timeout set on input value change", () => {
47             const searchBar = mount(<SearchBarView {...mockSearchProps()} />);
48             searchBar.find("input").simulate("change", { target: { value: "current value" } });
49             searchBar.find("form").simulate("submit");
50             jest.runTimersToTime(1000);
51             expect(onSearch).toHaveBeenCalledTimes(1);
52             expect(onSearch).toBeCalledWith("current value");
53         });
54
55     });
56
57     describe("on input value change", () => {
58         it("calls onSearch after default timeout", () => {
59             const searchBar = mount(<SearchBarView {...mockSearchProps()} />);
60             searchBar.find("input").simulate("change", { target: { value: "current value" } });
61             expect(onSearch).not.toBeCalled();
62             jest.runTimersToTime(DEFAULT_SEARCH_DEBOUNCE);
63             expect(onSearch).toBeCalledWith("current value");
64         });
65
66         it("calls onSearch after the time specified in props has passed", () => {
67             const searchBar = mount(<SearchBarView {...mockSearchProps()} />);
68             searchBar.find("input").simulate("change", { target: { value: "current value" } });
69             jest.runTimersToTime(1000);
70             expect(onSearch).not.toBeCalled();
71             jest.runTimersToTime(1000);
72             expect(onSearch).toBeCalledWith("current value");
73         });
74
75         it("calls onSearch only once after no change happened during the specified time", () => {
76             const searchBar = mount(<SearchBarView {...mockSearchProps()} />);
77             searchBar.find("input").simulate("change", { target: { value: "current value" } });
78             jest.runTimersToTime(500);
79             searchBar.find("input").simulate("change", { target: { value: "changed value" } });
80             jest.runTimersToTime(1000);
81             expect(onSearch).toHaveBeenCalledTimes(1);
82         });
83
84         it("calls onSearch again after the specified time has passed since previous call", () => {
85             const searchBar = mount(<SearchBarView {...mockSearchProps()} />);
86             searchBar.find("input").simulate("change", { target: { value: "current value" } });
87             jest.runTimersToTime(500);
88             searchBar.find("input").simulate("change", { target: { value: "intermediate value" } });
89             jest.runTimersToTime(1000);
90             expect(onSearch).toBeCalledWith("intermediate value");
91             searchBar.find("input").simulate("change", { target: { value: "latest value" } });
92             jest.runTimersToTime(1000);
93             expect(onSearch).toBeCalledWith("latest value");
94             expect(onSearch).toHaveBeenCalledTimes(2);
95
96         });
97     });
98 });
99
100 const mockSearchProps = () => ({
101     value: "current value",
102     onSearch: jest.fn(),
103     debounce: 1000,
104     currentView: '',
105     open: true,
106     onSetView: jest.fn(),
107     openView: jest.fn(),
108     loseView: jest.fn(),
109     closeView: jest.fn(),
110     saveQuery: jest.fn(),
111     loadQueries: () => ['test']
112 });