943325b75f13dba6d2d81031add51bbdf5f262c0
[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     let axiosInstance: AxiosInstance;
27     let axiosMock: MockAdapter;
28
29     beforeEach(() => {
30         axiosInstance = axios.create();
31         axiosMock = new MockAdapter(axiosInstance);
32     });
33
34     it("#create", async () => {
35         axiosMock
36             .onPost("/resource")
37             .reply(200, { owner_uuid: "ownerUuidValue" });
38
39         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
40         const resource = await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
41         expect(resource).toEqual({ ownerUuid: "ownerUuidValue" });
42     });
43
44     it("#create maps request params to snake case", async () => {
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     });
50
51     it("#delete", async () => {
52         axiosMock
53             .onDelete("/resource/uuid")
54             .reply(200, { deleted_at: "now" });
55
56         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
57         const resource = await commonResourceService.delete("uuid");
58         expect(resource).toEqual({ deletedAt: "now" });
59     });
60
61     it("#get", async () => {
62         axiosMock
63             .onGet("/resource/uuid")
64             .reply(200, {
65                 modified_at: "now",
66                 properties: {
67                     responsible_owner_uuid: "another_owner"
68                 }
69             });
70
71         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
72         const resource = await commonResourceService.get("uuid");
73         // Only first level keys are mapped to camel case
74         expect(resource).toEqual({
75             modifiedAt: "now",
76             properties: {
77                 responsible_owner_uuid: "another_owner"
78             }
79         });
80     });
81
82     it("#list", async () => {
83         axiosMock
84             .onGet("/resource")
85             .reply(200, {
86                 kind: "kind",
87                 offset: 2,
88                 limit: 10,
89                 items: [{
90                     modified_at: "now",
91                     properties: {
92                         is_active: true
93                     }
94                 }],
95                 items_available: 20
96             });
97
98         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
99         const resource = await commonResourceService.list({ limit: 10, offset: 1 });
100         // First level keys are mapped to camel case inside "items" arrays
101         expect(resource).toEqual({
102             kind: "kind",
103             offset: 2,
104             limit: 10,
105             items: [{
106                 modifiedAt: "now",
107                 properties: {
108                     is_active: true
109                 }
110             }],
111             itemsAvailable: 20
112         });
113     });
114
115     it("#list using POST when query string is too big", async () => {
116         axiosMock
117             .onAny("/resource")
118             .reply(200);
119         const tooBig = 'x'.repeat(1500);
120         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
121         await commonResourceService.list({ filters: tooBig });
122         expect(axiosMock.history.get.length).toBe(0);
123         expect(axiosMock.history.post.length).toBe(1);
124         expect(axiosMock.history.post[0].data.get('filters')).toBe(`[${tooBig}]`);
125         expect(axiosMock.history.post[0].params._method).toBe('GET');
126     });
127 });