File size: 13,549 Bytes
79ed62f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import { describe, it, expect, beforeEach } from 'vitest';
import { http, HttpResponse } from 'msw';
import { server } from '../../helpers/msw/server';
import { useInAppNotificationStore } from '../../../src/store/inAppNotificationStore';
import { resetAllStores } from '../../helpers/store';

// Raw notification factory matching the server shape (is_read as 0/1, params as strings)
function buildRawNotif(overrides: Record<string, unknown> = {}) {
  const id = Math.floor(Math.random() * 100000);
  return {
    id,
    type: 'simple',
    scope: 'trip',
    target: 1,
    sender_id: 2,
    sender_username: 'alice',
    sender_avatar: null,
    recipient_id: 1,
    title_key: 'notif.title',
    title_params: '{}',
    text_key: 'notif.text',
    text_params: '{}',
    positive_text_key: null,
    negative_text_key: null,
    response: null,
    navigate_text_key: null,
    navigate_target: null,
    is_read: 0,
    created_at: '2025-01-01T00:00:00.000Z',
    ...overrides,
  };
}

beforeEach(() => {
  resetAllStores();
});

describe('inAppNotificationStore', () => {
  describe('FE-NOTIF-001: fetchNotifications() loads first page', () => {
    it('populates notifications, total, and unreadCount', async () => {
      await useInAppNotificationStore.getState().fetchNotifications();
      const state = useInAppNotificationStore.getState();

      expect(state.notifications.length).toBeGreaterThan(0);
      expect(state.total).toBeGreaterThan(0);
      expect(state.unreadCount).toBe(5);
      expect(state.isLoading).toBe(false);
    });
  });

  describe('FE-NOTIF-002: Pagination — loading more appends to list', () => {
    it('appends additional notifications when fetchNotifications is called again', async () => {
      // First page
      await useInAppNotificationStore.getState().fetchNotifications(true);
      const firstPageCount = useInAppNotificationStore.getState().notifications.length;
      const total = useInAppNotificationStore.getState().total;

      // Only test pagination if there are more items
      if (firstPageCount < total) {
        await useInAppNotificationStore.getState().fetchNotifications();
        const state = useInAppNotificationStore.getState();
        expect(state.notifications.length).toBeGreaterThan(firstPageCount);
      } else {
        // All notifications fit in one page
        expect(firstPageCount).toBe(total);
      }
    });
  });

  describe('FE-NOTIF-003: markRead(id)', () => {
    it('updates is_read to true for the notification', async () => {
      // Seed with an unread notification
      const unread = buildRawNotif({ id: 42, is_read: 0 });
      useInAppNotificationStore.setState({
        notifications: [{ ...unread, title_params: {}, text_params: {}, is_read: false }] as never,
        unreadCount: 1,
      });

      await useInAppNotificationStore.getState().markRead(42);
      const state = useInAppNotificationStore.getState();

      const notif = state.notifications.find((n) => n.id === 42);
      expect(notif?.is_read).toBe(true);
      expect(state.unreadCount).toBe(0);
    });
  });

  describe('FE-NOTIF-004: handleNewNotification() prepends to list', () => {
    it('adds a new notification at the start of the list', () => {
      // Seed existing notifications
      useInAppNotificationStore.setState({
        notifications: [{ ...buildRawNotif({ id: 1 }), title_params: {}, text_params: {}, is_read: false }] as never,
        total: 1,
        unreadCount: 1,
      });

      const newRaw = buildRawNotif({ id: 99 });
      useInAppNotificationStore.getState().handleNewNotification(newRaw as never);

      const state = useInAppNotificationStore.getState();
      expect(state.notifications[0].id).toBe(99);
      expect(state.notifications.length).toBe(2);
      expect(state.total).toBe(2);
      expect(state.unreadCount).toBe(2);
    });
  });

  describe('FE-NOTIF-005: handleUpdatedNotification() updates existing notification', () => {
    it('replaces the notification in the list', () => {
      useInAppNotificationStore.setState({
        notifications: [{ ...buildRawNotif({ id: 7, is_read: 0 }), title_params: {}, text_params: {}, is_read: false }] as never,
        total: 1,
        unreadCount: 1,
      });

      const updated = buildRawNotif({ id: 7, is_read: 1 });
      useInAppNotificationStore.getState().handleUpdatedNotification(updated as never);

      const state = useInAppNotificationStore.getState();
      const notif = state.notifications.find((n) => n.id === 7);
      expect(notif?.is_read).toBe(true);
    });
  });

  describe('FE-NOTIF-006: Unread count is correct', () => {
    it('unreadCount matches the number of unread notifications', async () => {
      await useInAppNotificationStore.getState().fetchNotifications(true);
      const state = useInAppNotificationStore.getState();

      // The mock returns 5 unread from the server
      expect(state.unreadCount).toBe(5);
    });
  });

  describe('FE-STORE-NOTIF-007: fetchNotifications early-return when already loading', () => {
    it('does not fetch when isLoading is true', async () => {
      useInAppNotificationStore.setState({ isLoading: true });

      await useInAppNotificationStore.getState().fetchNotifications();
      const state = useInAppNotificationStore.getState();

      expect(state.notifications).toEqual([]);
      expect(state.isLoading).toBe(true);
    });
  });

  describe('FE-STORE-NOTIF-008: fetchNotifications(reset=true) resets existing list', () => {
    it('replaces seeded notifications with fresh data', async () => {
      // Seed store with 3 notifications
      useInAppNotificationStore.setState({
        notifications: [
          { ...buildRawNotif({ id: 901 }), title_params: {}, text_params: {}, is_read: false },
          { ...buildRawNotif({ id: 902 }), title_params: {}, text_params: {}, is_read: false },
          { ...buildRawNotif({ id: 903 }), title_params: {}, text_params: {}, is_read: false },
        ] as never,
        total: 3,
      });

      await useInAppNotificationStore.getState().fetchNotifications(true);
      const state = useInAppNotificationStore.getState();

      // Should not contain seeded IDs
      expect(state.notifications.find(n => n.id === 901)).toBeUndefined();
      expect(state.notifications.find(n => n.id === 902)).toBeUndefined();
      expect(state.notifications.find(n => n.id === 903)).toBeUndefined();
      // Should contain data from MSW (IDs 1-20)
      expect(state.notifications.length).toBe(20);
      expect(state.isLoading).toBe(false);
    });
  });

  describe('FE-STORE-NOTIF-009: hasMore is set correctly', () => {
    it('hasMore is true when more items exist, false when all loaded', async () => {
      // Default MSW returns 25 total, 20 per page
      await useInAppNotificationStore.getState().fetchNotifications(true);
      expect(useInAppNotificationStore.getState().hasMore).toBe(true);

      // Second page: offset=20, returns 5 items, total=25 => 25 >= 25 => hasMore=false
      await useInAppNotificationStore.getState().fetchNotifications();
      expect(useInAppNotificationStore.getState().hasMore).toBe(false);
    });
  });

  describe('FE-STORE-NOTIF-010: fetchUnreadCount updates unreadCount', () => {
    it('sets unreadCount from server response', async () => {
      useInAppNotificationStore.setState({ unreadCount: 0 });

      await useInAppNotificationStore.getState().fetchUnreadCount();
      expect(useInAppNotificationStore.getState().unreadCount).toBe(5);
    });
  });

  describe('FE-STORE-NOTIF-011: markUnread(id)', () => {
    it('sets is_read to false and increments unreadCount', async () => {
      useInAppNotificationStore.setState({
        notifications: [{ ...buildRawNotif({ id: 50, is_read: 1 }), title_params: {}, text_params: {}, is_read: true }] as never,
        unreadCount: 0,
      });

      await useInAppNotificationStore.getState().markUnread(50);
      const state = useInAppNotificationStore.getState();

      expect(state.notifications.find(n => n.id === 50)?.is_read).toBe(false);
      expect(state.unreadCount).toBe(1);
    });
  });

  describe('FE-STORE-NOTIF-012: markAllRead()', () => {
    it('marks all notifications as read and sets unreadCount to 0', async () => {
      useInAppNotificationStore.setState({
        notifications: [
          { ...buildRawNotif({ id: 60 }), title_params: {}, text_params: {}, is_read: false },
          { ...buildRawNotif({ id: 61 }), title_params: {}, text_params: {}, is_read: false },
          { ...buildRawNotif({ id: 62 }), title_params: {}, text_params: {}, is_read: false },
        ] as never,
        unreadCount: 3,
      });

      await useInAppNotificationStore.getState().markAllRead();
      const state = useInAppNotificationStore.getState();

      expect(state.notifications.every(n => n.is_read === true)).toBe(true);
      expect(state.unreadCount).toBe(0);
    });
  });

  describe('FE-STORE-NOTIF-013: deleteNotification removes unread item and decrements counts', () => {
    it('removes notification and decrements total and unreadCount', async () => {
      useInAppNotificationStore.setState({
        notifications: [{ ...buildRawNotif({ id: 5 }), title_params: {}, text_params: {}, is_read: false }] as never,
        total: 3,
        unreadCount: 1,
      });

      await useInAppNotificationStore.getState().deleteNotification(5);
      const state = useInAppNotificationStore.getState();

      expect(state.notifications.find(n => n.id === 5)).toBeUndefined();
      expect(state.total).toBe(2);
      expect(state.unreadCount).toBe(0);
    });
  });

  describe('FE-STORE-NOTIF-014: deleteNotification on read item does not decrement unreadCount', () => {
    it('decrements total but not unreadCount', async () => {
      useInAppNotificationStore.setState({
        notifications: [{ ...buildRawNotif({ id: 6, is_read: 1 }), title_params: {}, text_params: {}, is_read: true }] as never,
        total: 2,
        unreadCount: 0,
      });

      await useInAppNotificationStore.getState().deleteNotification(6);
      const state = useInAppNotificationStore.getState();

      expect(state.total).toBe(1);
      expect(state.unreadCount).toBe(0);
    });
  });

  describe('FE-STORE-NOTIF-015: deleteAll clears all state', () => {
    it('resets notifications, total, unreadCount, and hasMore', async () => {
      useInAppNotificationStore.setState({
        notifications: [
          { ...buildRawNotif({ id: 70 }), title_params: {}, text_params: {}, is_read: false },
          { ...buildRawNotif({ id: 71 }), title_params: {}, text_params: {}, is_read: false },
        ] as never,
        total: 2,
        unreadCount: 2,
        hasMore: true,
      });

      await useInAppNotificationStore.getState().deleteAll();
      const state = useInAppNotificationStore.getState();

      expect(state.notifications).toEqual([]);
      expect(state.total).toBe(0);
      expect(state.unreadCount).toBe(0);
      expect(state.hasMore).toBe(false);
    });
  });

  describe('FE-STORE-NOTIF-016: respondToBoolean updates notification', () => {
    it('updates response and is_read from server', async () => {
      useInAppNotificationStore.setState({
        notifications: [{
          ...buildRawNotif({ id: 10, type: 'boolean' }),
          title_params: {},
          text_params: {},
          is_read: false,
        }] as never,
        unreadCount: 1,
      });

      await useInAppNotificationStore.getState().respondToBoolean(10, 'positive');
      const state = useInAppNotificationStore.getState();

      const notif = state.notifications.find(n => n.id === 10);
      expect(notif?.response).toBe('positive');
      expect(notif?.is_read).toBe(true);
    });
  });

  describe('FE-STORE-NOTIF-017: normalizeNotification coerces stringified params', () => {
    it('parses JSON string params into objects', () => {
      const raw = buildRawNotif({
        id: 200,
        title_params: '{"trip":"Rome"}',
        text_params: '{"user":"alice"}',
      });

      useInAppNotificationStore.getState().handleNewNotification(raw as never);
      const notif = useInAppNotificationStore.getState().notifications.find(n => n.id === 200);

      expect(notif?.title_params).toEqual({ trip: 'Rome' });
      expect(notif?.text_params).toEqual({ user: 'alice' });
    });
  });

  describe('FE-STORE-NOTIF-018: normalizeNotification handles already-parsed params', () => {
    it('stores object params without error', () => {
      const raw = buildRawNotif({
        id: 201,
        title_params: {},
        text_params: { key: 'value' },
      });

      expect(() => {
        useInAppNotificationStore.getState().handleNewNotification(raw as never);
      }).not.toThrow();

      const notif = useInAppNotificationStore.getState().notifications.find(n => n.id === 201);
      expect(notif?.title_params).toEqual({});
      expect(notif?.text_params).toEqual({ key: 'value' });
    });
  });

  describe('FE-STORE-NOTIF-019: fetchUnreadCount is best-effort', () => {
    it('does not throw on server error and preserves state', async () => {
      useInAppNotificationStore.setState({ unreadCount: 3 });

      server.use(
        http.get('/api/notifications/in-app/unread-count', () => {
          return new HttpResponse(null, { status: 500 });
        }),
      );

      await expect(useInAppNotificationStore.getState().fetchUnreadCount()).resolves.not.toThrow();
      expect(useInAppNotificationStore.getState().unreadCount).toBe(3);
    });
  });
});