Merge pull request #609 from ericv105/main

85: Unit-testing nextjs & react code
This commit is contained in:
Keith Stevens
2023-01-12 11:46:18 +09:00
committed by GitHub
13 changed files with 10893 additions and 4 deletions
-13
View File
@@ -1,13 +0,0 @@
import React from "react";
import { Container } from "./Container";
describe("<Container />", () => {
it("renders", () => {
// see: https://on.cypress.io/mounting-react
const className = "my-class";
const text = "test_container";
cy.mount(<Container className={className}>{text}</Container>);
cy.get(`div.${className}`).should("have.class", className).should("be.visible").should("contain", text);
});
});
@@ -0,0 +1,34 @@
import { fireEvent, render, screen } from "@testing-library/react";
import { RouterContext } from "next/dist/shared/lib/router-context";
import React from "react";
import { createMockRouter } from "src/test-utils/createMockRouter";
import { OptionProps, TaskOption } from "./TaskOption";
describe("TaskOption component", () => {
const testProps: OptionProps = {
title: "Task Title",
alt: "Task Title",
img: "/imgPath",
link: "/fake/path",
};
it("should render", () => {
render(<TaskOption {...testProps} />);
expect(screen.getByRole("heading")).toHaveTextContent("Task Title");
expect(screen.getByRole("img")).toHaveAttribute("alt", "Task Title");
expect(screen.getByRole("link")).toHaveAttribute("href", "/fake/path");
});
it("should navigate properly on click", () => {
const router = createMockRouter({ pathname: "/fake/path" });
render(
<RouterContext.Provider value={router}>
<TaskOption {...testProps} />
</RouterContext.Provider>
);
expect(screen.getByRole("link")).toHaveAttribute("href", "/fake/path");
fireEvent.click(screen.getByRole("link"));
expect(router.push).toHaveBeenCalledWith("/fake/path", expect.anything(), expect.anything());
});
});