chore: tests for hoppscotch-cli (#2300)

Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
Deepanshu Dhruw
2022-05-11 15:44:19 +05:30
committed by GitHub
parent d04520698d
commit 432337b801
30 changed files with 1919 additions and 155 deletions

View File

@@ -0,0 +1,116 @@
import { collectionsRunner } from "../../../utils/collections";
import { HoppRESTRequest } from "@hoppscotch/data";
import axios, { AxiosResponse } from "axios";
import "@relmify/jest-fp-ts";
jest.mock("axios");
const SAMPLE_HOPP_REQUEST = <HoppRESTRequest>{
v: "1",
name: "request",
method: "GET",
endpoint: "https://example.com",
params: [],
headers: [],
preRequestScript: "",
testScript: "",
auth: {
authActive: false,
authType: "none",
},
body: {
contentType: null,
body: null,
},
};
const SAMPLE_RESOLVED_RESPONSE = <AxiosResponse>{
data: { body: 1 },
status: 200,
statusText: "OK",
config: {
url: "https://example.com",
supported: true,
method: "GET",
},
headers: [],
};
describe("collectionsRunner", () => {
beforeEach(() => {
jest.clearAllMocks();
});
afterAll(() => {
jest.clearAllMocks();
});
test("Empty HoppCollection.", () => {
return expect(collectionsRunner([])()).resolves.toStrictEqual([]);
});
test("Empty requests and folders in collection.", () => {
return expect(
collectionsRunner([
{
v: 1,
name: "name",
folders: [],
requests: [],
},
])()
).resolves.toMatchObject([]);
});
test("Non-empty requests in collection.", () => {
(axios as unknown as jest.Mock).mockResolvedValue(SAMPLE_RESOLVED_RESPONSE);
return expect(
collectionsRunner([
{
v: 1,
name: "collection",
folders: [],
requests: [SAMPLE_HOPP_REQUEST],
},
])()
).resolves.toMatchObject([
{
path: "collection/request",
tests: [],
errors: [],
result: true,
},
]);
});
test("Non-empty folders in collection.", () => {
(axios as unknown as jest.Mock).mockResolvedValue(SAMPLE_RESOLVED_RESPONSE);
return expect(
collectionsRunner([
{
v: 1,
name: "collection",
folders: [
{
v: 1,
name: "folder",
folders: [],
requests: [SAMPLE_HOPP_REQUEST],
},
],
requests: [],
},
])()
).resolves.toMatchObject([
{
path: "collection/folder/request",
tests: [],
errors: [],
result: true,
},
]);
});
});

View File

@@ -0,0 +1,35 @@
import { collectionsRunnerResult } from "../../../utils/collections";
const FALSE_RESULT_REPORT = {
path: "some_path",
tests: [],
errors: [],
result: false,
duration: { test: 1, request: 1, preRequest: 1 },
};
const TRUE_RESULT_REPORT = {
path: "some_path",
tests: [],
errors: [],
result: true,
duration: { test: 1, request: 1, preRequest: 1 },
};
describe("collectionsRunnerResult", () => {
test("Empty request-report.", () => {
expect(collectionsRunnerResult([])).toBeTruthy();
});
test("Atleast 1 false result in request-report.", () => {
expect(
collectionsRunnerResult([FALSE_RESULT_REPORT, TRUE_RESULT_REPORT])
).toBeFalsy();
});
test("All true result(s) in request-report.", () => {
expect(
collectionsRunnerResult([TRUE_RESULT_REPORT, TRUE_RESULT_REPORT])
).toBeTruthy();
});
});