1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
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());
18 describe("CommonResourceService", () => {
19 const axiosInstance = axios.create();
20 const axiosMock = new MockAdapter(axiosInstance);
26 it("#create", async () => {
29 .reply(200, { owner_uuid: "ownerUuidValue" });
31 const commonResourceService = new CommonResourceService(axiosInstance, "resource");
32 const resource = await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
33 expect(resource).toEqual({ ownerUuid: "ownerUuidValue" });
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"});
43 it("#delete", async () => {
45 .onDelete("/resource/uuid")
46 .reply(200, { deleted_at: "now" });
48 const commonResourceService = new CommonResourceService(axiosInstance, "resource");
49 const resource = await commonResourceService.delete("uuid");
50 expect(resource).toEqual({ deletedAt: "now" });
53 it("#get", async () => {
55 .onGet("/resource/uuid")
56 .reply(200, { modified_at: "now" });
58 const commonResourceService = new CommonResourceService(axiosInstance, "resource");
59 const resource = await commonResourceService.get("uuid");
60 expect(resource).toEqual({ modifiedAt: "now" });
63 it("#list", async () => {
76 const commonResourceService = new CommonResourceService(axiosInstance, "resource");
77 const resource = await commonResourceService.list({ limit: 10, offset: 1 });
78 expect(resource).toEqual({