test(promos): add unit tests for isPromoDateActive date range logic

Make today injectable via optional parameter for deterministic testing.
This commit is contained in:
Teetow
2026-04-28 07:12:34 +02:00
parent 427dbe12c8
commit 3d86605aaf
2 changed files with 85 additions and 2 deletions

View File

@@ -0,0 +1,81 @@
import { describe, expect, test } from "bun:test";
import { isPromoDateActive } from "./types";
import type { PromoData } from "./types";
const makePromo = (overrides: Partial<PromoData> = {}): PromoData => ({
type: "banner",
message: "Test promo",
...overrides,
});
describe("isPromoDateActive", () => {
describe("no date range set", () => {
test("returns true when neither startDate nor endDate is set", () => {
expect(isPromoDateActive(makePromo(), "2026-04-28")).toBe(true);
});
});
describe("startDate only", () => {
test("returns false before startDate", () => {
expect(
isPromoDateActive(makePromo({ startDate: "2026-04-29" }), "2026-04-28"),
).toBe(false);
});
test("returns true on startDate", () => {
expect(
isPromoDateActive(makePromo({ startDate: "2026-04-29" }), "2026-04-29"),
).toBe(true);
});
test("returns true after startDate", () => {
expect(
isPromoDateActive(makePromo({ startDate: "2026-04-29" }), "2026-05-01"),
).toBe(true);
});
});
describe("endDate only", () => {
test("returns true before endDate", () => {
expect(
isPromoDateActive(makePromo({ endDate: "2026-05-13" }), "2026-05-12"),
).toBe(true);
});
test("returns true on endDate", () => {
expect(
isPromoDateActive(makePromo({ endDate: "2026-05-13" }), "2026-05-13"),
).toBe(true);
});
test("returns false after endDate", () => {
expect(
isPromoDateActive(makePromo({ endDate: "2026-05-13" }), "2026-05-14"),
).toBe(false);
});
});
describe("startDate and endDate", () => {
const promo = makePromo({ startDate: "2026-04-29", endDate: "2026-05-13" });
test("returns false before window", () => {
expect(isPromoDateActive(promo, "2026-04-28")).toBe(false);
});
test("returns true on first day", () => {
expect(isPromoDateActive(promo, "2026-04-29")).toBe(true);
});
test("returns true mid-window", () => {
expect(isPromoDateActive(promo, "2026-05-06")).toBe(true);
});
test("returns true on last day", () => {
expect(isPromoDateActive(promo, "2026-05-13")).toBe(true);
});
test("returns false after window", () => {
expect(isPromoDateActive(promo, "2026-05-14")).toBe(false);
});
});
});

View File

@@ -66,8 +66,10 @@ const routeMatchesAllowlist = (path: string, allowlist: string[]) =>
allowlist.some((route) => path === route || path.startsWith(`${route}/`));
/** Returns false if today is outside the promo's startDate/endDate window. */
export const isPromoDateActive = (promo: PromoData): boolean => {
const today = new Date().toISOString().slice(0, 10);
export const isPromoDateActive = (
promo: PromoData,
today = new Date().toISOString().slice(0, 10),
): boolean => {
if (promo.startDate && today < promo.startDate) return false;
if (promo.endDate && today > promo.endDate) return false;
return true;