Files
co-lite/solvway-admin-portal/app/components/locations/LocationTree.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

48 lines
2.3 KiB
TypeScript

import { render, screen, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import { LocationTree } from "./LocationTree";
import type { AdminLocationNode } from "@/app/lib/types";
const nodes: AdminLocationNode[] = [
{ id: 1, code: "GLOBAL", name: "Global", type: "G", status: "Y", parent: null },
{ id: 2, code: "MY", name: "Malaysia", type: "C", status: "Y", parent: { id: 1 } },
{ id: 21, code: "WM", name: "West Malaysia", type: "R", status: "Y", parent: { id: 2 } },
{ id: 35, code: "MYB", name: "Kuala Lumpur", type: "B", status: "Y", parent: { id: 21 } },
];
describe("LocationTree", () => {
it("renders the tree root and marks it as a tree", () => {
render(<LocationTree nodes={nodes} selectedId={null} onSelect={vi.fn()} />);
expect(screen.getByRole("tree", { name: "Locations" })).toBeInTheDocument();
expect(screen.getByRole("treeitem", { name: /Global/ })).toBeInTheDocument();
});
it("only shows children of an expanded node", () => {
render(<LocationTree nodes={nodes} selectedId={null} onSelect={vi.fn()} />);
// Nodes start expanded (the business tree is small); collapse Global so the
// hide/show semantics of expand/collapse are exercised explicitly.
fireEvent.click(screen.getByLabelText(/Collapse Global/));
expect(screen.queryByText("Malaysia")).not.toBeInTheDocument();
fireEvent.click(screen.getByLabelText(/Expand Global/));
expect(screen.getByText("Malaysia")).toBeInTheDocument();
});
it("calls onSelect with the node id when a row is clicked", () => {
const onSelect = vi.fn();
render(<LocationTree nodes={nodes} selectedId={null} onSelect={onSelect} />);
fireEvent.click(screen.getByText("Global"));
expect(onSelect).toHaveBeenCalledWith(1);
});
it("marks the selected node with aria-selected", () => {
render(<LocationTree nodes={nodes} selectedId={1} onSelect={vi.fn()} />);
expect(screen.getByRole("treeitem", { name: /Global/ })).toHaveAttribute("aria-selected", "true");
});
it("filters to matching nodes and their ancestors when searchTerm is set", () => {
render(<LocationTree nodes={nodes} selectedId={null} onSelect={vi.fn()} searchTerm="kuala" />);
// Ancestors of a match are auto-expanded so the match is reachable.
expect(screen.getByText("Kuala Lumpur")).toBeInTheDocument();
});
});