Skip to content

Commit 3d86605

Browse files
committed
test(promos): add unit tests for isPromoDateActive date range logic
Make today injectable via optional parameter for deterministic testing.
1 parent 427dbe1 commit 3d86605

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { isPromoDateActive } from "./types";
3+
import type { PromoData } from "./types";
4+
5+
const makePromo = (overrides: Partial<PromoData> = {}): PromoData => ({
6+
type: "banner",
7+
message: "Test promo",
8+
...overrides,
9+
});
10+
11+
describe("isPromoDateActive", () => {
12+
describe("no date range set", () => {
13+
test("returns true when neither startDate nor endDate is set", () => {
14+
expect(isPromoDateActive(makePromo(), "2026-04-28")).toBe(true);
15+
});
16+
});
17+
18+
describe("startDate only", () => {
19+
test("returns false before startDate", () => {
20+
expect(
21+
isPromoDateActive(makePromo({ startDate: "2026-04-29" }), "2026-04-28"),
22+
).toBe(false);
23+
});
24+
25+
test("returns true on startDate", () => {
26+
expect(
27+
isPromoDateActive(makePromo({ startDate: "2026-04-29" }), "2026-04-29"),
28+
).toBe(true);
29+
});
30+
31+
test("returns true after startDate", () => {
32+
expect(
33+
isPromoDateActive(makePromo({ startDate: "2026-04-29" }), "2026-05-01"),
34+
).toBe(true);
35+
});
36+
});
37+
38+
describe("endDate only", () => {
39+
test("returns true before endDate", () => {
40+
expect(
41+
isPromoDateActive(makePromo({ endDate: "2026-05-13" }), "2026-05-12"),
42+
).toBe(true);
43+
});
44+
45+
test("returns true on endDate", () => {
46+
expect(
47+
isPromoDateActive(makePromo({ endDate: "2026-05-13" }), "2026-05-13"),
48+
).toBe(true);
49+
});
50+
51+
test("returns false after endDate", () => {
52+
expect(
53+
isPromoDateActive(makePromo({ endDate: "2026-05-13" }), "2026-05-14"),
54+
).toBe(false);
55+
});
56+
});
57+
58+
describe("startDate and endDate", () => {
59+
const promo = makePromo({ startDate: "2026-04-29", endDate: "2026-05-13" });
60+
61+
test("returns false before window", () => {
62+
expect(isPromoDateActive(promo, "2026-04-28")).toBe(false);
63+
});
64+
65+
test("returns true on first day", () => {
66+
expect(isPromoDateActive(promo, "2026-04-29")).toBe(true);
67+
});
68+
69+
test("returns true mid-window", () => {
70+
expect(isPromoDateActive(promo, "2026-05-06")).toBe(true);
71+
});
72+
73+
test("returns true on last day", () => {
74+
expect(isPromoDateActive(promo, "2026-05-13")).toBe(true);
75+
});
76+
77+
test("returns false after window", () => {
78+
expect(isPromoDateActive(promo, "2026-05-14")).toBe(false);
79+
});
80+
});
81+
});

src/assets/data/promos/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ const routeMatchesAllowlist = (path: string, allowlist: string[]) =>
6666
allowlist.some((route) => path === route || path.startsWith(`${route}/`));
6767

6868
/** Returns false if today is outside the promo's startDate/endDate window. */
69-
export const isPromoDateActive = (promo: PromoData): boolean => {
70-
const today = new Date().toISOString().slice(0, 10);
69+
export const isPromoDateActive = (
70+
promo: PromoData,
71+
today = new Date().toISOString().slice(0, 10),
72+
): boolean => {
7173
if (promo.startDate && today < promo.startDate) return false;
7274
if (promo.endDate && today > promo.endDate) return false;
7375
return true;

0 commit comments

Comments
 (0)