15407: Tests that only first level keys are mapped on resource objects.
[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         axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
45         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
46         await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
47         expect(axiosInstance.post).toHaveBeenCalledWith("/resource/", {owner_uuid: "ownerUuidValue"});
48     });
49
50     it("#delete", async () => {
51         axiosMock
52             .onDelete("/resource/uuid")
53             .reply(200, { deleted_at: "now" });
54
55         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
56         const resource = await commonResourceService.delete("uuid");
57         expect(resource).toEqual({ deletedAt: "now" });
58     });
59
60     it("#get", async () => {
61         axiosMock
62             .onGet("/resource/uuid")
63             .reply(200, {
64                 modified_at: "now",
65                 properties: {
66                     responsible_owner_uuid: "another_owner"
67                 }
68             });
69
70         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
71         const resource = await commonResourceService.get("uuid");
72         // Only first level keys are mapped to camel case
73         expect(resource).toEqual({
74             modifiedAt: "now",
75             properties: {
76                 responsible_owner_uuid: "another_owner"
77             }
78         });
79     });
80
81     it("#list", async () => {
82         axiosMock
83             .onGet("/resource/")
84             .reply(200, {
85                 kind: "kind",
86                 offset: 2,
87                 limit: 10,
88                 items: [{
89                     modified_at: "now",
90                     properties: {
91                         is_active: true
92                     }
93                 }],
94                 items_available: 20
95             });
96
97         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
98         const resource = await commonResourceService.list({ limit: 10, offset: 1 });
99         // First level keys are mapped to camel case inside "items" arrays
100         expect(resource).toEqual({
101             kind: "kind",
102             offset: 2,
103             limit: 10,
104             items: [{
105                 modifiedAt: "now",
106                 properties: {
107                     is_active: true
108                 }
109             }],
110             itemsAvailable: 20
111         });
112     });
113 });