d909c092aa8bad0a37ac0308a4da8ca80a872585
[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, { AxiosInstance } from "axios";
7 import MockAdapter from "axios-mock-adapter";
8 import { Resource } from "../../models/resource";
9
10 export const mockResourceService = <R extends Resource, C extends CommonResourceService<R>>(Service: new (client: AxiosInstance) => C) => {
11     const axiosInstance = axios.create();
12     const axiosMock = new MockAdapter(axiosInstance);
13     const service = new Service(axiosInstance);
14     Object.keys(service).map(key => service[key] = jest.fn());
15     return service;
16 };
17
18 describe("CommonResourceService", () => {
19     const axiosInstance = axios.create();
20     const axiosMock = new MockAdapter(axiosInstance);
21
22     beforeEach(() => {
23         axiosMock.reset();
24     });
25
26     it("#create", async () => {
27         axiosMock
28             .onPost("/resource/")
29             .reply(200, { owner_uuid: "ownerUuidValue" });
30
31         const commonResourceService = new CommonResourceService(axiosInstance, "resource");
32         const resource = await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
33         expect(resource).toEqual({ ownerUuid: "ownerUuidValue" });
34     });
35
36     it("#create maps request params to snake case", async () => {
37         axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
38         const commonResourceService = new CommonResourceService(axiosInstance, "resource");
39         await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
40         expect(axiosInstance.post).toHaveBeenCalledWith("/resource/", {owner_uuid: "ownerUuidValue"});
41     });
42
43     it("#delete", async () => {
44         axiosMock
45             .onDelete("/resource/uuid")
46             .reply(200, { deleted_at: "now" });
47
48         const commonResourceService = new CommonResourceService(axiosInstance, "resource");
49         const resource = await commonResourceService.delete("uuid");
50         expect(resource).toEqual({ deletedAt: "now" });
51     });
52
53     it("#get", async () => {
54         axiosMock
55             .onGet("/resource/uuid")
56             .reply(200, { modified_at: "now" });
57
58         const commonResourceService = new CommonResourceService(axiosInstance, "resource");
59         const resource = await commonResourceService.get("uuid");
60         expect(resource).toEqual({ modifiedAt: "now" });
61     });
62
63     it("#list", async () => {
64         axiosMock
65             .onGet("/resource/")
66             .reply(200, {
67                 kind: "kind",
68                 offset: 2,
69                 limit: 10,
70                 items: [{
71                     modified_at: "now"
72                 }],
73                 items_available: 20
74             });
75
76         const commonResourceService = new CommonResourceService(axiosInstance, "resource");
77         const resource = await commonResourceService.list({ limit: 10, offset: 1 });
78         expect(resource).toEqual({
79             kind: "kind",
80             offset: 2,
81             limit: 10,
82             items: [{
83                 modifiedAt: "now"
84             }],
85             itemsAvailable: 20
86         });
87     });
88 });