|
1 | 1 | import React from "react";
|
2 |
| -import { render, screen } from "@testing-library/react"; |
| 2 | +import { render, screen, fireEvent, waitFor } from "@testing-library/react"; |
3 | 3 | import App from "./App";
|
| 4 | +import User from "./components/User/page.jsx"; |
4 | 5 |
|
| 6 | +// App component test |
5 | 7 | test("renders main components of Resume Builder", () => {
|
6 | 8 | render(<App />);
|
7 | 9 |
|
@@ -30,3 +32,86 @@ test("renders main components of Resume Builder", () => {
|
30 | 32 | ).toBeInTheDocument();
|
31 | 33 | expect(screen.getByRole("button", { name: /Export/i })).toBeInTheDocument();
|
32 | 34 | });
|
| 35 | + |
| 36 | +// User component test |
| 37 | +describe("User Component", () => { |
| 38 | + test("renders Add User button", () => { |
| 39 | + render(<User />); |
| 40 | + const addButton = screen.getByRole("button", { name: /Add User/i }); |
| 41 | + expect(addButton).toBeInTheDocument(); |
| 42 | + }); |
| 43 | + |
| 44 | + test("opens modal when Add User button is clicked", async () => { |
| 45 | + render(<User />); |
| 46 | + const addButton = screen.getByRole("button", { name: /Add User/i }); |
| 47 | + |
| 48 | + fireEvent.click(addButton); |
| 49 | + |
| 50 | + await waitFor(() => { |
| 51 | + expect(screen.getByRole("dialog")).toBeInTheDocument(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + test("closes modal when close button is clicked", async () => { |
| 56 | + render(<User />); |
| 57 | + const addButton = screen.getByRole("button", { name: /Add User/i }); |
| 58 | + |
| 59 | + fireEvent.click(addButton); |
| 60 | + |
| 61 | + await waitFor(() => { |
| 62 | + const closeButton = screen.getByRole("button", { name: /×/i }); |
| 63 | + fireEvent.click(closeButton); |
| 64 | + expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + test("closes modal when clicking outside", async () => { |
| 69 | + render(<User />); |
| 70 | + const addButton = screen.getByRole("button", { name: /Add User/i }); |
| 71 | + |
| 72 | + fireEvent.click(addButton); |
| 73 | + |
| 74 | + await waitFor(() => { |
| 75 | + const modal = screen.getByRole("dialog"); |
| 76 | + fireEvent.mouseDown(document.body); |
| 77 | + expect(modal).not.toBeInTheDocument(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + test("handles form input and submission", async () => { |
| 82 | + render(<User />); |
| 83 | + const addButton = screen.getByRole("button", { name: /Add User/i }); |
| 84 | + |
| 85 | + fireEvent.click(addButton); |
| 86 | + |
| 87 | + await waitFor(() => { |
| 88 | + const nameInput = screen.getByPlaceholderText(/name/i); |
| 89 | + const phoneInput = screen.getByPlaceholderText(/phone/i); |
| 90 | + const emailInput = screen.getByPlaceholderText(/email/i); |
| 91 | + const submitButton = screen.getByRole("button", { name: /save/i }); |
| 92 | + |
| 93 | + fireEvent.change(nameInput, { target: { value: "John Doe" } }); |
| 94 | + fireEvent.change(phoneInput, { target: { value: "+2343456890" } }); |
| 95 | + fireEvent.change(emailInput, { |
| 96 | + target: { value: "[email protected]" }, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(nameInput).toHaveValue("John Doe"); |
| 100 | + expect(phoneInput).toHaveValue("+2343456890"); |
| 101 | + expect(emailInput).toHaveValue("[email protected]"); |
| 102 | + |
| 103 | + const consoleSpy = jest.spyOn(console, "log"); |
| 104 | + fireEvent.click(submitButton); |
| 105 | + |
| 106 | + expect(consoleSpy).toHaveBeenCalledWith({ |
| 107 | + name: "John Doe", |
| 108 | + phone_number: "+2343456890", |
| 109 | + |
| 110 | + }); |
| 111 | + consoleSpy.mockRestore(); |
| 112 | + |
| 113 | + expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
0 commit comments