Skip to content

Commit

Permalink
test cov for list, remove and create specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazuh committed Feb 5, 2024
1 parent e6b414c commit a0c0335
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 33 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"test": "jest --coverage --silent=true",
"test": "jest --ci --coverage --verbose --silent=true",
"test:watch": "jest --silent=true --watch",
"test:verbose": "DEBUG_PRINT_LIMIT=9999999 npm run test -- --silent=false",
"preview": "vite preview"
},
Expand Down
6 changes: 3 additions & 3 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ describe("App", () => {
}));
});

it("Renders fine at first", async () => {
it("renders fine at first", async () => {
render(<App />);
screen.getByText(/Postmaiden/i);
});

it("Suspends interaction if persisted session changes", async () => {
it("suspends interaction if persisted session changes", async () => {
render(<App />);

await act(() => jest.runAllTimers());
Expand All @@ -52,7 +52,7 @@ describe("App", () => {
).toBeVisible();
});

it("Blocks interaction if browser doesnt support the offline persistence", () => {
it("blocks interaction if browser doesnt support the offline persistence", () => {
(isPersistenceSupported as jest.Mock).mockReturnValue(false);

render(<App />);
Expand Down
207 changes: 198 additions & 9 deletions src/features/project-workspace/ProjectWorkspacePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import "@testing-library/jest-dom";
import { act, render, screen } from "@testing-library/react";
import * as wouter from "wouter";
import { ProjectWorkspacePage } from "./ProjectWorkspacePage";
import * as OPFSProjectService from "./opfs-project-service";
import * as OPFSSharedInternalsService from "@/services/opfs-projects-shared-internals";

jest.mock("wouter", () => ({
useParams: jest.fn().mockReturnValue({}),
}));

jest.mock("@/services/opfs-projects-shared-internals", () => ({
retrieveProject: jest.fn(),
persistProject: jest.fn(),
}));

describe("Projects workspace page", () => {
Expand All @@ -20,21 +21,23 @@ describe("Projects workspace page", () => {
uuid: "2372aa5e-9042-4c47-a7e5-a01d9d6005ea",
});

jest.spyOn(OPFSProjectService, "retrieveProject").mockResolvedValue({
uuid: "2372aa5e-9042-4c47-a7e5-a01d9d6005ea",
name: "Umbrella Corp API",
sections: [],
specs: [],
});
jest
.spyOn(OPFSSharedInternalsService, "retrieveProject")
.mockResolvedValue({
uuid: "2372aa5e-9042-4c47-a7e5-a01d9d6005ea",
name: "Umbrella Corp API",
sections: [],
specs: [],
});
});

it("displays the name of the retrieved project matching the url params", async () => {
it("displays the name of the retrieved project matching the url params, by retrieving the project from OFPS", async () => {
await act(async () => render(<ProjectWorkspacePage />));

expect(screen.getByText(/Umbrella Corp API/i)).toBeVisible();
});

it("shows error if theres an invalid url params", async () => {
it("shows error if theres an invalid uuid as url param", async () => {
jest.spyOn(wouter, "useParams").mockReturnValue({
uuid: "aaaa-bbbb-cccc-dddd-eeee",
});
Expand All @@ -43,4 +46,190 @@ describe("Projects workspace page", () => {

expect(screen.getByText(/Invalid project URL/i)).toBeVisible();
});

it("lists the request specs of the retrieved project, by retrieving the project from OFPS", async () => {
jest
.spyOn(OPFSSharedInternalsService, "retrieveProject")
.mockResolvedValue({
uuid: "2372aa5e-9042-4c47-a7e5-a01d9d6005ea",
name: "Umbrella Corp API",
specs: [
{
uuid: "681d4cb2-2654-41b7-93b0-359703458299",
method: "GET",
url: "https://re.capcom.com/stars-members",
headers: [
{
key: "Content-Type",
value: "application/json",
isEnabled: true,
},
],
body: "{}",
},
],
sections: [],
});

await act(async () => render(<ProjectWorkspacePage />));
expect(screen.getByText(/stars-members/i)).toBeVisible();
});

it("can easily create new request spec with everything empty but some default headers, by updating the project stored in OFPS", async () => {
jest
.spyOn(OPFSSharedInternalsService, "retrieveProject")
.mockResolvedValue({
uuid: "2372aa5e-9042-4c47-a7e5-a01d9d6005ea",
name: "Umbrella Corp API",
specs: [
{
uuid: "681d4cb2-2654-41b7-93b0-359703458299",
method: "GET",
url: "https://re.capcom.com/stars-members",
headers: [
{
key: "Content-Type",
value: "application/json",
isEnabled: true,
},
],
body: "{}",
},
],
sections: [],
});

jest.spyOn(OPFSSharedInternalsService, "persistProject");

await act(async () => render(<ProjectWorkspacePage />));

const button = screen.getByRole("button", { name: /Create request spec/i });
await act(async () => button.click());

expect(OPFSSharedInternalsService.persistProject).toHaveBeenCalledTimes(1);

const [lastCallArg] = (
OPFSSharedInternalsService.persistProject as jest.Mock
).mock.lastCall;
expect(lastCallArg.uuid).toEqual("2372aa5e-9042-4c47-a7e5-a01d9d6005ea");
expect(lastCallArg.name).toEqual("Umbrella Corp API");
expect(lastCallArg.specs).toHaveLength(2);

const recentlyInsertedSpec = lastCallArg.specs[1];
expect(recentlyInsertedSpec.headers).toEqual([
{
key: "Content-Type",
value: "application/json",
isEnabled: false,
},
{
key: "Accept",
value: "application/json",
isEnabled: true,
},
]);
expect(recentlyInsertedSpec.method).toEqual("GET");
expect(recentlyInsertedSpec.url).toEqual("");
expect(recentlyInsertedSpec.body).toEqual("");
});

it("can remove a existing spec, by updating the project stored in OFPS", async () => {
jest
.spyOn(OPFSSharedInternalsService, "retrieveProject")
.mockResolvedValue({
uuid: "2372aa5e-9042-4c47-a7e5-a01d9d6005ea",
name: "Umbrella Corp API",
specs: [
{
uuid: "681d4cb2-2654-41b7-93b0-359703458299",
method: "GET",
url: "https://re.capcom.com/stars-members",
headers: [
{
key: "Content-Type",
value: "application/json",
isEnabled: true,
},
],
body: "{}",
},
{
uuid: "6203a158-66ab-446a-8c73-e3578d4f5951",
method: "GET",
url: "https://re.capcom.com/bio-weapons",
headers: [
{
key: "Content-Type",
value: "application/json",
isEnabled: true,
},
],
body: "{}",
},
{
uuid: "a127b563-1c28-4d97-9e34-0617499b228b",
method: "GET",
url: "https://re.capcom.com/nest-labs",
headers: [
{
key: "Content-Type",
value: "application/json",
isEnabled: true,
},
],
body: "{}",
},
],
sections: [],
});

jest.spyOn(OPFSSharedInternalsService, "persistProject");

await act(async () => render(<ProjectWorkspacePage />));

const removeBtns = screen.getAllByRole("button", { name: /Remove/i });
const removeBtn = removeBtns.at(1); // "bio-weapons" spec, the one in the middle
await act(async () => removeBtn!.click());

const confirmBtn = screen.getByRole("button", {
name: /Remove request spec/i,
});
await act(async () => confirmBtn.click());

expect(OPFSSharedInternalsService.persistProject).toHaveBeenCalledTimes(1);

const [lastCallArg] = (
OPFSSharedInternalsService.persistProject as jest.Mock
).mock.lastCall;
expect(lastCallArg.uuid).toEqual("2372aa5e-9042-4c47-a7e5-a01d9d6005ea");
expect(lastCallArg.name).toEqual("Umbrella Corp API");
expect(lastCallArg.specs).toEqual([
{
uuid: "681d4cb2-2654-41b7-93b0-359703458299",
method: "GET",
url: "https://re.capcom.com/stars-members",
headers: [
{
key: "Content-Type",
value: "application/json",
isEnabled: true,
},
],
body: "{}",
},
{
uuid: "a127b563-1c28-4d97-9e34-0617499b228b",
method: "GET",
url: "https://re.capcom.com/nest-labs",
headers: [
{
key: "Content-Type",
value: "application/json",
isEnabled: true,
},
],
body: "{}",
},
]);
});
});
50 changes: 50 additions & 0 deletions src/features/project-workspace/opfs-project-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { retrieveProject } from "./opfs-project-service";
import * as opfsAdapters from "../../services/origin-private-file-system";

jest.mock("../../services/origin-private-file-system");

describe("OPFS project service", () => {
beforeEach(() => {
jest.resetAllMocks();
});

test("project retrieval is integrated with the most primitive OPFS service module", async () => {
(opfsAdapters.makeOpfsMainDirAdapter as jest.Mock).mockResolvedValueOnce({
retrieveFilenames: jest
.fn()
.mockResolvedValue([
"82184240-6b29-4ae8-82f5-fbe7d1bb814a_MyAPI.json",
"e5ae94f4-2c32-4129-a2b1-cdf2c4c770dc_Biohazard API.json",
]),
});

(opfsAdapters.makeOpfsFileAdapter as jest.Mock).mockImplementationOnce(
(options: { filename: string }) => {
if (
!options.filename.startsWith("e5ae94f4-2c32-4129-a2b1-cdf2c4c770dc")
) {
throw new Error("Unexpected test case.");
}

return {
retrieve: jest.fn().mockImplementation(async () => {
return {
uuid: "e5ae94f4-2c32-4129-a2b1-cdf2c4c770dc",
name: "Biohazard API",
mocked: "content",
};
}),
};
}
);

const content = await retrieveProject(
"e5ae94f4-2c32-4129-a2b1-cdf2c4c770dc"
);
expect(content).toEqual({
uuid: "e5ae94f4-2c32-4129-a2b1-cdf2c4c770dc",
name: "Biohazard API",
mocked: "content",
});
});
});
10 changes: 5 additions & 5 deletions src/features/projects-management/ProjectsManagementPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { act, fireEvent, render, screen } from "@testing-library/react";
import { ProjectsManagementPage } from "./ProjectsManagementPage";
import * as OPFSProjectsListingService from "./opfs-projects-listing-service";

describe("Projects listing management page", () => {
describe("Projects listing page", () => {
beforeEach(() => {
jest.clearAllMocks();
});
Expand All @@ -20,7 +20,7 @@ describe("Projects listing management page", () => {
expect(screen.getByText(/Start now/i)).toBeVisible();
});

it("lists projects", async () => {
it("lists projects by retrieving from OPFS", async () => {
jest
.spyOn(OPFSProjectsListingService, "retrieveProjectsListing")
.mockResolvedValue({
Expand All @@ -42,7 +42,7 @@ describe("Projects listing management page", () => {
expect(screen.getByText(/Some third thing/i)).toBeVisible();
});

it("can create new project", async () => {
it("can create new project by storing it in OPFS", async () => {
jest
.spyOn(OPFSProjectsListingService, "retrieveProjectsListing")
.mockResolvedValue({
Expand All @@ -69,7 +69,7 @@ describe("Projects listing management page", () => {
).toHaveBeenCalledWith("My favorite API");
});

it("can remove a project", async () => {
it("can remove a project by removing the file from OPFS", async () => {
jest
.spyOn(OPFSProjectsListingService, "retrieveProjectsListing")
.mockResolvedValue({
Expand All @@ -91,7 +91,7 @@ describe("Projects listing management page", () => {
).toHaveBeenCalledWith({ uuid: "123-123-123", name: "Some random API" });
});

it("can update a project in a modal", async () => {
it("can update a project in a modal by persisting a updated version of it in OPFS", async () => {
jest
.spyOn(OPFSProjectsListingService, "retrieveProjectsListing")
.mockResolvedValue({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as uuid from "uuid";
import {
retrieveProjectsListing,
persistNewProjectListingItem,
removeProjectListingItem,
updateProjectListingItem,
} from "./opfs-projects-listing-service";
import * as opfsAdapters from "../../services/origin-private-file-system";
import * as uuid from "uuid";

jest.mock("../../services/origin-private-file-system");

jest.mock("uuid");

jest.mock("../../services/origin-private-file-system");

describe("OPFS project listing service", () => {
beforeEach(() => {
jest.resetAllMocks();
Expand Down
Loading

0 comments on commit a0c0335

Please sign in to comment.