|
| 1 | +import { render, screen, fireEvent } from "@testing-library/react"; |
| 2 | + |
| 3 | +import Login from "../Login"; |
| 4 | +import { expect } from "vitest"; |
| 5 | + |
| 6 | +describe("Login Component", () => { |
| 7 | + it("renders the login form", () => { |
| 8 | + render(<Login />); |
| 9 | + expect(screen.getByLabelText(/username/i)).toBeInTheDocument(); |
| 10 | + expect(screen.getByLabelText(/password/i)).toBeInTheDocument(); |
| 11 | + expect(screen.getByRole("button", { name: /登录/i })).toBeInTheDocument(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("updates the username and password fields", () => { |
| 15 | + render(<Login />); |
| 16 | + const usernameInput = screen.getByLabelText(/username/i); |
| 17 | + const passwordInput = screen.getByLabelText(/password/i); |
| 18 | + |
| 19 | + fireEvent.change(usernameInput, { target: { value: "testuser" } }); |
| 20 | + fireEvent.change(passwordInput, { target: { value: "password123" } }); |
| 21 | + |
| 22 | + expect(usernameInput.value).toBe("testuser"); |
| 23 | + expect(passwordInput.value).toBe("password123"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("handles form submission", () => { |
| 27 | + render(<Login />); |
| 28 | + const usernameInput = screen.getByLabelText(/username/i); |
| 29 | + const passwordInput = screen.getByLabelText(/password/i); |
| 30 | + const button = screen.getByRole("button", { name: /登录/i }); |
| 31 | + |
| 32 | + const spyConsole = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 33 | + |
| 34 | + fireEvent.change(usernameInput, { target: { value: "testuser" } }); |
| 35 | + fireEvent.change(passwordInput, { target: { value: "password123" } }); |
| 36 | + |
| 37 | + fireEvent.click(button); |
| 38 | + |
| 39 | + expect(usernameInput.value).toBe("testuser"); |
| 40 | + expect(passwordInput.value).toBe("password123"); |
| 41 | + |
| 42 | + expect(spyConsole).toHaveBeenCalledTimes(2); |
| 43 | + }); |
| 44 | +}); |
0 commit comments