[ARVADOS-WORKBENCH2] updated: 6ac1c00c725bab873d1f7e85eeffb917af1a9635

Git user git at public.curoverse.com
Tue Jun 12 08:57:39 EDT 2018


Summary of changes:
 .../main-app-bar/search-bar/search-bar.test.tsx    | 99 ++++++++++++++++++++++
 .../main-app-bar/search-bar/search-bar.tsx         |  6 +-
 2 files changed, 104 insertions(+), 1 deletion(-)
 create mode 100644 src/components/main-app-bar/search-bar/search-bar.test.tsx

       via  6ac1c00c725bab873d1f7e85eeffb917af1a9635 (commit)
      from  7edbf7ca5bdd726ac2e433a230cca6b2b0a6caa8 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit 6ac1c00c725bab873d1f7e85eeffb917af1a9635
Author: Michal Klobukowski <michal.klobukowski at contractors.roche.com>
Date:   Tue Jun 12 14:57:19 2018 +0200

    Create search-bar tests
    
    Feature #13590
    
    Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski at contractors.roche.com>

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
index 0000000..eaadd12
--- /dev/null
+++ b/src/components/main-app-bar/search-bar/search-bar.test.tsx
@@ -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
diff --git a/src/components/main-app-bar/search-bar/search-bar.tsx b/src/components/main-app-bar/search-bar/search-bar.tsx
index d8c7ede..944eb42 100644
--- a/src/components/main-app-bar/search-bar/search-bar.tsx
+++ b/src/components/main-app-bar/search-bar/search-bar.tsx
@@ -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 });

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list