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';
11 configure({ adapter: new Adapter() });
13 describe("<SearchBar />", () => {
17 let onSearch: () => void;
23 describe("on submit", () => {
24 it("calls onSearch with initial value passed via props", () => {
25 const searchBar = mount(<SearchBarView value="initial value" onSearch={onSearch} currentView='' open={true} onSetView={jest.fn()} openView={jest.fn()} closeView={jest.fn()} />);
26 searchBar.find("form").simulate("submit");
27 expect(onSearch).toBeCalledWith("initial value");
30 it("calls onSearch with current value", () => {
31 const searchBar = mount(<SearchBarView value="" onSearch={onSearch} currentView='' open={true} onSetView={jest.fn()} openView={jest.fn()} closeView={jest.fn()} />);
32 searchBar.find("input").simulate("change", { target: { value: "current value" } });
33 searchBar.find("form").simulate("submit");
34 expect(onSearch).toBeCalledWith("current value");
37 it("calls onSearch with new value passed via props", () => {
38 const searchBar = mount(<SearchBarView value="" onSearch={onSearch} currentView='' open={true} onSetView={jest.fn()} openView={jest.fn()} closeView={jest.fn()} />);
39 searchBar.find("input").simulate("change", { target: { value: "current value" } });
40 searchBar.setProps({ value: "new value" });
41 searchBar.find("form").simulate("submit");
42 expect(onSearch).toBeCalledWith("new value");
45 it("cancels timeout set on input value change", () => {
46 const searchBar = mount(<SearchBarView value="" onSearch={onSearch} debounce={1000} currentView='' open={true} onSetView={jest.fn()} openView={jest.fn()} closeView={jest.fn()} />);
47 searchBar.find("input").simulate("change", { target: { value: "current value" } });
48 searchBar.find("form").simulate("submit");
49 jest.advanceTimersByTime(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 searchBar = mount(<SearchBarView value="" onSearch={onSearch} currentView='' open={true} onSetView={jest.fn()} openView={jest.fn()} closeView={jest.fn()} />);
59 searchBar.find("input").simulate("change", { target: { value: "current value" } });
60 expect(onSearch).not.toBeCalled();
61 jest.advanceTimersByTime(DEFAULT_SEARCH_DEBOUNCE);
62 expect(onSearch).toBeCalledWith("current value");
65 it("calls onSearch after the time specified in props has passed", () => {
66 const searchBar = mount(<SearchBarView value="" onSearch={onSearch} debounce={2000} currentView='' open={true} onSetView={jest.fn()} openView={jest.fn()} closeView={jest.fn()} />);
67 searchBar.find("input").simulate("change", { target: { value: "current value" } });
68 jest.advanceTimersByTime(1000);
69 expect(onSearch).not.toBeCalled();
70 jest.advanceTimersByTime(1000);
71 expect(onSearch).toBeCalledWith("current value");
74 it("calls onSearch only once after no change happened during the specified time", () => {
75 const searchBar = mount(<SearchBarView value="" onSearch={onSearch} debounce={1000} currentView='' open={true} onSetView={jest.fn()} openView={jest.fn()} closeView={jest.fn()} />);
76 searchBar.find("input").simulate("change", { target: { value: "current value" } });
77 jest.advanceTimersByTime(500);
78 searchBar.find("input").simulate("change", { target: { value: "changed value" } });
79 jest.advanceTimersByTime(1000);
80 expect(onSearch).toHaveBeenCalledTimes(1);
83 it("calls onSearch again after the specified time has passed since previous call", () => {
84 const searchBar = mount(<SearchBarView value="" onSearch={onSearch} debounce={1000} currentView='' open={true} onSetView={jest.fn()} openView={jest.fn()} closeView={jest.fn()} />);
85 searchBar.find("input").simulate("change", { target: { value: "current value" } });
86 jest.advanceTimersByTime(500);
87 searchBar.find("input").simulate("change", { target: { value: "intermediate value" } });
88 jest.advanceTimersByTime(1000);
89 expect(onSearch).toBeCalledWith("intermediate value");
90 searchBar.find("input").simulate("change", { target: { value: "latest value" } });
91 jest.advanceTimersByTime(1000);
92 expect(onSearch).toBeCalledWith("latest value");
93 expect(onSearch).toHaveBeenCalledTimes(2);