|
| 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 | +}); |
0 commit comments