Skip to content

Commit

Permalink
feat: add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhoue-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Jul 3, 2023
1 parent 3a3457b commit 28dbcd2
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
97 changes: 97 additions & 0 deletions server/services/CommonService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {
ILegacyCustomClusterClient,
OpenSearchDashboardsRequest,
OpenSearchDashboardsResponseFactory,
RequestHandlerContext,
} from "opensearch-dashboards/server";
import CommonService from "./CommonService";

const contextMock = {
core: {},
} as RequestHandlerContext;
const responseMock = ({
custom: jest.fn((args) => args),
} as unknown) as OpenSearchDashboardsResponseFactory;

const mockedClient = {
callAsCurrentUser: jest.fn(),
callAsInternalUser: jest.fn(),
close: jest.fn(),
asScoped: jest.fn(() => ({
callAsCurrentUser: jest.fn((...args) => args),
callAsInternalUser: jest.fn(),
})),
} as any;

describe("CommonService spec", () => {
it("http method should valid when calling transport.request", async () => {
const commonService = new CommonService(mockedClient);
const result = await commonService.apiCaller(
contextMock,
{
body: {
endpoint: "transport.request",
data: {
method: "invalid method",
},
},
} as OpenSearchDashboardsRequest,
responseMock
);
expect(result).toEqual({
statusCode: 200,
body: {
ok: false,
error: `Method must be one of, case insensitive ['HEAD', 'GET', 'POST', 'PUT', 'DELETE']. Received 'invalid method'.`,
},
});
});

it("should return error when no endpoint is provided", async () => {
const commonService = new CommonService(mockedClient);
const result = await commonService.apiCaller(
contextMock,
{
body: {
endpoint: "",
},
} as OpenSearchDashboardsRequest,
responseMock
);
expect(result).toEqual({
statusCode: 200,
body: {
ok: false,
error: `Expected non-empty string on endpoint`,
},
});
});

it("should patch path when data.path does not start with /", async () => {
const commonService = new CommonService(mockedClient);
const result = await commonService.apiCaller(
contextMock,
{
body: {
endpoint: "transport.request",
data: {
path: "",
},
},
} as OpenSearchDashboardsRequest,
responseMock
);
expect(result).toEqual({
statusCode: 200,
body: {
ok: true,
response: [
"transport.request",
{
path: "/",
},
],
},
});
});
});
2 changes: 1 addition & 1 deletion server/services/CommonService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface ICommonCaller {
<T>(arg: any): T;
}

export default class IndexService {
export default class CommonService {
osDriver: ILegacyCustomClusterClient;

constructor(osDriver: ILegacyCustomClusterClient) {
Expand Down

0 comments on commit 28dbcd2

Please sign in to comment.