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

54 lines
2.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { ApiError } from "@/app/lib/clientApi";
const ship = vi.hoisted(() => ({ listRateCards: vi.fn(), calculateShipping: vi.fn() }));
vi.mock("@/app/lib/services/adminShipping", () => ship);
const api = vi.hoisted(() => ({ apiGet: vi.fn() }));
vi.mock("@/app/lib/clientApi", async (orig) => ({ ...(await orig<typeof import("@/app/lib/clientApi")>()), apiGet: api.apiGet }));
import { CalculatorTab } from "./CalculatorTab";
beforeEach(() => {
ship.listRateCards.mockReset().mockResolvedValue([{ id: 3, name: "2026 rates", effectiveFrom: "2026-11-01T00:00:00", status: "DRAFT" }]);
ship.calculateShipping.mockReset().mockResolvedValue({ zoneCode: "WM", billableWeightG: 2000, fee: 11, rateCardId: 3 });
api.apiGet.mockReset().mockImplementation(async (path: string) =>
path === "/api/geo-locations" ? [{ id: 5, name: "Malaysia" }] : [{ id: 50, name: "Selangor" }]);
});
async function chooseDestination() {
await userEvent.click(screen.getByRole("combobox", { name: "Country" }));
await userEvent.click(await screen.findByRole("option", { name: "Malaysia" }));
await waitFor(() => expect(screen.getByRole("combobox", { name: "State" })).not.toBeDisabled());
await userEvent.click(screen.getByRole("combobox", { name: "State" }));
await userEvent.click(await screen.findByRole("option", { name: "Selangor" }));
}
describe("CalculatorTab (add-shipping-rates)", () => {
it("calculates on a chosen draft card and shows zone, billable weight and fee", async () => {
render(<CalculatorTab />);
await userEvent.click(screen.getByRole("combobox", { name: "Rate card" }));
await userEvent.click(await screen.findByRole("option", { name: "2026 rates (DRAFT)" }));
await chooseDestination();
fireEvent.change(screen.getByLabelText("Weight (kg)"), { target: { value: "2" } });
fireEvent.change(screen.getByLabelText("Length (cm)"), { target: { value: "10" } });
fireEvent.click(screen.getByRole("button", { name: "Calculate" }));
expect(await screen.findByTestId("calc-result")).toHaveTextContent("Zone WM · billable 2.000 kg · fee RM11.00");
expect(ship.calculateShipping).toHaveBeenCalledWith({
rateCardId: 3, geoLocationId: "50", weightKg: 2, lengthCm: 10, widthCm: 0, heightCm: 0,
});
});
it("uses the active card when none is chosen and shows a refusal", async () => {
ship.calculateShipping.mockRejectedValue(new ApiError("We don't deliver to this address yet", 422));
render(<CalculatorTab />);
await chooseDestination();
fireEvent.click(screen.getByRole("button", { name: "Calculate" }));
expect(await screen.findByRole("alert")).toHaveTextContent("We don't deliver to this address yet");
expect(ship.calculateShipping.mock.calls[0][0]).not.toHaveProperty("rateCardId");
});
});