File size: 8,087 Bytes
766d85d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// FE-COMP-NOTIF-001 to FE-COMP-NOTIF-016
import { render, screen, waitFor } from '../../../tests/helpers/render';
import userEvent from '@testing-library/user-event';
import { useAuthStore } from '../../store/authStore';
import { useSettingsStore } from '../../store/settingsStore';
import { useInAppNotificationStore } from '../../store/inAppNotificationStore';
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
import { buildUser, buildSettings } from '../../../tests/helpers/factories';
import type { InAppNotification } from '../../store/inAppNotificationStore';
import InAppNotificationItem from './InAppNotificationItem';

const buildNotification = (overrides: Partial<InAppNotification> = {}): InAppNotification => ({
  id: 1,
  type: 'simple',
  scope: 'trip',
  target: 1,
  sender_id: 2,
  sender_username: 'alice',
  sender_avatar: null,
  recipient_id: 1,
  title_key: 'notifications.title',
  title_params: {},
  text_key: 'notifications.empty',
  text_params: {},
  positive_text_key: null,
  negative_text_key: null,
  response: null,
  navigate_text_key: null,
  navigate_target: null,
  is_read: false,
  created_at: new Date().toISOString(),
  ...overrides,
});

beforeEach(() => {
  resetAllStores();
  seedStore(useAuthStore, { user: buildUser(), isAuthenticated: true });
  seedStore(useSettingsStore, { settings: buildSettings() });
});

describe('InAppNotificationItem', () => {
  it('FE-COMP-NOTIF-001: renders without crashing', () => {
    render(<InAppNotificationItem notification={buildNotification()} />);
    expect(document.body).toBeInTheDocument();
  });

  it('FE-COMP-NOTIF-002: shows sender avatar initial letter', () => {
    render(<InAppNotificationItem notification={buildNotification({ sender_username: 'bob' })} />);
    // Avatar shows first letter uppercase: "B"
    expect(screen.getByText('B')).toBeInTheDocument();
  });

  it('FE-COMP-NOTIF-003: shows notification title text', () => {
    render(<InAppNotificationItem notification={buildNotification({ title_key: 'notifications.title' })} />);
    // t('notifications.title') = "Notifications"
    expect(screen.getByText('Notifications')).toBeInTheDocument();
  });

  it('FE-COMP-NOTIF-004: shows notification body text', () => {
    render(<InAppNotificationItem notification={buildNotification({ text_key: 'notifications.empty' })} />);
    // t('notifications.empty') = "No notifications"
    expect(screen.getByText('No notifications')).toBeInTheDocument();
  });

  it('FE-COMP-NOTIF-005: shows Mark as read button for unread notification', () => {
    render(<InAppNotificationItem notification={buildNotification({ is_read: false })} />);
    expect(screen.getByTitle('Mark as read')).toBeInTheDocument();
  });

  it('FE-COMP-NOTIF-006: does not show Mark as read button for read notification', () => {
    render(<InAppNotificationItem notification={buildNotification({ is_read: true })} />);
    expect(screen.queryByTitle('Mark as read')).not.toBeInTheDocument();
  });

  it('FE-COMP-NOTIF-007: shows Delete button', () => {
    render(<InAppNotificationItem notification={buildNotification()} />);
    expect(screen.getByTitle('Delete')).toBeInTheDocument();
  });

  it('FE-COMP-NOTIF-008: clicking Mark as read calls markRead', async () => {
    const user = userEvent.setup();
    const markRead = vi.fn().mockResolvedValue(undefined);
    seedStore(useInAppNotificationStore, { markRead });
    render(<InAppNotificationItem notification={buildNotification({ id: 42, is_read: false })} />);
    await user.click(screen.getByTitle('Mark as read'));
    expect(markRead).toHaveBeenCalledWith(42);
  });

  it('FE-COMP-NOTIF-009: clicking Delete calls deleteNotification', async () => {
    const user = userEvent.setup();
    const deleteNotification = vi.fn().mockResolvedValue(undefined);
    seedStore(useInAppNotificationStore, { deleteNotification });
    render(<InAppNotificationItem notification={buildNotification({ id: 99 })} />);
    await user.click(screen.getByTitle('Delete'));
    expect(deleteNotification).toHaveBeenCalledWith(99);
  });

  it('FE-COMP-NOTIF-010: shows relative timestamp', () => {
    render(<InAppNotificationItem notification={buildNotification({ created_at: new Date().toISOString() })} />);
    // Recent notification shows "just now"
    expect(screen.getByText('just now')).toBeInTheDocument();
  });

  it('FE-COMP-NOTIF-011: shows avatar image when sender_avatar is provided', () => {
    render(
      <InAppNotificationItem
        notification={buildNotification({ sender_avatar: 'https://example.com/avatar.png' })}
      />
    );
    expect(document.querySelector('img')).toBeInTheDocument();
    expect(document.querySelector('img')?.getAttribute('src')).toBe('https://example.com/avatar.png');
  });

  it('FE-COMP-NOTIF-012: boolean notification shows Accept and Reject buttons', () => {
    render(
      <InAppNotificationItem
        notification={buildNotification({
          type: 'boolean',
          positive_text_key: 'common.yes',
          negative_text_key: 'common.no',
        })}
      />
    );
    expect(screen.getByText('Yes')).toBeInTheDocument();
    expect(screen.getByText('No')).toBeInTheDocument();
  });

  it('FE-COMP-NOTIF-013: clicking Accept calls respondToBoolean with positive', async () => {
    const user = userEvent.setup();
    const respondToBoolean = vi.fn().mockResolvedValue(undefined);
    seedStore(useInAppNotificationStore, { respondToBoolean });
    render(
      <InAppNotificationItem
        notification={buildNotification({
          id: 55,
          type: 'boolean',
          positive_text_key: 'common.yes',
          negative_text_key: 'common.no',
          response: null,
        })}
      />
    );
    await user.click(screen.getByText('Yes'));
    expect(respondToBoolean).toHaveBeenCalledWith(55, 'positive');
  });

  it('FE-COMP-NOTIF-014: clicking Reject calls respondToBoolean with negative', async () => {
    const user = userEvent.setup();
    const respondToBoolean = vi.fn().mockResolvedValue(undefined);
    seedStore(useInAppNotificationStore, { respondToBoolean });
    render(
      <InAppNotificationItem
        notification={buildNotification({
          id: 66,
          type: 'boolean',
          positive_text_key: 'common.yes',
          negative_text_key: 'common.no',
          response: null,
        })}
      />
    );
    await user.click(screen.getByText('No'));
    expect(respondToBoolean).toHaveBeenCalledWith(66, 'negative');
  });

  it('FE-COMP-NOTIF-015: navigate notification shows action button', () => {
    render(
      <InAppNotificationItem
        notification={buildNotification({
          type: 'navigate',
          navigate_text_key: 'notifications.title',
          navigate_target: '/trips/1',
        })}
      />
    );
    // t('notifications.title') = "Notifications" — the navigate button renders this
    const navigateBtn = document.querySelector('button[style*="pointer"]') ??
      Array.from(document.querySelectorAll('button')).find(b => b.textContent?.includes('Notifications'));
    expect(navigateBtn).toBeInTheDocument();
  });

  it('FE-COMP-NOTIF-016: clicking navigate button marks read and navigates', async () => {
    const user = userEvent.setup();
    const markRead = vi.fn().mockResolvedValue(undefined);
    const onClose = vi.fn();
    seedStore(useInAppNotificationStore, { markRead });
    render(
      <InAppNotificationItem
        notification={buildNotification({
          id: 77,
          type: 'navigate',
          navigate_text_key: 'notifications.title',
          navigate_target: '/trips/1',
          is_read: false,
        })}
        onClose={onClose}
      />
    );
    // The navigate button renders t('notifications.title') = "Notifications"
    const btn = Array.from(document.querySelectorAll('button')).find(
      b => b.textContent?.includes('Notifications')
    );
    expect(btn).toBeTruthy();
    await user.click(btn!);
    expect(markRead).toHaveBeenCalledWith(77);
    expect(onClose).toHaveBeenCalled();
  });
});