/**
* QuickCaptureWidget Component Tests
* Following TDD principles
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { QuickCaptureWidget } from "../QuickCaptureWidget";
global.fetch = vi.fn() as typeof global.fetch;
describe("QuickCaptureWidget", (): void => {
beforeEach((): void => {
vi.clearAllMocks();
});
it("should render input field", (): void => {
render();
expect(screen.getByRole("textbox")).toBeInTheDocument();
});
it("should render submit button", (): void => {
render();
expect(screen.getByRole("button", { name: /add|capture|submit/i })).toBeInTheDocument();
});
it("should allow text input", async (): Promise => {
const user = userEvent.setup();
render();
const input = screen.getByRole("textbox");
await user.type(input, "Quick note for later");
expect(input).toHaveValue("Quick note for later");
});
it("should submit note when button clicked", async (): Promise => {
const user = userEvent.setup();
vi.mocked(global.fetch).mockResolvedValueOnce({
ok: true,
json: () => ({ success: true }),
});
render();
const input = screen.getByRole("textbox");
const button = screen.getByRole("button", { name: /add|capture|submit/i });
await user.type(input, "New quick note");
await user.click(button);
await waitFor(() => {
expect(global.fetch).toHaveBeenCalledWith(
expect.stringContaining("/api"),
expect.objectContaining({
method: "POST",
})
);
});
});
it("should clear input after successful submission", async (): Promise => {
const user = userEvent.setup();
vi.mocked(global.fetch).mockResolvedValueOnce({
ok: true,
json: () => ({ success: true }),
});
render();
const input = screen.getByRole("textbox");
const button = screen.getByRole("button", { name: /add|capture|submit/i });
await user.type(input, "Test note");
await user.click(button);
await waitFor(() => {
expect(input).toHaveValue("");
});
});
it("should handle submission errors", async (): Promise => {
const user = userEvent.setup();
vi.mocked(global.fetch).mockRejectedValueOnce(new Error("API Error"));
render();
const input = screen.getByRole("textbox");
const button = screen.getByRole("button", { name: /add|capture|submit/i });
await user.type(input, "Test note");
await user.click(button);
await waitFor(() => {
expect(screen.getByText(/error|failed/i)).toBeInTheDocument();
});
});
it("should not submit empty notes", async (): Promise => {
const user = userEvent.setup();
render();
const button = screen.getByRole("button", { name: /add|capture|submit/i });
await user.click(button);
expect(global.fetch).not.toHaveBeenCalled();
});
it("should support keyboard shortcut (Enter)", async (): Promise => {
const user = userEvent.setup();
vi.mocked(global.fetch).mockResolvedValueOnce({
ok: true,
json: () => ({ success: true }),
});
render();
const input = screen.getByRole("textbox");
await user.type(input, "Quick note{Enter}");
await waitFor(() => {
expect(global.fetch).toHaveBeenCalled();
});
});
it("should show success feedback after submission", async (): Promise => {
const user = userEvent.setup();
vi.mocked(global.fetch).mockResolvedValueOnce({
ok: true,
json: () => ({ success: true }),
});
render();
const input = screen.getByRole("textbox");
const button = screen.getByRole("button", { name: /add|capture|submit/i });
await user.type(input, "Test note");
await user.click(button);
await waitFor(() => {
expect(screen.getByText(/success|saved|captured/i)).toBeInTheDocument();
});
});
});