|
1 | 1 | import { render, screen, waitFor } from "@testing-library/react"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
2 | 3 | import { service } from "service"; |
| 4 | +import liteUsers from "test/fixtures/liteUsers.json"; |
3 | 5 | import wrapper from "test/hookWrapper"; |
4 | 6 | import i18n from "test/i18n"; |
5 | 7 | import { assertErrorAlert, mockConsoleError } from "test/utils"; |
6 | 8 |
|
7 | 9 | import ReminderCarousel from "./ReminderCarousel"; |
8 | 10 |
|
| 11 | +const ONE_WEEK_MS = 7 * 24 * 60 * 60 * 1000; |
| 12 | +const DISMISSED_REMINDERS_KEY = "dismissedReminders"; |
| 13 | + |
9 | 14 | const { t } = i18n; |
10 | 15 |
|
11 | 16 | const getReminders = service.account.getReminders as jest.MockedFunction< |
12 | 17 | typeof service.account.getReminders |
13 | 18 | >; |
14 | 19 |
|
15 | 20 | describe("ReminderCarousel", () => { |
| 21 | + beforeEach(() => { |
| 22 | + localStorage.removeItem(DISMISSED_REMINDERS_KEY); |
| 23 | + }); |
| 24 | + |
16 | 25 | it("renders nothing when backend returns no reminders", async () => { |
17 | 26 | getReminders.mockResolvedValue({ remindersList: [] }); |
18 | 27 |
|
@@ -48,4 +57,135 @@ describe("ReminderCarousel", () => { |
48 | 57 |
|
49 | 58 | await assertErrorAlert("Failed to fetch reminders"); |
50 | 59 | }); |
| 60 | + |
| 61 | + it("dismisses a reminder via the dismiss control and persists the choice", async () => { |
| 62 | + const user = userEvent.setup(); |
| 63 | + getReminders.mockResolvedValue({ |
| 64 | + remindersList: [{ completeProfileReminder: {} }], |
| 65 | + }); |
| 66 | + |
| 67 | + render(<ReminderCarousel />, { wrapper }); |
| 68 | + |
| 69 | + expect( |
| 70 | + await screen.findByText(t("dashboard:reminder.complete_profile.title")), |
| 71 | + ).toBeVisible(); |
| 72 | + |
| 73 | + await user.click( |
| 74 | + screen.getByRole("button", { |
| 75 | + name: t("dashboard:reminder.carousel_dismiss_button_a11y"), |
| 76 | + }), |
| 77 | + ); |
| 78 | + |
| 79 | + expect( |
| 80 | + screen.queryByText(t("dashboard:reminder.complete_profile.title")), |
| 81 | + ).not.toBeInTheDocument(); |
| 82 | + |
| 83 | + const stored: Record<string, number> = JSON.parse( |
| 84 | + localStorage.getItem(DISMISSED_REMINDERS_KEY) ?? "{}", |
| 85 | + ); |
| 86 | + expect(typeof stored.complete_profile).toBe("number"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("does not show a reminder that was dismissed within the last week", async () => { |
| 90 | + const now = 1_700_000_000_000; |
| 91 | + jest.spyOn(Date, "now").mockReturnValue(now); |
| 92 | + localStorage.setItem( |
| 93 | + DISMISSED_REMINDERS_KEY, |
| 94 | + JSON.stringify({ |
| 95 | + complete_profile: now - 24 * 60 * 60 * 1000, |
| 96 | + }), |
| 97 | + ); |
| 98 | + |
| 99 | + getReminders.mockResolvedValue({ |
| 100 | + remindersList: [{ completeProfileReminder: {} }], |
| 101 | + }); |
| 102 | + |
| 103 | + const { container } = render(<ReminderCarousel />, { wrapper }); |
| 104 | + |
| 105 | + await waitFor(() => expect(getReminders).toHaveBeenCalled()); |
| 106 | + expect(container).toBeEmptyDOMElement(); |
| 107 | + }); |
| 108 | + |
| 109 | + it("shows a reminder again once the dismiss is older than one week", async () => { |
| 110 | + const now = 1_700_000_000_000; |
| 111 | + jest.spyOn(Date, "now").mockReturnValue(now); |
| 112 | + localStorage.setItem( |
| 113 | + DISMISSED_REMINDERS_KEY, |
| 114 | + JSON.stringify({ |
| 115 | + complete_profile: now - ONE_WEEK_MS - 1, |
| 116 | + }), |
| 117 | + ); |
| 118 | + |
| 119 | + getReminders.mockResolvedValue({ |
| 120 | + remindersList: [{ completeProfileReminder: {} }], |
| 121 | + }); |
| 122 | + |
| 123 | + render(<ReminderCarousel />, { wrapper }); |
| 124 | + |
| 125 | + expect( |
| 126 | + await screen.findByText(t("dashboard:reminder.complete_profile.title")), |
| 127 | + ).toBeVisible(); |
| 128 | + }); |
| 129 | + |
| 130 | + it("prunes stale dismiss entries from storage when a reminder is dismissed", async () => { |
| 131 | + const user = userEvent.setup(); |
| 132 | + const now = 2_000_000_000_000; |
| 133 | + jest.spyOn(Date, "now").mockReturnValue(now); |
| 134 | + localStorage.setItem( |
| 135 | + DISMISSED_REMINDERS_KEY, |
| 136 | + JSON.stringify({ |
| 137 | + "write_reference:7": now - ONE_WEEK_MS - 1000, |
| 138 | + }), |
| 139 | + ); |
| 140 | + |
| 141 | + getReminders.mockResolvedValue({ |
| 142 | + remindersList: [{ completeProfileReminder: {} }], |
| 143 | + }); |
| 144 | + |
| 145 | + render(<ReminderCarousel />, { wrapper }); |
| 146 | + await screen.findByText(t("dashboard:reminder.complete_profile.title")); |
| 147 | + |
| 148 | + await user.click( |
| 149 | + screen.getByRole("button", { |
| 150 | + name: t("dashboard:reminder.carousel_dismiss_button_a11y"), |
| 151 | + }), |
| 152 | + ); |
| 153 | + |
| 154 | + const stored: Record<string, number> = JSON.parse( |
| 155 | + localStorage.getItem(DISMISSED_REMINDERS_KEY) ?? "{}", |
| 156 | + ); |
| 157 | + expect(stored["write_reference:7"]).toBeUndefined(); |
| 158 | + expect(stored.complete_profile).toBe(now); |
| 159 | + }); |
| 160 | + |
| 161 | + it("dismissing one host request reminder does not dismiss another", async () => { |
| 162 | + const user = userEvent.setup(); |
| 163 | + const [alice, bob] = liteUsers; |
| 164 | + getReminders.mockResolvedValue({ |
| 165 | + remindersList: [ |
| 166 | + { respondToHostRequestReminder: { hostRequestId: 42, surferUser: alice } }, |
| 167 | + { respondToHostRequestReminder: { hostRequestId: 99, surferUser: bob } }, |
| 168 | + ], |
| 169 | + }); |
| 170 | + |
| 171 | + render(<ReminderCarousel />, { wrapper }); |
| 172 | + |
| 173 | + const aliceTitle = t("dashboard:reminder.respond_to_host_request.title", { |
| 174 | + name: alice.name, |
| 175 | + }); |
| 176 | + const bobTitle = t("dashboard:reminder.respond_to_host_request.title", { |
| 177 | + name: bob.name, |
| 178 | + }); |
| 179 | + |
| 180 | + expect(await screen.findByText(aliceTitle)).toBeVisible(); |
| 181 | + expect(screen.getByText(bobTitle)).toBeVisible(); |
| 182 | + |
| 183 | + const dismissButtons = screen.getAllByRole("button", { |
| 184 | + name: t("dashboard:reminder.carousel_dismiss_button_a11y"), |
| 185 | + }); |
| 186 | + await user.click(dismissButtons[0]); |
| 187 | + |
| 188 | + expect(screen.queryByText(aliceTitle)).not.toBeInTheDocument(); |
| 189 | + expect(screen.getByText(bobTitle)).toBeVisible(); |
| 190 | + }); |
51 | 191 | }); |
0 commit comments