15672: Adds unit test to API's list request with method=POST.
[arvados-workbench2.git] / src / services / common-service / common-resource-service.test.ts
index 5a3bae25fdf005d71245ef821b6cea7693d03a8d..a53ec40074eabf2dccb1700454569c4dd1a6f5ed 100644 (file)
@@ -32,7 +32,7 @@ describe("CommonResourceService", () => {
 
     it("#create", async () => {
         axiosMock
-            .onPost("/resource/")
+            .onPost("/resource")
             .reply(200, { owner_uuid: "ownerUuidValue" });
 
         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
@@ -41,10 +41,13 @@ describe("CommonResourceService", () => {
     });
 
     it("#create maps request params to snake case", async () => {
+        const realPost = axiosInstance.post;
         axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
         await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
-        expect(axiosInstance.post).toHaveBeenCalledWith("/resource/", {owner_uuid: "ownerUuidValue"});
+        expect(axiosInstance.post).toHaveBeenCalledWith("/resource", {owner_uuid: "ownerUuidValue"});
+        // Restore post function so that tests below don't break.
+        axiosInstance.post = realPost;
     });
 
     it("#delete", async () => {
@@ -60,36 +63,67 @@ describe("CommonResourceService", () => {
     it("#get", async () => {
         axiosMock
             .onGet("/resource/uuid")
-            .reply(200, { modified_at: "now" });
+            .reply(200, {
+                modified_at: "now",
+                properties: {
+                    responsible_owner_uuid: "another_owner"
+                }
+            });
 
         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
         const resource = await commonResourceService.get("uuid");
-        expect(resource).toEqual({ modifiedAt: "now" });
+        // Only first level keys are mapped to camel case
+        expect(resource).toEqual({
+            modifiedAt: "now",
+            properties: {
+                responsible_owner_uuid: "another_owner"
+            }
+        });
     });
 
     it("#list", async () => {
         axiosMock
-            .onGet("/resource/")
+            .onGet("/resource")
             .reply(200, {
                 kind: "kind",
                 offset: 2,
                 limit: 10,
                 items: [{
-                    modified_at: "now"
+                    modified_at: "now",
+                    properties: {
+                        is_active: true
+                    }
                 }],
                 items_available: 20
             });
 
         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
         const resource = await commonResourceService.list({ limit: 10, offset: 1 });
+        // First level keys are mapped to camel case inside "items" arrays
         expect(resource).toEqual({
             kind: "kind",
             offset: 2,
             limit: 10,
             items: [{
-                modifiedAt: "now"
+                modifiedAt: "now",
+                properties: {
+                    is_active: true
+                }
             }],
             itemsAvailable: 20
         });
     });
+
+    it("#list using POST when query string is too big", async () => {
+        axiosMock
+            .onPost("/resource")
+            .reply(200);
+        const tooBig = 'x'.repeat(1500);
+        const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
+        const resource = await commonResourceService.list({ filters: tooBig });
+        expect(axiosMock.history.get.length).toBe(0);
+        expect(axiosMock.history.post.length).toBe(1);
+        expect(axiosMock.history.post[0].data.get('filters')).toBe('['+tooBig+']');
+        expect(axiosMock.history.post[0].params._method).toBe('GET');
+    });
 });