Files
co-lite/solvway-admin-portal/app/components/ui/date-time-picker.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

114 lines
4.8 KiB
TypeScript

import { useState } from "react";
import { describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { DateTimePicker } from "./date-time-picker";
import { pickDate } from "@/test/pickDate";
import { pickTime } from "@/test/pickTime";
function Harness({ initial = "", onChange = vi.fn() }: { initial?: string; onChange?: (v: string) => void }) {
const [value, setValue] = useState(initial);
return (
<>
<label htmlFor="sales-from">Sales from</label>
<DateTimePicker id="sales-from" timeLabel="Sales from time" value={value}
onChange={(v) => { setValue(v); onChange(v); }} />
<button type="button" onClick={() => setValue("")}>reset</button>
</>
);
}
async function openTimes() {
await userEvent.setup().click(screen.getByLabelText("Sales from time"));
return (await screen.findAllByRole("option")).map((o) => o.textContent);
}
describe("DateTimePicker", () => {
it("emits empty until both date and time are filled", async () => {
const onChange = vi.fn();
render(<Harness onChange={onChange} />);
pickDate("Sales from", "2026-10-01");
expect(onChange).toHaveBeenLastCalledWith("");
await pickTime("Sales from time", "09:30");
expect(onChange).toHaveBeenLastCalledWith("2026-10-01T09:30");
});
it("renders no native time input", () => {
const { container } = render(<Harness />);
expect(container.querySelector('input[type="time"]')).toBeNull();
});
it("lists 12-hour times in 15-minute steps, then 11:59 PM for end of day", async () => {
render(<Harness />);
const options = await openTimes();
expect(options).toHaveLength(97);
expect(options[0]).toBe("12:00 AM");
expect(options[1]).toBe("12:15 AM");
expect(options).toContain("9:15 AM");
expect(options[48]).toBe("12:00 PM");
expect(options[95]).toBe("11:45 PM");
expect(options[96]).toBe("11:59 PM");
});
it("marks the time box required (invalid) once a date is picked, so form validation can block", () => {
render(<Harness />);
expect(screen.getByLabelText("Sales from time")).not.toBeRequired();
pickDate("Sales from", "2026-10-01");
expect(screen.getByLabelText("Sales from time")).toBeRequired();
});
it("preloads both parts, tolerating seconds, without emitting on mount", () => {
const onChange = vi.fn();
render(<Harness initial="2026-10-01T09:30:00" onChange={onChange} />);
expect(screen.getByLabelText("Sales from")).toHaveValue("01/10/2026");
expect(screen.getByLabelText("Sales from time")).toHaveTextContent("9:30 AM");
expect(onChange).not.toHaveBeenCalled();
});
it("keeps an off-grid stored time, shown and offered, without emitting", async () => {
const onChange = vi.fn();
render(<Harness initial="2026-10-01T14:37" onChange={onChange} />);
expect(screen.getByLabelText("Sales from time")).toHaveTextContent("2:37 PM");
expect(await openTimes()).toContain("2:37 PM");
expect(onChange).not.toHaveBeenCalled();
});
it("keeps a half-filled entry when the value stays empty, and clears on external reset", async () => {
render(<Harness initial="2026-10-01T09:30" />);
fireEvent.click(screen.getByText("reset"));
expect(screen.getByLabelText("Sales from")).toHaveValue("");
expect(screen.getByLabelText("Sales from time")).toHaveTextContent("Time");
pickDate("Sales from", "2026-10-02");
expect(screen.getByLabelText("Sales from")).toHaveValue("02/10/2026");
});
it("applies required to both boxes and gives the time box the -time id", () => {
render(<DateTimePicker id="x" required value="" onChange={() => {}} />);
expect(document.getElementById("x")).toBeRequired();
expect(document.getElementById("x-time")).toBeRequired();
});
it("says 'Enter a time' while a date has no time, and stops once the time is set", async () => {
render(<Harness />);
expect(screen.queryByText("Enter a time")).not.toBeInTheDocument();
pickDate("Sales from", "2026-10-01");
expect(screen.getByText("Enter a time")).toBeInTheDocument();
await pickTime("Sales from time", "09:30");
expect(screen.queryByText("Enter a time")).not.toBeInTheDocument();
});
it("passes aria-invalid and aria-describedby to the date box", () => {
render(
<>
<label htmlFor="ends">Ends</label>
<DateTimePicker id="ends" timeLabel="Ends time" value="" onChange={vi.fn()} aria-invalid aria-describedby="ends-error" />
<p id="ends-error">Set an end date and time.</p>
</>,
);
const box = screen.getByLabelText("Ends");
expect(box).toHaveAttribute("aria-invalid", "true");
expect(box).toHaveAttribute("aria-describedby", "ends-error");
expect(box).toHaveAccessibleDescription("Set an end date and time.");
});
});