-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathApp.test.tsx
More file actions
314 lines (253 loc) · 8.92 KB
/
App.test.tsx
File metadata and controls
314 lines (253 loc) · 8.92 KB
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
import { act } from '@testing-library/react';
import { renderWithProviders } from '../__helpers__/test-utils';
import { mockGitHubCloudAccount } from '../__mocks__/account-mocks';
import { mockGitifyNotification } from '../__mocks__/notifications-mocks';
import { mockSettings } from '../__mocks__/state-mocks';
import { Constants } from '../constants';
import { useAppContext } from '../hooks/useAppContext';
import { useNotifications } from '../hooks/useNotifications';
import type {
AuthState,
ClientID,
ClientSecret,
SettingsState,
Token,
} from '../types';
import type { DeviceFlowSession } from '../utils/auth/types';
import * as authUtils from '../utils/auth/utils';
import * as storage from '../utils/core/storage';
import * as notifications from '../utils/notifications/notifications';
import * as tray from '../utils/system/tray';
import { type AppContextState, AppProvider } from './App';
import { defaultSettings } from './defaults';
vi.mock('../hooks/useNotifications');
// Helper to render the context
const renderWithContext = () => {
let context!: AppContextState;
const CaptureContext = () => {
context = useAppContext();
return null;
};
renderWithProviders(
<AppProvider>
<CaptureContext />
</AppProvider>,
);
return () => context;
};
describe('renderer/context/App.tsx', () => {
const fetchNotificationsMock = vi.fn();
const markNotificationsAsReadMock = vi.fn();
const markNotificationsAsDoneMock = vi.fn();
const unsubscribeNotificationMock = vi.fn();
const removeAccountNotificationsMock = vi.fn();
beforeEach(() => {
vi.useFakeTimers();
vi.mocked(useNotifications).mockReturnValue({
status: 'success',
globalError: null,
notifications: [],
notificationCount: 0,
hasNotifications: false,
unreadNotificationCount: 0,
hasUnreadNotifications: false,
fetchNotifications: fetchNotificationsMock,
markNotificationsAsRead: markNotificationsAsReadMock,
markNotificationsAsDone: markNotificationsAsDoneMock,
unsubscribeNotification: unsubscribeNotificationMock,
removeAccountNotifications: removeAccountNotificationsMock,
} as ReturnType<typeof useNotifications>);
});
afterEach(() => {
vi.clearAllTimers();
});
describe('notification methods', () => {
const setTrayIconColorAndTitleSpy = vi
.spyOn(tray, 'setTrayIconColorAndTitle')
.mockImplementation(vi.fn());
vi.spyOn(notifications, 'getNotificationCount').mockImplementation(vi.fn());
vi.spyOn(notifications, 'getUnreadNotificationCount').mockImplementation(
vi.fn(),
);
const mockDefaultState = {
auth: { accounts: [] },
settings: mockSettings,
};
it('fetch notifications each interval', async () => {
renderWithProviders(<AppProvider>{null}</AppProvider>);
// Initial fetch happens on mount - advance timers to ensure it runs
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
});
expect(fetchNotificationsMock).toHaveBeenCalledTimes(1);
await act(async () => {
await vi.advanceTimersByTimeAsync(
Constants.DEFAULT_FETCH_NOTIFICATIONS_INTERVAL_MS,
);
});
expect(fetchNotificationsMock).toHaveBeenCalledTimes(2);
await act(async () => {
await vi.advanceTimersByTimeAsync(
Constants.DEFAULT_FETCH_NOTIFICATIONS_INTERVAL_MS,
);
});
expect(fetchNotificationsMock).toHaveBeenCalledTimes(3);
await act(async () => {
await vi.advanceTimersByTimeAsync(
Constants.DEFAULT_FETCH_NOTIFICATIONS_INTERVAL_MS,
);
});
expect(fetchNotificationsMock).toHaveBeenCalledTimes(4);
});
it('should call fetchNotifications', async () => {
const getContext = renderWithContext();
fetchNotificationsMock.mockReset();
act(() => {
getContext().fetchNotifications();
});
expect(fetchNotificationsMock).toHaveBeenCalledTimes(1);
});
it('should call markNotificationsAsRead', async () => {
const getContext = renderWithContext();
await act(async () => {
getContext().markNotificationsAsRead([mockGitifyNotification]);
});
expect(markNotificationsAsReadMock).toHaveBeenCalledTimes(1);
expect(markNotificationsAsReadMock).toHaveBeenCalledWith(
mockDefaultState,
[mockGitifyNotification],
);
expect(setTrayIconColorAndTitleSpy).toHaveBeenCalledTimes(1);
});
it('should call markNotificationsAsDone', async () => {
const getContext = renderWithContext();
await act(async () => {
getContext().markNotificationsAsDone([mockGitifyNotification]);
});
expect(markNotificationsAsDoneMock).toHaveBeenCalledTimes(1);
expect(markNotificationsAsDoneMock).toHaveBeenCalledWith(
mockDefaultState,
[mockGitifyNotification],
);
expect(setTrayIconColorAndTitleSpy).toHaveBeenCalledTimes(1);
});
it('should call unsubscribeNotification', async () => {
const getContext = renderWithContext();
await act(async () => {
getContext().unsubscribeNotification(mockGitifyNotification);
});
expect(unsubscribeNotificationMock).toHaveBeenCalledTimes(1);
expect(unsubscribeNotificationMock).toHaveBeenCalledWith(
mockDefaultState,
mockGitifyNotification,
);
expect(setTrayIconColorAndTitleSpy).toHaveBeenCalledTimes(1);
});
});
describe('settings methods', () => {
const saveStateSpy = vi
.spyOn(storage, 'saveState')
.mockImplementation(vi.fn());
it('should call updateSetting', async () => {
const getContext = renderWithContext();
await act(async () => {
getContext().updateSetting('participating', true);
});
expect(saveStateSpy).toHaveBeenCalledWith({
auth: {
accounts: [],
} as AuthState,
settings: {
...defaultSettings,
participating: true,
} as SettingsState,
});
});
it('should call resetSettings', async () => {
const getContext = renderWithContext();
act(() => {
getContext().resetSettings();
});
expect(saveStateSpy).toHaveBeenCalledWith({
auth: {
accounts: [],
} as AuthState,
settings: defaultSettings,
});
});
});
describe('authentication functions', () => {
const addAccountSpy = vi
.spyOn(authUtils, 'addAccount')
.mockImplementation(vi.fn())
.mockResolvedValueOnce({
accounts: [mockGitHubCloudAccount],
} as unknown as AuthState);
const removeAccountSpy = vi.spyOn(authUtils, 'removeAccount');
it('loginWithDeviceFlowStart calls startGitHubDeviceFlow', async () => {
const startGitHubDeviceFlowSpy = vi
.spyOn(authUtils, 'startGitHubDeviceFlow')
.mockImplementation(vi.fn());
const getContext = renderWithContext();
await act(async () => {
getContext().loginWithDeviceFlowStart();
});
expect(startGitHubDeviceFlowSpy).toHaveBeenCalled();
});
it('loginWithDeviceFlowPoll calls pollGitHubDeviceFlow', async () => {
const pollGitHubDeviceFlowSpy = vi
.spyOn(authUtils, 'pollGitHubDeviceFlow')
.mockImplementation(vi.fn());
const getContext = renderWithContext();
await act(async () => {
getContext().loginWithDeviceFlowPoll(
'session' as unknown as DeviceFlowSession,
);
});
expect(pollGitHubDeviceFlowSpy).toHaveBeenCalledWith('session');
});
it('loginWithDeviceFlowComplete calls addAccount', async () => {
const getContext = renderWithContext();
await act(async () => {
await getContext().loginWithDeviceFlowComplete(
'token' as Token,
Constants.GITHUB_HOSTNAME,
);
});
expect(addAccountSpy).toHaveBeenCalledWith(
expect.anything(),
'GitHub App',
'token',
'github.com',
);
});
it('loginWithOAuthApp calls performGitHubWebOAuth', async () => {
const performGitHubWebOAuthSpy = vi.spyOn(
authUtils,
'performGitHubWebOAuth',
);
const getContext = renderWithContext();
await act(async () => {
getContext().loginWithOAuthApp({
clientId: 'id' as ClientID,
clientSecret: 'secret' as ClientSecret,
hostname: Constants.GITHUB_HOSTNAME,
});
});
expect(performGitHubWebOAuthSpy).toHaveBeenCalled();
});
it('logoutFromAccount calls removeAccountNotifications, removeAccount', async () => {
const getContext = renderWithContext();
await act(async () => {
getContext().logoutFromAccount(mockGitHubCloudAccount);
});
expect(removeAccountNotificationsMock).toHaveBeenCalledWith(
mockGitHubCloudAccount,
);
expect(removeAccountSpy).toHaveBeenCalledWith(
expect.anything(),
mockGitHubCloudAccount,
);
});
});
});