import { describe, expect, it } from "vitest"; import { buildSwatchAccessibleName, getSwatchI18n } from "./swatchLabel"; describe("buildSwatchAccessibleName", () => { it("returns the plain label when the option is in stock", () => { expect(buildSwatchAccessibleName("Racing Green", false)).toBe( "Racing Green", ); }); it("appends the out-of-stock suffix when the option is unavailable", () => { expect(buildSwatchAccessibleName("Racing Green", true)).toBe( "Racing Green (Out of stock)", ); }); it("trims surrounding whitespace from the label", () => { expect(buildSwatchAccessibleName(" Blue ", true)).toBe( "Blue (Out of stock)", ); }); it("falls back to the bare suffix when there is no label", () => { expect(buildSwatchAccessibleName("", true)).toBe("Out of stock"); }); }); describe("getSwatchI18n", () => { it("prefers the localized string from the payload", () => { expect( getSwatchI18n("out_of_stock", "Out of stock", { i18n: { out_of_stock: "Rupture de stock" }, }), ).toBe("Rupture de stock"); }); it("falls back when the payload has no i18n bag", () => { expect(getSwatchI18n("out_of_stock", "Out of stock", {})).toBe( "Out of stock", ); }); it("falls back when the localized value is blank or non-string", () => { expect( getSwatchI18n("out_of_stock", "Out of stock", { i18n: { out_of_stock: " " }, }), ).toBe("Out of stock"); expect( getSwatchI18n("out_of_stock", "Out of stock", { i18n: { out_of_stock: 42 }, }), ).toBe("Out of stock"); }); });