export const mockResourceService = <R extends Resource, C extends CommonResourceService<R>>(
Service: new (client: AxiosInstance, actions: ApiActions) => C) => {
- const axiosInstance = axios.create();
- const axiosMock = new MockAdapter(axiosInstance);
- const service = new Service(axiosInstance, actions);
- Object.keys(service).map(key => service[key] = jest.fn());
- return service;
-};
+ const axiosInstance = axios.create();
+ const service = new Service(axiosInstance, actions);
+ Object.keys(service).map(key => service[key] = jest.fn());
+ return service;
+ };
describe("CommonResourceService", () => {
let axiosInstance: AxiosInstance;
--- /dev/null
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import axios, { AxiosInstance } from "axios";
+import { ApiActions } from "~/services/api/api-actions";
+import { CommonService } from "./common-service";
+
+const actions: ApiActions = {
+ progressFn: (id: string, working: boolean) => {},
+ errorFn: (id: string, message: string) => {}
+};
+
+describe("CommonService", () => {
+ let commonService: CommonService<any>;
+
+ beforeEach(() => {
+ commonService = new CommonService<any>({} as AxiosInstance, "resource", actions);
+ });
+
+ it("throws an exception when passing uuid as empty string to get()", () => {
+ expect(() => commonService.get("")).toThrowError("UUID cannot be empty string");
+ });
+
+ it("throws an exception when passing uuid as empty string to update()", () => {
+ expect(() => commonService.update("", {})).toThrowError("UUID cannot be empty string");
+ });
+
+ it("throws an exception when passing uuid as empty string to delete()", () => {
+ expect(() => commonService.delete("")).toThrowError("UUID cannot be empty string");
+ });
+});
\ No newline at end of file
}
}
+ private validateUuid(uuid: string) {
+ if (uuid === "") {
+ throw new Error('UUID cannot be empty string');
+ }
+ }
+
static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true, showErrors = true): Promise<R> {
const reqId = uuid();
actions.progressFn(reqId, true);
}
delete(uuid: string): Promise<T> {
+ this.validateUuid(uuid);
return CommonService.defaultResponse(
this.serverApi
.delete(this.resourceType + '/' + uuid),
}
get(uuid: string, showErrors?: boolean) {
+ this.validateUuid(uuid);
return CommonService.defaultResponse(
this.serverApi
.get<T>(this.resourceType + '/' + uuid),
}
update(uuid: string, data: Partial<T>) {
+ this.validateUuid(uuid);
return CommonService.defaultResponse(
this.serverApi
.put<T>(this.resourceType + '/' + uuid, data && CommonService.mapKeys(_.snakeCase)(data)),