Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jest jsongenerator #403

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const config = {
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
testEnvironment: "jest-environment-jsdom",

moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1", // Maps '@/' to the 'src' folder
},

// coverage init settings
// collectCoverage: true,
// coverageDirectory: "coverage",
Expand Down
1 change: 1 addition & 0 deletions src/app/customizer/JsonGenerator/components/Item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const Item = ({ field, handleChange, removeField, controls, categoryData }) => {
</select>
<button
onClick={() => removeField(field.id)}
aria-label="Remove Field"
className="text-gray-500 hover:text-red-400"
>
<CiCircleRemove
Expand Down
83 changes: 83 additions & 0 deletions src/app/customizer/JsonGenerator/tests/CardForm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import React from "react";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import CardForm from "../components/CardForm";
import { saveAs } from "file-saver";

// Mock external dependencies
jest.mock("file-saver", () => ({
saveAs: jest.fn(),
}));

// Test suite for CardForm Component
describe("CardForm Component", () => {
beforeEach(() => {
jest.clearAllMocks();
});

// Renders FieldsSection correctly
test("renders the FieldsSection component", () => {
render(<CardForm isDarkMode={false} />);
expect(screen.getByText("Field Name")).toBeInTheDocument();
expect(screen.getByText("Field Type")).toBeInTheDocument();
});

// Renders initial fields correctly
test("renders the initial fields from Init.jsx", () => {
render(<CardForm isDarkMode={false} />);
const allTextInputs = screen.getAllByRole("textbox");
const fieldNameInputs = allTextInputs.filter(
(input) => input.getAttribute("name") === "fieldName",
);
expect(fieldNameInputs.length).toBe(3);
const initialFieldNames = fieldNameInputs.map((input) => input.value);
expect(initialFieldNames).toEqual(["id", "first_name", "last_name"]);
});

// Adds a new field when button clicked
test('adds a new field when "ADD ANOTHER FIELD" button is clicked', () => {
render(<CardForm isDarkMode={false} />);
const addButton = screen.getByRole("button", {
name: /add another field/i,
});
fireEvent.click(addButton);
const fieldInputs = screen.getAllByRole("textbox");
expect(fieldInputs.length).toBeGreaterThan(1);
});

// Removes a field when remove button clicked
test("removes a field when the remove button is clicked", () => {
render(<CardForm isDarkMode={false} />);
const removeButtons = screen.getAllByRole("button", {
name: /remove field/i,
});
const initialFieldCount = removeButtons.length;
fireEvent.click(removeButtons[0]);
const updatedRemoveButtons = screen.queryAllByRole("button", {
name: /remove field/i,
});
expect(updatedRemoveButtons.length).toBe(initialFieldCount - 1);
});

// Generates JSON and downloads file
test('generates JSON data and downloads file when "Export" button is clicked', async () => {
render(<CardForm isDarkMode={false} />);
const generateButton = screen.getByRole("button", { name: /export/i });
fireEvent.click(generateButton);
await waitFor(() => {
expect(saveAs).toHaveBeenCalledTimes(1);
});
const filename = saveAs.mock.calls[0][1];
expect(filename).toBe("WebDevTools.json");
});

// Shows preview when "Preview" button clicked
test('shows preview when "Preview" button is clicked', async () => {
render(<CardForm isDarkMode={false} />);
const fieldTypeSelects = screen.getAllByRole("combobox");
fieldTypeSelects.forEach((select) => {
fireEvent.change(select, { target: { value: "int" } });
});
const modalHeading = await screen.findByText(/preview/i);
expect(modalHeading).toBeInTheDocument();
});
});
22 changes: 22 additions & 0 deletions src/app/customizer/JsonGenerator/tests/Heroish.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react";
import { render, screen } from "@testing-library/react";
import Heroish from "../components/Heroish";

// Test suite for Heroish Component
describe("Heroish Component", () => {
// Test that main heading renders
test("renders the main heading", () => {
render(<Heroish />);
const heading = screen.getByRole("heading", { name: "Json Generator" });
expect(heading).toBeInTheDocument();
});

// Test that description paragraph renders
test("renders the description paragraph", () => {
render(<Heroish />);
const descriptionText =
"Generate custom JSON data quickly and easily. Populate your applications with realistic-looking data for prototyping, testing, and more.";
const description = screen.getByText(descriptionText);
expect(description).toBeInTheDocument();
});
});
35 changes: 35 additions & 0 deletions src/app/customizer/JsonGenerator/tests/JsonGenerator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from "react";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import JsonGeneratorMain from "../page";

// Test suite for JsonGeneratorMain Component
describe("JsonGeneratorMain Component", () => {
// Test component renders and displays correct title
test("renders without crashing and displays correct title", () => {
render(<JsonGeneratorMain />);
const allTitleElements = screen.getAllByText("Json Generator");
expect(allTitleElements).toHaveLength(1);
});

// Test dark mode toggle functionality
test("passes isDarkMode prop correctly to child components", async () => {
render(<JsonGeneratorMain />);
const toggleButton = screen.getByRole("button", {
name: /toggle dark mode/i,
});
const mainElement = screen.getByRole("main");

// Initial state: light mode
expect(mainElement).toHaveClass("bg-white", "text-gray-800");

// Toggle dark mode
fireEvent.click(toggleButton);

await waitFor(() => {
const navBar = screen.getByRole("navigation");
expect(navBar).toHaveClass("bg-gray-800");
});

expect(mainElement).toHaveClass("bg-gray-900", "text-gray-400");
});
});
Loading