Create search-bar tests
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Tue, 12 Jun 2018 12:57:19 +0000 (14:57 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Tue, 12 Jun 2018 12:57:19 +0000 (14:57 +0200)
Feature #13590

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/components/main-app-bar/search-bar/search-bar.test.tsx [new file with mode: 0644]
src/components/main-app-bar/search-bar/search-bar.tsx

diff --git a/src/components/main-app-bar/search-bar/search-bar.test.tsx b/src/components/main-app-bar/search-bar/search-bar.test.tsx
new file mode 100644 (file)
index 0000000..eaadd12
--- /dev/null
@@ -0,0 +1,99 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import * as React from "react";
+import { mount, configure } from "enzyme";
+import SearchBar, { DEFAULT_SEARCH_DEBOUNCE } from "./search-bar";
+
+import * as Adapter from 'enzyme-adapter-react-16';
+
+configure({ adapter: new Adapter() });
+
+describe("<SearchBar />", () => {
+
+    jest.useFakeTimers();
+
+    let onSearch: () => void;
+
+    beforeEach(() => {
+        onSearch = jest.fn();
+    });
+
+    describe("on submit", () => {
+        it("calls onSearch with initial value passed via props", () => {
+            const searchBar = mount(<SearchBar value="initial value" onSearch={onSearch} />);
+            searchBar.find("form").simulate("submit");
+            expect(onSearch).toBeCalledWith("initial value");
+        });
+
+        it("calls onSearch with current value", () => {
+            const searchBar = mount(<SearchBar value="" onSearch={onSearch} />);
+            searchBar.find("input").simulate("change", { target: { value: "current value" } });
+            searchBar.find("form").simulate("submit");
+            expect(onSearch).toBeCalledWith("current value");
+        });
+
+        it("calls onSearch with new value passed via props", () => {
+            const searchBar = mount(<SearchBar value="" onSearch={onSearch} />);
+            searchBar.find("input").simulate("change", { target: { value: "current value" } });
+            searchBar.setProps({value: "new value"});
+            searchBar.find("form").simulate("submit");
+            expect(onSearch).toBeCalledWith("new value");
+        });
+
+        it("cancels timeout set on input value change", () => {
+            const searchBar = mount(<SearchBar value="" onSearch={onSearch} debounce={1000} />);
+            searchBar.find("input").simulate("change", { target: { value: "current value" } });
+            searchBar.find("form").simulate("submit");
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).toHaveBeenCalledTimes(1);
+            expect(onSearch).toBeCalledWith("current value");
+        });
+
+    });
+
+    describe("on input value change", () => {
+        it("calls onSearch after default timeout", () => {
+            const searchBar = mount(<SearchBar value="" onSearch={onSearch} />);
+            searchBar.find("input").simulate("change", { target: { value: "current value" } });
+            expect(onSearch).not.toBeCalled();
+            jest.advanceTimersByTime(DEFAULT_SEARCH_DEBOUNCE);
+            expect(onSearch).toBeCalledWith("current value");
+        });
+        
+        it("calls onSearch after the time specified in props has passed", () => {
+            const searchBar = mount(<SearchBar value="" onSearch={onSearch} debounce={2000}/>);
+            searchBar.find("input").simulate("change", { target: { value: "current value" } });
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).not.toBeCalled();
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).toBeCalledWith("current value");
+        });
+        
+        it("calls onSearch only once after no change happened during the specified time", () => {
+            const searchBar = mount(<SearchBar value="" onSearch={onSearch} debounce={1000}/>);
+            searchBar.find("input").simulate("change", { target: { value: "current value" } });
+            jest.advanceTimersByTime(500);
+            searchBar.find("input").simulate("change", { target: { value: "changed value" } });
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).toHaveBeenCalledTimes(1);
+        });
+        
+        it("calls onSearch again after the specified time has passed since previous call", () => {
+            const searchBar = mount(<SearchBar value="" onSearch={onSearch} debounce={1000}/>);
+            searchBar.find("input").simulate("change", { target: { value: "current value" } });
+            jest.advanceTimersByTime(500);
+            searchBar.find("input").simulate("change", { target: { value: "intermediate value" } });
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).toBeCalledWith("intermediate value")
+            searchBar.find("input").simulate("change", { target: { value: "latest value" } });
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).toBeCalledWith("latest value")
+            expect(onSearch).toHaveBeenCalledTimes(2);
+            
+        });
+
+    });
+
+});
\ No newline at end of file
index d8c7edeeafa525c0edf0b4640945d1219a3fd314..944eb426dcaa0d12b889f9ef3fb49235b6951393 100644 (file)
@@ -21,7 +21,7 @@ interface SearchBarState {
     value: string;
 }
 
-const DEFAULT_SEARCH_DEBOUNCE = 1000;
+export const DEFAULT_SEARCH_DEBOUNCE = 1000;
 
 class SearchBar extends React.Component<SearchBarProps> {
 
@@ -48,6 +48,10 @@ class SearchBar extends React.Component<SearchBarProps> {
         </Paper>
     }
 
+    componentDidMount() {
+        this.setState({value: this.props.value});
+    }
+
     componentWillReceiveProps(nextProps: SearchBarProps) {
         if (nextProps.value !== this.props.value) {
             this.setState({ value: nextProps.value });