Files
pleno-vue/tests/unit/stripe-terminal-readers.spec.js
T

68 lines
3.4 KiB
JavaScript

import { describe, expect, it } from "vitest";
import {
getStripeTerminalStatusKey,
getStripeTerminalReaderById,
normalizeStripeTerminalReaders,
selectPreferredStripeTerminalReaderId,
STRIPE_TERMINAL_STATUS,
} from "@/components/displays/department/pos/displays/stripeTerminalReaders.js";
describe("stripeTerminalReaders", () => {
it("maps raw Stripe reader status into ready, in-use, and offline states", () => {
expect(getStripeTerminalStatusKey({ status: "online", action: null })).toBe(STRIPE_TERMINAL_STATUS.ready);
expect(getStripeTerminalStatusKey({ status: "busy", action: null })).toBe(STRIPE_TERMINAL_STATUS.inUse);
expect(getStripeTerminalStatusKey({ status: "online", action: { type: "process_payment_intent" } })).toBe(
STRIPE_TERMINAL_STATUS.inUse
);
expect(getStripeTerminalStatusKey({ status: "offline", action: null })).toBe(STRIPE_TERMINAL_STATUS.offline);
expect(getStripeTerminalStatusKey({ status: "unknown_state", action: null })).toBe(STRIPE_TERMINAL_STATUS.offline);
});
it("normalizes readers, keeps blocked readers visible, and sorts them by status priority", () => {
const normalizedReaders = normalizeStripeTerminalReaders([
{ id: "offline_terminal", label: "Offline terminal", status: "offline", action: null },
{ id: "busy_terminal", label: "Busy terminal", status: "online", action: { type: "payment_intent" } },
{ id: "ready_terminal", label: "Ready terminal", status: "online", action: null },
]);
expect(normalizedReaders.map((reader) => reader.id)).toEqual([
"ready_terminal",
"busy_terminal",
"offline_terminal",
]);
expect(normalizedReaders.map((reader) => reader.selectable)).toEqual([true, false, false]);
expect(normalizedReaders.map((reader) => reader.colorToken)).toEqual(["green", "yellow", "red"]);
});
it("keeps the current ready reader selected when it remains ready", () => {
const normalizedReaders = normalizeStripeTerminalReaders([
{ id: "ready_terminal_a", label: "Ready terminal A", status: "online", action: null },
{ id: "ready_terminal_b", label: "Ready terminal B", status: "online", action: null },
]);
expect(selectPreferredStripeTerminalReaderId(normalizedReaders, "ready_terminal_b")).toBe("ready_terminal_b");
});
it("falls back to the first ready reader when the current reader is no longer selectable", () => {
const normalizedReaders = normalizeStripeTerminalReaders([
{ id: "busy_terminal", label: "Busy terminal", status: "busy", action: null },
{ id: "ready_terminal", label: "Ready terminal", status: "online", action: null },
{ id: "offline_terminal", label: "Offline terminal", status: "offline", action: null },
]);
expect(selectPreferredStripeTerminalReaderId(normalizedReaders, "busy_terminal")).toBe("ready_terminal");
expect(getStripeTerminalReaderById(normalizedReaders, "offline_terminal")?.statusKey).toBe(
STRIPE_TERMINAL_STATUS.offline
);
});
it("clears the selection when no ready readers remain", () => {
const normalizedReaders = normalizeStripeTerminalReaders([
{ id: "busy_terminal", label: "Busy terminal", status: "online", action: { type: "payment_intent" } },
{ id: "offline_terminal", label: "Offline terminal", status: "offline", action: null },
]);
expect(selectPreferredStripeTerminalReaderId(normalizedReaders, "busy_terminal")).toBe("");
});
});