Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/modules/LoggedIn/Project/ProjectList.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React from "react";
import ProjectList from "./ProjectList";
import { render, fireEvent, act } from "@testing-library/react";
import Request from "../../../utils/Request";

jest.mock("../../../utils/Request");

const RequestMock = Request as jest.MockedClass<typeof Request>;

Object.defineProperty(window, "matchMedia", {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});

describe("ProjectList", () => {
it("can show list cards", async () => {
RequestMock.mockImplementation(baseURL => ({
baseURL,
get: () =>
Promise.resolve([
{
_id: "gaejgea7g8hea",
name: "jatim park map",
description: "jatim park map description",
},
] as any),
delete: jest.fn(),
put: jest.fn(),
post: jest.fn(),
}));
const { findByText } = render(<ProjectList onEditClick={jest.fn()} />);
const cardName = await findByText("jatim park map");
const cardDescription = await findByText("jatim park map description");
expect(cardName).toBeInTheDocument();
expect(cardDescription).toBeInTheDocument();
});

it("can show succes notification if delete confirm accepted", async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coba tambahin satu case lagi ya, yang can show failed notification if delete failed
cara mock nya bisa kamu lihat di https://github.com/indent-dev/blueprint/blob/master/src/modules/LoggedIn/Project/ProjectForm.test.tsx#L94-L104

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mas mau nanya ini saya udah nambahin casenya tapi gagal soalnya findByTitle itu berlaku untuk satu case aja mas. terus saya rubah ke findAllByTitle dia gak mau juga mas.

RequestMock.mockImplementation(baseURL => ({
baseURL,
get: jest.fn(),
delete: () =>
Promise.resolve({
_id: "gaejgea7g8hea",
name: "jatim park map",
description: "jatim park map description",
} as any),
put: jest.fn(),
post: jest.fn(),
}));
const { findByTitle, findByText } = render(
<ProjectList onEditClick={jest.fn()} />
);
const cardDelete = await findByTitle("cardDelete");
act(() => {
fireEvent.click(cardDelete);
});
const acceptButton = await findByText("Yes");
act(() => {
fireEvent.click(acceptButton);
});
const succesDeleteNotification = await findByText("Project delete success");
expect(succesDeleteNotification).toBeInTheDocument();
});
});