| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import {
- act,
- assertEquals,
- cleanup,
- fireEvent,
- render,
- screen,
- } from "./setup.ts";
- import LoginFrame from "../../islands/LoginFrame.tsx";
- // Mock fetch to prevent actual network calls
- const originalFetch = globalThis.fetch;
- function mockFetch(response = { success: false }) {
- globalThis.fetch = () =>
- Promise.resolve(
- new Response(JSON.stringify(response), {
- headers: { "Content-Type": "application/json" },
- }),
- );
- }
- function restoreFetch() {
- globalThis.fetch = originalFetch;
- }
- // Mock $loading to prevent errors
- function mockLoading() {
- globalThis.$loading = { show: () => {}, hide: () => {} };
- }
- function restoreLoading() {
- delete globalThis.$loading;
- }
- Deno.test({
- name: "LoginFrame - login mode renders email and password inputs",
- async fn() {
- mockFetch();
- const { container } = render(<LoginFrame mode="login" />);
- // Wait for useEffect to settle
- await act(async () => {});
- assertEquals(screen.getByPlaceholderText("Your email") !== null, true);
- assertEquals(screen.getByPlaceholderText("Your password") !== null, true);
- cleanup();
- restoreFetch();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "LoginFrame - login mode renders Sign in button",
- async fn() {
- mockFetch();
- const { container } = render(<LoginFrame mode="login" />);
- await act(async () => {});
- assertEquals(screen.getByText("Sign in").tagName, "BUTTON");
- cleanup();
- restoreFetch();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "LoginFrame - login mode renders Go Register button",
- async fn() {
- mockFetch();
- render(<LoginFrame mode="login" />);
- await act(async () => {});
- assertEquals(screen.getByText("Go Register").tagName, "BUTTON");
- cleanup();
- restoreFetch();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "LoginFrame - login mode does not render confirm password",
- async fn() {
- mockFetch();
- render(<LoginFrame mode="login" />);
- await act(async () => {});
- const confirmInput = screen.queryByPlaceholderText("Confirm your password");
- assertEquals(confirmInput, null);
- cleanup();
- restoreFetch();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "LoginFrame - register mode renders confirm password input",
- fn() {
- mockFetch();
- render(<LoginFrame mode="register" />);
- assertEquals(
- screen.getByPlaceholderText("Confirm your password") !== null,
- true,
- );
- cleanup();
- restoreFetch();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "LoginFrame - register mode renders Register button",
- fn() {
- mockFetch();
- render(<LoginFrame mode="register" />);
- assertEquals(screen.getByText("Register").tagName, "BUTTON");
- cleanup();
- restoreFetch();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "LoginFrame - register mode renders Go Login button",
- fn() {
- mockFetch();
- render(<LoginFrame mode="register" />);
- assertEquals(screen.getByText("Go Login").tagName, "BUTTON");
- cleanup();
- restoreFetch();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "LoginFrame - submit with empty fields sets error state",
- async fn() {
- mockFetch();
- mockLoading();
- render(<LoginFrame mode="login" />);
- await act(async () => {});
- const submitBtn = screen.getByText("Sign in");
- await act(async () => {
- fireEvent.click(submitBtn);
- });
- // Check that error borders appear on empty inputs
- const emailInput = screen.getByPlaceholderText("Your email");
- const passwordInput = screen.getByPlaceholderText("Your password");
- assertEquals(emailInput.className.includes("border-red-600"), true);
- assertEquals(passwordInput.className.includes("border-red-600"), true);
- cleanup();
- restoreFetch();
- restoreLoading();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "LoginFrame - typing clears error state",
- async fn() {
- mockFetch();
- mockLoading();
- render(<LoginFrame mode="login" />);
- await act(async () => {});
- // Trigger error
- await act(async () => {
- fireEvent.click(screen.getByText("Sign in"));
- });
- const emailInput = screen.getByPlaceholderText("Your email");
- assertEquals(emailInput.className.includes("border-red-600"), true);
- // Type into the email input to clear error
- await act(async () => {
- fireEvent.input(emailInput, { target: { value: "test@email.com" } });
- });
- assertEquals(emailInput.className.includes("border-red-600"), false);
- cleanup();
- restoreFetch();
- restoreLoading();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
|