|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
| 3 | +import { MigrationNotice } from "./MigrationNotice"; |
| 4 | +import { |
| 5 | + MIGRATION_NOTICE_DISMISSED_KEY, |
| 6 | + MIGRATION_NOTICE_TTL_MS, |
| 7 | + MIGRATION_TARGET_URL |
| 8 | +} from "../../lib/migrationNotice"; |
| 9 | + |
| 10 | +function createMockStorage(seed: Record<string, string> = {}) { |
| 11 | + const values = new Map<string, string>(Object.entries(seed)); |
| 12 | + |
| 13 | + return { |
| 14 | + getItem: vi.fn((key: string) => values.get(key) ?? null), |
| 15 | + setItem: vi.fn((key: string, value: string) => { |
| 16 | + values.set(key, value); |
| 17 | + }) |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +describe("MigrationNotice", () => { |
| 22 | + const fixedNow = 1_750_000_000_000; |
| 23 | + const nowProvider = () => fixedNow; |
| 24 | + |
| 25 | + test("renders for non-primary hosts and points to the new site", () => { |
| 26 | + const storage = createMockStorage(); |
| 27 | + render( |
| 28 | + <MigrationNotice hostname="c4lab.bime.ntu.edu.tw" storage={storage} nowProvider={nowProvider} /> |
| 29 | + ); |
| 30 | + |
| 31 | + expect(screen.getByRole("region", { name: /site migration notice/i })).toBeVisible(); |
| 32 | + expect(screen.getByRole("link", { name: /c4lab.github.io/i })).toHaveAttribute( |
| 33 | + "href", |
| 34 | + MIGRATION_TARGET_URL |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + test("does not render for the primary host", () => { |
| 39 | + const storage = createMockStorage(); |
| 40 | + render(<MigrationNotice hostname="c4lab.github.io" storage={storage} nowProvider={nowProvider} />); |
| 41 | + |
| 42 | + expect(screen.queryByRole("region", { name: /site migration notice/i })).not.toBeInTheDocument(); |
| 43 | + }); |
| 44 | + |
| 45 | + test("renders for localhost", () => { |
| 46 | + const storage = createMockStorage(); |
| 47 | + render(<MigrationNotice hostname="localhost" storage={storage} nowProvider={nowProvider} />); |
| 48 | + |
| 49 | + expect(screen.getByRole("region", { name: /site migration notice/i })).toBeVisible(); |
| 50 | + }); |
| 51 | + |
| 52 | + test("dismisses and persists to storage", async () => { |
| 53 | + const user = userEvent.setup(); |
| 54 | + const storage = createMockStorage(); |
| 55 | + render( |
| 56 | + <MigrationNotice hostname="c4lab.bime.ntu.edu.tw" storage={storage} nowProvider={nowProvider} /> |
| 57 | + ); |
| 58 | + |
| 59 | + await user.click(screen.getByRole("button", { name: /dismiss migration notice/i })); |
| 60 | + |
| 61 | + expect(screen.queryByRole("region", { name: /site migration notice/i })).not.toBeInTheDocument(); |
| 62 | + expect(storage.setItem).toHaveBeenCalledWith(MIGRATION_NOTICE_DISMISSED_KEY, String(fixedNow)); |
| 63 | + }); |
| 64 | + |
| 65 | + test("stays hidden when dismissed within 24 hours", () => { |
| 66 | + const storage = createMockStorage({ |
| 67 | + [MIGRATION_NOTICE_DISMISSED_KEY]: String(fixedNow - MIGRATION_NOTICE_TTL_MS + 1) |
| 68 | + }); |
| 69 | + render( |
| 70 | + <MigrationNotice hostname="c4lab.bime.ntu.edu.tw" storage={storage} nowProvider={nowProvider} /> |
| 71 | + ); |
| 72 | + |
| 73 | + expect(screen.queryByRole("region", { name: /site migration notice/i })).not.toBeInTheDocument(); |
| 74 | + }); |
| 75 | + |
| 76 | + test("shows again when dismissed more than 24 hours ago", () => { |
| 77 | + const storage = createMockStorage({ |
| 78 | + [MIGRATION_NOTICE_DISMISSED_KEY]: String(fixedNow - MIGRATION_NOTICE_TTL_MS - 1) |
| 79 | + }); |
| 80 | + render( |
| 81 | + <MigrationNotice hostname="c4lab.bime.ntu.edu.tw" storage={storage} nowProvider={nowProvider} /> |
| 82 | + ); |
| 83 | + |
| 84 | + expect(screen.getByRole("region", { name: /site migration notice/i })).toBeVisible(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments