Format imports
[arvados-workbench2.git] / src / store / data-explorer / data-explorer-middleware.test.ts
index 412e703db0f3ca395a74c0ea829d0d915097a7e0..814d5855c0b6a45b0ee0224d73e382f13b561560 100644 (file)
@@ -1,52 +1,16 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 import { DataExplorerMiddlewareService } from "./data-explorer-middleware-service";
 import { dataExplorerMiddleware } from "./data-explorer-middleware";
-import { MiddlewareAPI } from "../../../node_modules/redux";
-import { columns } from "../../views/project-panel/project-panel";
-import { DataColumns } from "../../components/data-table/data-table";
+import { MiddlewareAPI } from "redux";
+import { DataColumns } from "~/components/data-table/data-table";
 import { dataExplorerActions } from "./data-explorer-action";
+import { SortDirection } from "~/components/data-table/data-column";
 
-// Copyright (C) The Arvados Authors. All rights reserved.
-//
-// SPDX-License-Identifier: AGPL-3.0
 
 describe("DataExplorerMiddleware", () => {
-    it("initializes service with middleware api", () => {
-        const config = {
-            id: "",
-            columns: [],
-            requestItems: jest.fn(),
-            setApi: jest.fn()
-        };
-        const service = new ServiceMock(config);
-        const api = {
-            getState: jest.fn(),
-            dispatch: jest.fn()
-        };
-        dataExplorerMiddleware(service)(api)(jest.fn());
-        expect(config.setApi).toHaveBeenCalled();
-    });
-
-    it("initializes columns in the store", () => {
-        const config = {
-            id: "Id",
-            columns: [{
-                name: "Column",
-                selected: true,
-                render: jest.fn()
-            }],
-            requestItems: jest.fn(),
-            setApi: jest.fn()
-        };
-        const service = new ServiceMock(config);
-        const api = {
-            getState: jest.fn(),
-            dispatch: jest.fn()
-        };
-        const next = jest.fn();
-        dataExplorerMiddleware(service)(api)(next);
-        expect(next)
-            .toHaveBeenCalledWith(dataExplorerActions.SET_COLUMNS({ id: service.Id, columns: service.Columns }));
-    });
 
     it("handles only actions that are identified by service id", () => {
         const config = {
@@ -54,6 +18,9 @@ describe("DataExplorerMiddleware", () => {
             columns: [{
                 name: "Column",
                 selected: true,
+                configurable: false,
+                sortDirection: SortDirection.NONE,
+                filters: [],
                 render: jest.fn()
             }],
             requestItems: jest.fn(),
@@ -79,6 +46,9 @@ describe("DataExplorerMiddleware", () => {
             columns: [{
                 name: "Column",
                 selected: true,
+                configurable: false,
+                sortDirection: SortDirection.NONE,
+                filters: [],
                 render: jest.fn()
             }],
             requestItems: jest.fn(),
@@ -109,7 +79,7 @@ describe("DataExplorerMiddleware", () => {
         };
         const next = jest.fn();
         const middleware = dataExplorerMiddleware(service)(api)(next);
-        middleware(dataExplorerActions.SET_PAGE({ id: service.Id, page: 0 }));
+        middleware(dataExplorerActions.SET_PAGE({ id: service.getId(), page: 0 }));
         expect(api.dispatch).toHaveBeenCalledTimes(1);
     });
 
@@ -127,7 +97,7 @@ describe("DataExplorerMiddleware", () => {
         };
         const next = jest.fn();
         const middleware = dataExplorerMiddleware(service)(api)(next);
-        middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.Id, rowsPerPage: 0 }));
+        middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.getId(), rowsPerPage: 0 }));
         expect(api.dispatch).toHaveBeenCalledTimes(1);
     });
 
@@ -145,7 +115,7 @@ describe("DataExplorerMiddleware", () => {
         };
         const next = jest.fn();
         const middleware = dataExplorerMiddleware(service)(api)(next);
-        middleware(dataExplorerActions.SET_FILTERS({ id: service.Id, columnName: "", filters: [] }));
+        middleware(dataExplorerActions.SET_FILTERS({ id: service.getId(), columnName: "", filters: [] }));
         expect(api.dispatch).toHaveBeenCalledTimes(2);
     });
 
@@ -163,15 +133,8 @@ describe("DataExplorerMiddleware", () => {
         };
         const next = jest.fn();
         const middleware = dataExplorerMiddleware(service)(api)(next);
-        middleware(dataExplorerActions.SET_PAGE({ id: service.Id, page: 0 }));
-        middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.Id, rowsPerPage: 0 }));
-        middleware(dataExplorerActions.SET_FILTERS({ id: service.Id, columnName: "", filters: [] }));
-        middleware(dataExplorerActions.TOGGLE_SORT({ id: service.Id, columnName: "" }));
-        middleware(dataExplorerActions.TOGGLE_COLUMN({ id: service.Id, columnName: "" }));
-        middleware(dataExplorerActions.REQUEST_ITEMS({ id: service.Id }));
-        middleware(dataExplorerActions.SET_SEARCH_VALUE({ id: service.Id, searchValue: "" }));
-        middleware(dataExplorerActions.RESET_PAGINATION({ id: service.Id }));
-        expect(api.dispatch).toHaveBeenCalledTimes(7);
+        middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.getId(), rowsPerPage: 0 }));
+        expect(api.dispatch).toHaveBeenCalledTimes(1);
     });
 
     it("handles TOGGLE_SORT action", () => {
@@ -188,7 +151,7 @@ describe("DataExplorerMiddleware", () => {
         };
         const next = jest.fn();
         const middleware = dataExplorerMiddleware(service)(api)(next);
-        middleware(dataExplorerActions.TOGGLE_SORT({ id: service.Id, columnName: "" }));
+        middleware(dataExplorerActions.TOGGLE_SORT({ id: service.getId(), columnName: "" }));
         expect(api.dispatch).toHaveBeenCalledTimes(1);
     });
 
@@ -206,7 +169,7 @@ describe("DataExplorerMiddleware", () => {
         };
         const next = jest.fn();
         const middleware = dataExplorerMiddleware(service)(api)(next);
-        middleware(dataExplorerActions.SET_SEARCH_VALUE({ id: service.Id, searchValue: "" }));
+        middleware(dataExplorerActions.SET_EXPLORER_SEARCH_VALUE({ id: service.getId(), searchValue: "" }));
         expect(api.dispatch).toHaveBeenCalledTimes(2);
     });
 
@@ -224,11 +187,11 @@ describe("DataExplorerMiddleware", () => {
         };
         const next = jest.fn();
         const middleware = dataExplorerMiddleware(service)(api)(next);
-        middleware(dataExplorerActions.SET_COLUMNS({ id: service.Id, columns: [] }));
-        middleware(dataExplorerActions.SET_ITEMS({ id: service.Id, items: [], rowsPerPage: 0, itemsAvailable: 0, page: 0 }));
-        middleware(dataExplorerActions.TOGGLE_COLUMN({ id: service.Id, columnName: "" }));
+        middleware(dataExplorerActions.SET_COLUMNS({ id: service.getId(), columns: [] }));
+        middleware(dataExplorerActions.SET_ITEMS({ id: service.getId(), items: [], rowsPerPage: 0, itemsAvailable: 0, page: 0 }));
+        middleware(dataExplorerActions.TOGGLE_COLUMN({ id: service.getId(), columnName: "" }));
         expect(api.dispatch).toHaveBeenCalledTimes(0);
-        expect(next).toHaveBeenCalledTimes(4);
+        expect(next).toHaveBeenCalledTimes(3);
     });
 
 });
@@ -237,27 +200,16 @@ class ServiceMock extends DataExplorerMiddlewareService {
     constructor(private config: {
         id: string,
         columns: DataColumns<any>,
-        requestItems: (api: MiddlewareAPI) => void;
-        setApi: () => void;
+        requestItems: (api: MiddlewareAPI) => void
     }) {
-        super();
+        super(config.id);
     }
 
-    get Id() {
-        return this.config.id;
-    }
-
-    get Columns() {
+    getColumns() {
         return this.config.columns;
     }
 
-    requestItems() {
-        this.config.requestItems(this.api);
+    requestItems(api: MiddlewareAPI) {
+        this.config.requestItems(api);
     }
-
-    set Api(value: MiddlewareAPI) {
-        this.config.setApi();
-        this.api = value;
-    }
-
-}
\ No newline at end of file
+}