|
| 1 | +import type { Post } from "$types"; |
| 2 | +import { flushSync, untrack } from "svelte"; |
| 3 | +import { afterEach, beforeEach, describe, it } from "vitest"; |
| 4 | +import { NewsroomNotificationsContext } from "./newsroom-notifications.svelte"; |
| 5 | + |
| 6 | +const post = (id: string, publishedAt: string | null = `2026-01-${id.padStart(2, "0")}T00:00:00.000Z`): Post => ({ |
| 7 | + id, |
| 8 | + title: `Post ${id}`, |
| 9 | + slug: `post-${id}`, |
| 10 | + excerpt: null, |
| 11 | + type: "news", |
| 12 | + tags: null, |
| 13 | + featured: false, |
| 14 | + heroImage: null, |
| 15 | + body: null, |
| 16 | + publishedAt, |
| 17 | + author: "author", |
| 18 | + updatedAt: publishedAt ?? "2026-01-01T00:00:00.000Z", |
| 19 | + createdAt: publishedAt ?? "2026-01-01T00:00:00.000Z" |
| 20 | +}); |
| 21 | + |
| 22 | +describe("NewsroomNotificationsContext", () => { |
| 23 | + beforeEach(() => { |
| 24 | + localStorage.clear(); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + localStorage.clear(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("starts with zero unread posts", ({ expect }) => { |
| 32 | + const cleanup = $effect.root(() => { |
| 33 | + const notifications = new NewsroomNotificationsContext(); |
| 34 | + |
| 35 | + untrack(() => { |
| 36 | + expect(notifications.current.seenPostIds).toEqual([]); |
| 37 | + expect(notifications.current.lastSeenPublishedAt).toBeNull(); |
| 38 | + expect(notifications.unreadCount).toBe(0); |
| 39 | + expect(notifications.newestUnseen).toBeNull(); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + cleanup(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("marks latest posts as unseen until they are seen", ({ expect }) => { |
| 47 | + const cleanup = $effect.root(() => { |
| 48 | + const notifications = new NewsroomNotificationsContext(); |
| 49 | + const posts = [post("1"), post("2")]; |
| 50 | + |
| 51 | + untrack(() => { |
| 52 | + notifications.setLatestPosts(posts); |
| 53 | + expect(notifications.unreadCount).toBe(2); |
| 54 | + expect(notifications.unseenPosts.map((item) => item.id)).toEqual(["2", "1"]); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + cleanup(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("markPostSeen removes exactly one post from unread state", ({ expect }) => { |
| 62 | + const cleanup = $effect.root(() => { |
| 63 | + const notifications = new NewsroomNotificationsContext(); |
| 64 | + const posts = [post("1"), post("2")]; |
| 65 | + |
| 66 | + untrack(() => { |
| 67 | + notifications.setLatestPosts(posts); |
| 68 | + notifications.markPostSeen(posts[0]); |
| 69 | + flushSync(); |
| 70 | + |
| 71 | + expect(notifications.unreadCount).toBe(1); |
| 72 | + expect(notifications.unseenPosts[0].id).toBe("2"); |
| 73 | + expect(notifications.current.seenPostIds).toEqual(["1"]); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + cleanup(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("markAllSeen clears all currently tracked posts", ({ expect }) => { |
| 81 | + const cleanup = $effect.root(() => { |
| 82 | + const notifications = new NewsroomNotificationsContext(); |
| 83 | + const posts = [post("1"), post("2")]; |
| 84 | + |
| 85 | + untrack(() => { |
| 86 | + notifications.setLatestPosts(posts); |
| 87 | + notifications.markAllSeen(); |
| 88 | + flushSync(); |
| 89 | + |
| 90 | + expect(notifications.unreadCount).toBe(0); |
| 91 | + expect(notifications.current.seenPostIds).toEqual(["1", "2"]); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + cleanup(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("de-duplicates seen ids", ({ expect }) => { |
| 99 | + const cleanup = $effect.root(() => { |
| 100 | + const notifications = new NewsroomNotificationsContext(); |
| 101 | + const item = post("1"); |
| 102 | + |
| 103 | + untrack(() => { |
| 104 | + notifications.markPostSeen(item); |
| 105 | + notifications.markPostSeen(item); |
| 106 | + flushSync(); |
| 107 | + |
| 108 | + expect(notifications.current.seenPostIds).toEqual(["1"]); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + cleanup(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("bounds seen ids to 100", ({ expect }) => { |
| 116 | + const cleanup = $effect.root(() => { |
| 117 | + const notifications = new NewsroomNotificationsContext(); |
| 118 | + const posts = Array.from({ length: 101 }, (_, index) => post(String(index + 1))); |
| 119 | + |
| 120 | + untrack(() => { |
| 121 | + notifications.markAllSeen(posts); |
| 122 | + flushSync(); |
| 123 | + |
| 124 | + expect(notifications.current.seenPostIds).toHaveLength(100); |
| 125 | + expect(notifications.current.seenPostIds[0]).toBe("2"); |
| 126 | + expect(notifications.current.seenPostIds.at(-1)).toBe("101"); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + cleanup(); |
| 131 | + }); |
| 132 | + |
| 133 | + it("shows newer posts after previously dismissing current latest posts", ({ expect }) => { |
| 134 | + const cleanup = $effect.root(() => { |
| 135 | + const notifications = new NewsroomNotificationsContext(); |
| 136 | + const firstPosts = [post("1"), post("2")]; |
| 137 | + const newerPost = post("3", "2026-02-01T00:00:00.000Z"); |
| 138 | + |
| 139 | + untrack(() => { |
| 140 | + notifications.setLatestPosts(firstPosts); |
| 141 | + notifications.markAllSeen(); |
| 142 | + notifications.setLatestPosts([newerPost, ...firstPosts]); |
| 143 | + |
| 144 | + expect(notifications.unreadCount).toBe(1); |
| 145 | + expect(notifications.newestUnseen?.id).toBe("3"); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + cleanup(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments