Merge branch 'master' into 13765-information-inside-details-panel
[arvados-workbench2.git] / src / common / api / 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 from "axios";
7 import MockAdapter from "axios-mock-adapter";
8
9 describe("CommonResourceService", () => {
10     const axiosInstance = axios.create();
11     const axiosMock = new MockAdapter(axiosInstance);
12
13     beforeEach(() => {
14         axiosMock.reset();
15     });
16
17     it("#create", async () => {
18         axiosMock
19             .onPost("/resource/")
20             .reply(200, { owner_uuid: "ownerUuidValue" });
21
22         const commonResourceService = new CommonResourceService(axiosInstance, "resource");
23         const resource = await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
24         expect(resource).toEqual({ ownerUuid: "ownerUuidValue" });
25     });
26
27     it("#create maps request params to snake case", async () => {
28         axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
29         const commonResourceService = new CommonResourceService(axiosInstance, "resource");
30         await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
31         expect(axiosInstance.post).toHaveBeenCalledWith("/resource/", {owner_uuid: "ownerUuidValue"});
32     });
33
34     it("#delete", async () => {
35         axiosMock
36             .onDelete("/resource/uuid")
37             .reply(200, { deleted_at: "now" });
38
39         const commonResourceService = new CommonResourceService(axiosInstance, "resource");
40         const resource = await commonResourceService.delete("uuid");
41         expect(resource).toEqual({ deletedAt: "now" });
42     });
43
44     it("#get", async () => {
45         axiosMock
46             .onGet("/resource/uuid")
47             .reply(200, { modified_at: "now" });
48
49         const commonResourceService = new CommonResourceService(axiosInstance, "resource");
50         const resource = await commonResourceService.get("uuid");
51         expect(resource).toEqual({ modifiedAt: "now" });
52     });
53
54     it("#list", async () => {
55         axiosMock
56             .onGet("/resource/")
57             .reply(200, {
58                 kind: "kind",
59                 offset: 2,
60                 limit: 10,
61                 items: [{
62                     modified_at: "now"
63                 }],
64                 items_available: 20
65             });
66
67         const commonResourceService = new CommonResourceService(axiosInstance, "resource");
68         const resource = await commonResourceService.list({ limit: 10, offset: 1 });
69         expect(resource).toEqual({
70             kind: "kind",
71             offset: 2,
72             limit: 10,
73             items: [{
74                 modifiedAt: "now"
75             }],
76             itemsAvailable: 20
77         });
78     });
79 });