X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/515f96c411358b45a978b6b7c93da860b09ee7d0..6af02d6e6596f45ac761daa04a53b2466e86ea98:/services/workbench2/src/components/search-input/search-input.test.tsx

diff --git a/services/workbench2/src/components/search-input/search-input.test.tsx b/services/workbench2/src/components/search-input/search-input.test.tsx
index de36ae3858..ba0f43d8b5 100644
--- a/services/workbench2/src/components/search-input/search-input.test.tsx
+++ b/services/workbench2/src/components/search-input/search-input.test.tsx
@@ -11,12 +11,10 @@ configure({ adapter: new Adapter() });
 
 describe("<SearchInput />", () => {
 
-    // jest.useFakeTimers() applies to all setTimeout functions
-    jest.useFakeTimers();
-
     let onSearch: () => void;
 
     beforeEach(() => {
+        jest.useFakeTimers();
         onSearch = jest.fn();
     });
 
@@ -46,7 +44,7 @@ describe("<SearchInput />", () => {
             const searchInput = mount(<SearchInput selfClearProp="" value="" onSearch={onSearch} debounce={1000} />);
             searchInput.find("input").simulate("change", { target: { value: "current value" } });
             searchInput.find("form").simulate("submit");
-            jest.runTimersToTime(1000);
+            jest.advanceTimersByTime(1000);
             expect(onSearch).toHaveBeenCalledTimes(1);
             expect(onSearch).toBeCalledWith("current value");
         });
@@ -58,45 +56,39 @@ describe("<SearchInput />", () => {
             const searchInput = mount(<SearchInput selfClearProp="" value="" onSearch={onSearch} />);
             searchInput.find("input").simulate("change", { target: { value: "current value" } });
             expect(onSearch).not.toBeCalled();
-            setTimeout(() => {
-                expect(onSearch).toBeCalledWith("current value");
-            }, DEFAULT_SEARCH_DEBOUNCE);
+            jest.advanceTimersByTime(DEFAULT_SEARCH_DEBOUNCE);
+            expect(onSearch).toBeCalledWith("current value");
         });
 
         it("calls onSearch after the time specified in props has passed", () => {
             const searchInput = mount(<SearchInput selfClearProp="" value="" onSearch={onSearch} debounce={2000}/>);
             searchInput.find("input").simulate("change", { target: { value: "current value" } });
+            jest.advanceTimersByTime(1000);
             expect(onSearch).not.toBeCalled();
-            setTimeout(() => {
-                expect(onSearch).toBeCalledWith("current value");
-            }, 1000);
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).toBeCalledWith("current value");
         });
 
         it("calls onSearch only once after no change happened during the specified time", () => {
             const searchInput = mount(<SearchInput selfClearProp="" value="" onSearch={onSearch} debounce={1000}/>);
             searchInput.find("input").simulate("change", { target: { value: "current value" } });
-            setTimeout(() => {
-                searchInput.find("input").simulate("change", { target: { value: "changed value" } });
-            }, 500);
-            setTimeout(() => {
-                expect(onSearch).toHaveBeenCalledTimes(1);
-            }, 1000);
+            jest.advanceTimersByTime(500);
+            searchInput.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 searchInput = mount(<SearchInput selfClearProp="" value="" onSearch={onSearch} debounce={1000}/>);
             searchInput.find("input").simulate("change", { target: { value: "current value" } });
-            setTimeout(() => {
-                searchInput.find("input").simulate("change", { target: { value: "intermediate value" } });
-            }, 500);
-            setTimeout(() => {
-                expect(onSearch).toBeCalledWith("intermediate value");
-            }, 1000);
+            jest.advanceTimersByTime(500);
+            searchInput.find("input").simulate("change", { target: { value: "intermediate value" } });
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).toBeCalledWith("intermediate value");
             searchInput.find("input").simulate("change", { target: { value: "latest value" } });
-            setTimeout(() => {
-                expect(onSearch).toBeCalledWith("latest value");
-                expect(onSearch).toHaveBeenCalledTimes(2);
-            }, 1000);
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).toBeCalledWith("latest value");
+            expect(onSearch).toHaveBeenCalledTimes(2);
 
         });
 
@@ -107,23 +99,20 @@ describe("<SearchInput />", () => {
             const searchInput = mount(<SearchInput selfClearProp="abc" value="123" onSearch={onSearch} debounce={1000}/>);
 
             // component should clear value upon creation
-            setTimeout(() => {
-                expect(onSearch).toBeCalledWith("");
-                expect(onSearch).toHaveBeenCalledTimes(1);
-            }, 1000);
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).toBeCalledWith("");
+            expect(onSearch).toHaveBeenCalledTimes(1);
 
             // component should not clear on same selfClearProp
             searchInput.setProps({ selfClearProp: 'abc' });
-            setTimeout(() => {
-                expect(onSearch).toHaveBeenCalledTimes(1);
-            }, 1000);
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).toHaveBeenCalledTimes(1);
 
             // component should clear on selfClearProp change
             searchInput.setProps({ selfClearProp: '111' });
-            setTimeout(() => {
-                expect(onSearch).toBeCalledWith("");
-                expect(onSearch).toHaveBeenCalledTimes(2);
-            }, 1000);
+            jest.advanceTimersByTime(1000);
+            expect(onSearch).toBeCalledWith("");
+            expect(onSearch).toHaveBeenCalledTimes(2);
         });
     });
 });
