Extract permission types, create model and service
[arvados.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("<SearchBarView />", () => {
15
16     jest.useFakeTimers();
17
18     let onSearch: () => void;
19
20     beforeEach(() => {
21         onSearch = jest.fn();
22     });
23
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");
31         });
32
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");
40         });
41
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);
49         });
50
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);
62
63         });
64     });
65 });
66
67 const mockSearchProps = () => ({
68     currentView: '',
69     open: true,
70     onSetView: jest.fn(),
71     openView: jest.fn(),
72     loseView: jest.fn(),
73     closeView: jest.fn(),
74     saveRecentQuery: jest.fn(),
75     loadRecentQueries: () => ['test'],
76     saveQuery: jest.fn(),
77     deleteSavedQuery: jest.fn(),
78     openSearchView: jest.fn(),
79     editSavedQuery: jest.fn(),
80     navigateTo: jest.fn(),
81     searchDataOnEnter: jest.fn()
82 });