15672: Adds unit test to API's list request with method=POST.
[arvados-workbench2.git] / src / services / common-service / common-resource-service.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { CommonResourceService } from "./common-resource-service";
6 import axios, { AxiosInstance } from "axios";
7 import MockAdapter from "axios-mock-adapter";
8 import { Resource } from "src/models/resource";
9 import { ApiActions } from "~/services/api/api-actions";
10
11 const actions: ApiActions = {
12     progressFn: (id: string, working: boolean) => {},
13     errorFn: (id: string, message: string) => {}
14 };
15
16 export const mockResourceService = <R extends Resource, C extends CommonResourceService<R>>(
17     Service: new (client: AxiosInstance, actions: ApiActions) => C) => {
18     const axiosInstance = axios.create();
19     const axiosMock = new MockAdapter(axiosInstance);
20     const service = new Service(axiosInstance, actions);
21     Object.keys(service).map(key => service[key] = jest.fn());
22     return service;
23 };
24
25 describe("CommonResourceService", () => {
26     const axiosInstance = axios.create();
27     const axiosMock = new MockAdapter(axiosInstance);
28
29     beforeEach(() => {
30         axiosMock.reset();
31     });
32
33     it("#create", async () => {
34         axiosMock
35             .onPost("/resource")
36             .reply(200, { owner_uuid: "ownerUuidValue" });
37
38         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
39         const resource = await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
40         expect(resource).toEqual({ ownerUuid: "ownerUuidValue" });
41     });
42
43     it("#create maps request params to snake case", async () => {
44         const realPost = axiosInstance.post;
45         axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
46         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
47         await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
48         expect(axiosInstance.post).toHaveBeenCalledWith("/resource", {owner_uuid: "ownerUuidValue"});
49         // Restore post function so that tests below don't break.
50         axiosInstance.post = realPost;
51     });
52
53     it("#delete", async () => {
54         axiosMock
55             .onDelete("/resource/uuid")
56             .reply(200, { deleted_at: "now" });
57
58         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
59         const resource = await commonResourceService.delete("uuid");
60         expect(resource).toEqual({ deletedAt: "now" });
61     });
62
63     it("#get", async () => {
64         axiosMock
65             .onGet("/resource/uuid")
66             .reply(200, {
67                 modified_at: "now",
68                 properties: {
69                     responsible_owner_uuid: "another_owner"
70                 }
71             });
72
73         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
74         const resource = await commonResourceService.get("uuid");
75         // Only first level keys are mapped to camel case
76         expect(resource).toEqual({
77             modifiedAt: "now",
78             properties: {
79                 responsible_owner_uuid: "another_owner"
80             }
81         });
82     });
83
84     it("#list", async () => {
85         axiosMock
86             .onGet("/resource")
87             .reply(200, {
88                 kind: "kind",
89                 offset: 2,
90                 limit: 10,
91                 items: [{
92                     modified_at: "now",
93                     properties: {
94                         is_active: true
95                     }
96                 }],
97                 items_available: 20
98             });
99
100         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
101         const resource = await commonResourceService.list({ limit: 10, offset: 1 });
102         // First level keys are mapped to camel case inside "items" arrays
103         expect(resource).toEqual({
104             kind: "kind",
105             offset: 2,
106             limit: 10,
107             items: [{
108                 modifiedAt: "now",
109                 properties: {
110                     is_active: true
111                 }
112             }],
113             itemsAvailable: 20
114         });
115     });
116
117     it("#list using POST when query string is too big", async () => {
118         axiosMock
119             .onPost("/resource")
120             .reply(200);
121         const tooBig = 'x'.repeat(1500);
122         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
123         const resource = await commonResourceService.list({ filters: tooBig });
124         expect(axiosMock.history.get.length).toBe(0);
125         expect(axiosMock.history.post.length).toBe(1);
126         expect(axiosMock.history.post[0].data.get('filters')).toBe('['+tooBig+']');
127         expect(axiosMock.history.post[0].params._method).toBe('GET');
128     });
129 });