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 "src/models/resource";
9 import { ApiActions } from "~/services/api/api-actions";
11 const actions: ApiActions = {
12 progressFn: (id: string, working: boolean) => {},
13 errorFn: (id: string, message: string) => {}
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());
25 describe("CommonResourceService", () => {
26 const axiosInstance = axios.create();
27 const axiosMock = new MockAdapter(axiosInstance);
33 it("#create", async () => {
36 .reply(200, { owner_uuid: "ownerUuidValue" });
38 const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
39 const resource = await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
40 expect(resource).toEqual({ ownerUuid: "ownerUuidValue" });
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"});
50 it("#delete", async () => {
52 .onDelete("/resource/uuid")
53 .reply(200, { deleted_at: "now" });
55 const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
56 const resource = await commonResourceService.delete("uuid");
57 expect(resource).toEqual({ deletedAt: "now" });
60 it("#get", async () => {
62 .onGet("/resource/uuid")
63 .reply(200, { modified_at: "now" });
65 const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
66 const resource = await commonResourceService.get("uuid");
67 expect(resource).toEqual({ modifiedAt: "now" });
70 it("#list", async () => {
83 const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
84 const resource = await commonResourceService.list({ limit: 10, offset: 1 });
85 expect(resource).toEqual({