Add typescript paths to top level folders
[arvados-workbench2.git] / src / common / api / common-resource-service.test.ts
index d28abc459448ce033d2d51580989f473da73d7b5..a1d5e0868f4e0f0c333251022aed668e2edc7ded 100644 (file)
@@ -2,24 +2,50 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import CommonResourceService from "./common-resource-service";
-import axios from "axios";
+import { CommonResourceService } from "./common-resource-service";
+import axios, { AxiosInstance } from "axios";
 import MockAdapter from "axios-mock-adapter";
+import { Resource } from "~/models/resource";
 
-describe("CommonResourceService", () => {
+export const mockResourceService = <R extends Resource, C extends CommonResourceService<R>>(Service: new (client: AxiosInstance) => C) => {
+    const axiosInstance = axios.create();
+    const axiosMock = new MockAdapter(axiosInstance);
+    const service = new Service(axiosInstance);
+    Object.keys(service).map(key => service[key] = jest.fn());
+    return service;
+};
 
-    const axiosMock = new MockAdapter(axios);
+describe("CommonResourceService", () => {
+    const axiosInstance = axios.create();
+    const axiosMock = new MockAdapter(axiosInstance);
 
     beforeEach(() => {
         axiosMock.reset();
     });
 
+    it("#create", async () => {
+        axiosMock
+            .onPost("/resource/")
+            .reply(200, { owner_uuid: "ownerUuidValue" });
+
+        const commonResourceService = new CommonResourceService(axiosInstance, "resource");
+        const resource = await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
+        expect(resource).toEqual({ ownerUuid: "ownerUuidValue" });
+    });
+
+    it("#create maps request params to snake case", async () => {
+        axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
+        const commonResourceService = new CommonResourceService(axiosInstance, "resource");
+        await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
+        expect(axiosInstance.post).toHaveBeenCalledWith("/resource/", {owner_uuid: "ownerUuidValue"});
+    });
+
     it("#delete", async () => {
         axiosMock
             .onDelete("/resource/uuid")
             .reply(200, { deleted_at: "now" });
 
-        const commonResourceService = new CommonResourceService(axios, "resource");
+        const commonResourceService = new CommonResourceService(axiosInstance, "resource");
         const resource = await commonResourceService.delete("uuid");
         expect(resource).toEqual({ deletedAt: "now" });
     });
@@ -29,7 +55,7 @@ describe("CommonResourceService", () => {
             .onGet("/resource/uuid")
             .reply(200, { modified_at: "now" });
 
-        const commonResourceService = new CommonResourceService(axios, "resource");
+        const commonResourceService = new CommonResourceService(axiosInstance, "resource");
         const resource = await commonResourceService.get("uuid");
         expect(resource).toEqual({ modifiedAt: "now" });
     });
@@ -47,7 +73,7 @@ describe("CommonResourceService", () => {
                 items_available: 20
             });
 
-        const commonResourceService = new CommonResourceService(axios, "resource");
+        const commonResourceService = new CommonResourceService(axiosInstance, "resource");
         const resource = await commonResourceService.list({ limit: 10, offset: 1 });
         expect(resource).toEqual({
             kind: "kind",