diff --git a/src/seqta/utils/Openers/OpenCoursesAssessmentsFixPopup.test.ts b/src/seqta/utils/Openers/OpenCoursesAssessmentsFixPopup.test.ts index 6826361a..492775bf 100644 --- a/src/seqta/utils/Openers/OpenCoursesAssessmentsFixPopup.test.ts +++ b/src/seqta/utils/Openers/OpenCoursesAssessmentsFixPopup.test.ts @@ -18,7 +18,7 @@ describe("shouldShowCoursesAssessmentsFixPopup", () => { ).toBe(true); }); - it("does not show when BetterSEQTA features are on unless What's New just closed", () => { + it("does not show when BetterSEQTA features are on", () => { expect( shouldShowCoursesAssessmentsFixPopup({ onoff: true, @@ -27,18 +27,6 @@ describe("shouldShowCoursesAssessmentsFixPopup", () => { ).toBe(false); }); - it("shows after What's New even when BetterSEQTA features are already on", () => { - expect( - shouldShowCoursesAssessmentsFixPopup( - { - onoff: true, - coursesAssessmentsFixPopupShown: false, - }, - { afterWhatsNew: true }, - ), - ).toBe(true); - }); - it("does not show again after the user has seen it", () => { expect( shouldShowCoursesAssessmentsFixPopup( @@ -46,7 +34,6 @@ describe("shouldShowCoursesAssessmentsFixPopup", () => { onoff: false, coursesAssessmentsFixPopupShown: true, }, - { afterWhatsNew: true }, ), ).toBe(false); }); diff --git a/src/seqta/utils/Openers/OpenCoursesAssessmentsFixPopup.ts b/src/seqta/utils/Openers/OpenCoursesAssessmentsFixPopup.ts index 52b6bb05..c14cc5a0 100644 --- a/src/seqta/utils/Openers/OpenCoursesAssessmentsFixPopup.ts +++ b/src/seqta/utils/Openers/OpenCoursesAssessmentsFixPopup.ts @@ -12,10 +12,8 @@ export type CoursesAssessmentsFixPopupState = { export function shouldShowCoursesAssessmentsFixPopup( state: CoursesAssessmentsFixPopupState = settingsState, - options?: { afterWhatsNew?: boolean }, ): boolean { if (state.coursesAssessmentsFixPopupShown) return false; - if (options?.afterWhatsNew) return true; return state.onoff === false; } @@ -238,13 +236,12 @@ function setPopupButtonsDisabled(disabled: boolean) { export function OpenCoursesAssessmentsFixPopup( onDismissed?: () => void, - options?: { afterWhatsNew?: boolean }, ) { if (document.getElementById("whatsnewbk")) { onDismissed?.(); return; } - if (!shouldShowCoursesAssessmentsFixPopup(settingsState, options)) { + if (!shouldShowCoursesAssessmentsFixPopup(settingsState)) { onDismissed?.(); return; } diff --git a/src/seqta/utils/Openers/OpenThemeOfTheMonthPopup.test.ts b/src/seqta/utils/Openers/OpenThemeOfTheMonthPopup.test.ts new file mode 100644 index 00000000..ea8e9883 --- /dev/null +++ b/src/seqta/utils/Openers/OpenThemeOfTheMonthPopup.test.ts @@ -0,0 +1,96 @@ +/** @jest-environment jsdom */ +import { OpenThemeOfTheMonthPopup } from "./OpenThemeOfTheMonthPopup"; +import { settingsState } from "../listeners/SettingsState"; + +jest.mock("../DevApiBase", () => ({ + getApiBase: () => "https://example.test", +})); +jest.mock("./PopupManager", () => ({ closePopup: jest.fn() })); +jest.mock("../listeners/SettingsState", () => ({ + settingsState: { animations: false }, +})); + +const entry = { + id: "totm-1", + month: "2026-10", + title: "October Theme", + description: "Preview", + cover_image: "https://example.test/cover.png", + theme_id: null, + theme: null, + created_at: 1, + updated_at: 1, +}; + +beforeEach(() => { + jest.useFakeTimers(); + settingsState.themeOfTheMonthDismissedMonth = undefined; + document.body.innerHTML = ""; +}); + +afterEach(() => { + document + .querySelector(".themeOfTheMonthCardSecondary") + ?.click(); + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); + +it.each([false, true])( + "click-away dismisses and records the month once (expanded: %s)", + async (expanded) => { + const onDismissed = jest.fn(); + await OpenThemeOfTheMonthPopup(entry, onDismissed); + if (expanded) + document + .querySelector(".themeOfTheMonthCardPopout")! + .click(); + const outside = expanded + ? document.getElementById("theme-of-the-month-backdrop")! + : document.body; + outside.click(); + document.body.click(); + jest.advanceTimersByTime(30_000); + expect(document.getElementById("theme-of-the-month-card")).toBeNull(); + expect(document.getElementById("theme-of-the-month-backdrop")).toBeNull(); + expect(settingsState.themeOfTheMonthDismissedMonth).toBe(entry.month); + expect(onDismissed).toHaveBeenCalledTimes(1); + }, +); + +it("keeps the card open while interacting with it and its fullscreen preview", async () => { + await OpenThemeOfTheMonthPopup(entry); + document.querySelector(".themeOfTheMonthCardBody")!.click(); + document + .querySelector(".themeOfTheMonthCardImage")! + .click(); + const overlay = document.getElementById("bsplus-popup-media-overlay")!; + expect(overlay).not.toBeNull(); + overlay.querySelector("img")!.click(); + overlay.click(); + jest.advanceTimersByTime(180); + expect(document.getElementById("theme-of-the-month-card")).not.toBeNull(); + expect(settingsState.themeOfTheMonthDismissedMonth).toBeUndefined(); + document.body.click(); + jest.advanceTimersByTime(180); + expect(document.getElementById("theme-of-the-month-card")).toBeNull(); +}); + +it("ignores the opening click and cleans up an existing card when replaced", async () => { + const opener = document.createElement("button"); + document.body.append(opener); + const oldDismissed = jest.fn(); + opener.addEventListener( + "click", + () => void OpenThemeOfTheMonthPopup(entry, oldDismissed), + ); + opener.click(); + expect(document.getElementById("theme-of-the-month-card")).not.toBeNull(); + const newDismissed = jest.fn(); + await OpenThemeOfTheMonthPopup({ ...entry, month: "2026-11" }, newDismissed); + document.body.click(); + jest.advanceTimersByTime(30_000); + expect(settingsState.themeOfTheMonthDismissedMonth).toBe("2026-11"); + expect(newDismissed).toHaveBeenCalledTimes(1); + expect(oldDismissed).not.toHaveBeenCalled(); +}); diff --git a/src/seqta/utils/Openers/OpenThemeOfTheMonthPopup.ts b/src/seqta/utils/Openers/OpenThemeOfTheMonthPopup.ts index 221212be..7cdd3b93 100644 --- a/src/seqta/utils/Openers/OpenThemeOfTheMonthPopup.ts +++ b/src/seqta/utils/Openers/OpenThemeOfTheMonthPopup.ts @@ -227,6 +227,7 @@ const TOTM_MORPH_MS = 550; const TOTM_LAYOUT_SWAP_MS = TOTM_MORPH_MS / 2; let themeOfTheMonthAnimGen = 0; +let cleanupThemeOfTheMonthPopup: (() => void) | undefined; // --------------------------------------------------------------------------- // Dimension helpers @@ -675,6 +676,7 @@ export async function OpenThemeOfTheMonthPopup( entry: ThemeOfTheMonthEntry, onDismissed?: () => void, ): Promise { + cleanupThemeOfTheMonthPopup?.(); document.getElementById("theme-of-the-month-card")?.remove(); document.getElementById("theme-of-the-month-backdrop")?.remove(); @@ -779,13 +781,29 @@ export async function OpenThemeOfTheMonthPopup( ); }; - const dismissWithCleanup = () => { + const cleanup = () => { pauseAutoClose(); window.removeEventListener("resize", onResize); backdrop.remove(); document.removeEventListener("keydown", onDocKey, true); + document.removeEventListener("click", onOutsideClick, true); + if (cleanupThemeOfTheMonthPopup === cleanup) cleanupThemeOfTheMonthPopup = undefined; + }; + const dismissWithCleanup = () => { + cleanup(); closeThemeOfTheMonthCard(card, onDismissed); }; + const dismissForMonth = () => { + settingsState.themeOfTheMonthDismissedMonth = entry.month; + dismissWithCleanup(); + }; + const onOutsideClick = (event: MouseEvent) => { + const target = event.target; + if (target instanceof Node && card.contains(target)) return; + if (target instanceof Element && target.closest("#bsplus-popup-media-overlay")) return; + dismissForMonth(); + }; + cleanupThemeOfTheMonthPopup = cleanup; autoCloseTimeout = window.setTimeout(dismissWithCleanup, 30_000); card.addEventListener("mouseenter", pauseAutoClose, { once: true }); @@ -807,22 +825,12 @@ export async function OpenThemeOfTheMonthPopup( toggleExpanded(); }); - backdrop.addEventListener("click", () => { - if (!isExpanded || expandAnimating) return; - isExpanded = false; - void applyExpandedState(false); - }); - document.addEventListener("keydown", onDocKey, true); - card.querySelector(".themeOfTheMonthCardSecondary")?.addEventListener("click", () => { - settingsState.themeOfTheMonthDismissedMonth = entry.month; - dismissWithCleanup(); - }); + card.querySelector(".themeOfTheMonthCardSecondary")?.addEventListener("click", dismissForMonth); card.querySelector(".themeOfTheMonthCardPrimary")?.addEventListener("click", () => { - settingsState.themeOfTheMonthDismissedMonth = entry.month; - dismissWithCleanup(); + dismissForMonth(); void openThemeStoreWithHighlight(linkedThemeId!); }); @@ -852,6 +860,8 @@ export async function OpenThemeOfTheMonthPopup( card.style.left = "0"; document.body.append(backdrop, card); + // Capture has already passed for the click that opened this card. + document.addEventListener("click", onOutsideClick, true); // Set initial collapsed position instantly (no transition). applyThemeOfTheMonthCardPosition(card, false, false); @@ -886,4 +896,4 @@ export async function showThemeOfTheMonthPopupNow(): Promise { } await OpenThemeOfTheMonthPopup(entry); -} \ No newline at end of file +} diff --git a/src/seqta/utils/Openers/StartupPopupQueue.ts b/src/seqta/utils/Openers/StartupPopupQueue.ts index 30f549a0..4635e5a3 100644 --- a/src/seqta/utils/Openers/StartupPopupQueue.ts +++ b/src/seqta/utils/Openers/StartupPopupQueue.ts @@ -16,7 +16,7 @@ type QueueStep = (goNext: () => void) => void; /** * Runs startup modals in order: What's New (if the extension just updated), - * Courses/Assessments fix notice (after What's New, or if features are off), + * Courses/Assessments fix notice (only if features are off), * Theme of the Month (when the user hasn't dismissed this calendar month), * then feedback reply notifications for pending submissions. */ @@ -32,14 +32,8 @@ export async function runStartupPopupQueue() { steps.push((goNext) => OpenWhatsNewPopup(goNext)); } - if ( - shouldShowCoursesAssessmentsFixPopup(settingsState, { - afterWhatsNew, - }) - ) { - steps.push((goNext) => - OpenCoursesAssessmentsFixPopup(goNext, { afterWhatsNew }), - ); + if (shouldShowCoursesAssessmentsFixPopup(settingsState)) { + steps.push((goNext) => OpenCoursesAssessmentsFixPopup(goNext)); } // Fetch the Theme of the Month before queueing so we don't show an empty