supersam/src/services/orderGroupViews.test.js

119 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from "vitest";
import {
buildOrderGroupBuckets,
filterOrderGroups,
getOrderGroupDisplayStatusLabel,
getOrderGroupDisplayStatusValue,
getOrderGroupDeliveryStatusLabel,
getOrderGroupDeliveryHalfDay,
getOrderGroupStatusTone,
ORDER_GROUP_DISPLAY_STATUS_OPTIONS,
groupOrderGroupsByDate,
isOrderGroupAgreedForDelivery,
isOrderGroupVisibleToDriver,
} from "./orderGroupViews";
describe("orderGroupViews", () => {
const groups = [
{
id: "g-1",
customerDate: "14.04.26",
customerName: "А",
customerPhone: "1",
deliveryTime: "До обеда",
orderNumbers: ["A-1"],
status: "ready_for_notification",
deliveryStatus: "agreed",
smsSentAt: null,
ordersCount: 1,
readyCount: 1,
notReadyCount: 0,
createdAt: "2026-05-05T10:00:00Z",
updatedAt: "2026-05-05T10:00:00Z",
},
{
id: "g-2",
customerDate: "14.04.26",
customerName: "B",
customerPhone: "2",
deliveryStatus: "delivered",
sourceOrders: [{ delivery_time: "После обеда" }],
orderNumbers: ["B-1", "B-2"],
status: "ready_for_notification",
smsSentAt: "2026-05-05T12:00:00Z",
ordersCount: 2,
readyCount: 2,
notReadyCount: 0,
createdAt: "2026-05-05T11:00:00Z",
updatedAt: "2026-05-05T11:00:00Z",
},
{
id: "g-3",
customerDate: "15.04.26",
customerName: "C",
customerPhone: "3",
deliveryStatus: "pending_confirmation",
orderNumbers: ["C-1"],
status: "draft",
smsSentAt: null,
ordersCount: 1,
readyCount: 0,
notReadyCount: 1,
createdAt: "2026-05-05T13:00:00Z",
updatedAt: "2026-05-05T13:00:00Z",
},
];
it("groups delivery groups by customer date", () => {
const grouped = groupOrderGroupsByDate(groups);
expect(grouped).toHaveLength(2);
expect(grouped[0].date).toBe("14.04.26");
expect(grouped[0].items).toHaveLength(2);
expect(grouped[1].date).toBe("15.04.26");
});
it("builds readiness buckets from group status and sms timestamp", () => {
const buckets = buildOrderGroupBuckets(groups);
expect(buckets.ready_to_launch).toHaveLength(1);
expect(buckets.sms_sent).toHaveLength(1);
expect(buckets.manual_work).toHaveLength(1);
});
it("normalizes delivery half day and filters by date and time", () => {
expect(getOrderGroupDeliveryHalfDay(groups[0])).toBe("Первая половина дня");
expect(getOrderGroupDeliveryHalfDay(groups[1])).toBe("Вторая половина дня");
expect(getOrderGroupDeliveryStatusLabel(groups[0].deliveryStatus)).toBe("Согласовано");
expect(isOrderGroupVisibleToDriver(groups[0])).toBe(true);
expect(isOrderGroupAgreedForDelivery(groups[0])).toBe(true);
expect(isOrderGroupVisibleToDriver(groups[2])).toBe(false);
const filtered = filterOrderGroups(groups, {
dateFrom: "2026-04-14",
dateTo: "2026-04-14",
deliveryHalfDay: "morning",
deliveryStatus: "agreed",
});
expect(filtered).toHaveLength(1);
expect(filtered[0].id).toBe("g-1");
});
it("shows agreed delivery status as the visible card status", () => {
expect(getOrderGroupDisplayStatusLabel(groups[0])).toBe("Согласовано");
expect(getOrderGroupDisplayStatusValue(groups[0])).toBe("delivery:agreed");
expect(getOrderGroupStatusTone(groups[0])).toBe("accent");
expect(getOrderGroupDisplayStatusLabel(groups[2])).toBe("draft");
});
it("filters by the visible card status", () => {
const agreedGroups = filterOrderGroups(groups, { displayStatus: "delivery:agreed" });
const deliveredGroups = filterOrderGroups(groups, { displayStatus: "delivery:delivered" });
expect(ORDER_GROUP_DISPLAY_STATUS_OPTIONS.map((option) => option.label)).toContain("Согласовано");
expect(agreedGroups.map((group) => group.id)).toEqual(["g-1"]);
expect(deliveredGroups.map((group) => group.id)).toEqual(["g-2"]);
});
});