mirror of
https://github.com/audacity/audacity.github.io.git
synced 2026-05-31 06:47:16 -05:00
test(promos): add unit tests for isPromoDateActive date range logic
Make today injectable via optional parameter for deterministic testing.
This commit is contained in:
81
src/assets/data/promos/types.test.ts
Normal file
81
src/assets/data/promos/types.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user