Skip to content

Commit ffd8536

Browse files
committed
refactor: test suites
Signed-off-by: Adam Setch <[email protected]>
1 parent 606df87 commit ffd8536

26 files changed

+369
-298
lines changed

src/preload/index.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ describe('preload/index', () => {
7575

7676
it('exposes api on window when context isolation disabled', async () => {
7777
await importPreload();
78+
7879
const w = window as unknown as { gitify: Record<string, unknown> };
80+
7981
expect(w.gitify).toBeDefined();
8082
expect(exposeInMainWorld).not.toHaveBeenCalled();
8183
});
@@ -84,16 +86,20 @@ describe('preload/index', () => {
8486
(process as unknown as { contextIsolated?: boolean }).contextIsolated =
8587
true;
8688
await importPreload();
89+
8790
expect(exposeInMainWorld).toHaveBeenCalledTimes(1);
91+
8892
const [key, api] = exposeInMainWorld.mock.calls[0];
8993
expect(key).toBe('gitify');
9094
expect(api).toHaveProperty('openExternalLink');
9195
});
9296

9397
it('tray.updateColor sends correct events', async () => {
9498
await importPreload();
99+
95100
const api = (window as unknown as { gitify: TestApi }).gitify; // casting only in test boundary
96101
api.tray.updateColor(-1);
102+
97103
expect(sendMainEvent).toHaveBeenNthCalledWith(
98104
1,
99105
EVENTS.UPDATE_ICON_COLOR,
@@ -103,8 +109,10 @@ describe('preload/index', () => {
103109

104110
it('openExternalLink sends event with payload', async () => {
105111
await importPreload();
112+
106113
const api = (window as unknown as { gitify: TestApi }).gitify;
107114
api.openExternalLink('https://example.com', true);
115+
108116
expect(sendMainEvent).toHaveBeenCalledWith(EVENTS.OPEN_EXTERNAL, {
109117
url: 'https://example.com',
110118
activate: true,
@@ -114,60 +122,79 @@ describe('preload/index', () => {
114122
it('app.version returns dev in development', async () => {
115123
const originalEnv = process.env.NODE_ENV;
116124
process.env.NODE_ENV = 'development';
125+
117126
await importPreload();
127+
118128
const api = (window as unknown as { gitify: TestApi }).gitify;
129+
119130
await expect(api.app.version()).resolves.toBe('dev');
120131
process.env.NODE_ENV = originalEnv;
121132
});
122133

123134
it('app.version prefixes production version', async () => {
124135
const originalEnv = process.env.NODE_ENV;
125136
process.env.NODE_ENV = 'production';
137+
126138
invokeMainEvent.mockResolvedValueOnce('1.2.3');
139+
127140
await importPreload();
141+
128142
const api = (window as unknown as { gitify: TestApi }).gitify;
143+
129144
await expect(api.app.version()).resolves.toBe('v1.2.3');
130145
process.env.NODE_ENV = originalEnv;
131146
});
132147

133148
it('onSystemThemeUpdate registers listener', async () => {
134149
await importPreload();
150+
135151
const api = (window as unknown as { gitify: TestApi }).gitify;
136152
const callback = jest.fn();
137153
api.onSystemThemeUpdate(callback);
154+
138155
expect(onRendererEvent).toHaveBeenCalledWith(
139156
EVENTS.UPDATE_THEME,
140157
expect.any(Function),
141158
);
159+
142160
// Simulate event
143161
const listener = onRendererEvent.mock.calls[0][1];
144162
listener({}, 'dark');
163+
145164
expect(callback).toHaveBeenCalledWith('dark');
146165
});
147166

148167
it('raiseNativeNotification without url calls app.show', async () => {
149168
await importPreload();
169+
150170
const api = (window as unknown as { gitify: TestApi }).gitify;
151171
api.app.show = jest.fn();
172+
152173
const notification = api.raiseNativeNotification(
153174
'Title',
154175
'Body',
155176
) as MockNotification;
177+
156178
notification.triggerClick();
179+
157180
expect(api.app.show).toHaveBeenCalled();
158181
});
159182

160183
it('raiseNativeNotification with url hides app then opens link', async () => {
161184
await importPreload();
185+
162186
const api = (window as unknown as { gitify: TestApi }).gitify;
163187
api.app.hide = jest.fn();
164188
api.openExternalLink = jest.fn();
189+
165190
const notification = api.raiseNativeNotification(
166191
'Title',
167192
'Body',
168193
'https://x',
169194
) as MockNotification;
195+
170196
notification.triggerClick();
197+
171198
expect(api.app.hide).toHaveBeenCalled();
172199
expect(api.openExternalLink).toHaveBeenCalledWith('https://x', true);
173200
});

src/renderer/components/Sidebar.test.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jest.mock('react-router-dom', () => ({
1515

1616
describe('renderer/components/Sidebar.tsx', () => {
1717
const mockFetchNotifications = jest.fn();
18-
const mockOpenExternalLink = jest
18+
const openExternalLinkSpy = jest
1919
.spyOn(comms, 'openExternalLink')
2020
.mockImplementation();
2121

@@ -61,7 +61,8 @@ describe('renderer/components/Sidebar.tsx', () => {
6161

6262
await userEvent.click(screen.getByTestId('sidebar-home'));
6363

64-
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/', { replace: true });
64+
expect(mockNavigate).toHaveBeenCalledTimes(1);
65+
expect(mockNavigate).toHaveBeenCalledWith('/', { replace: true });
6566
});
6667

6768
describe('notifications icon', () => {
@@ -77,8 +78,8 @@ describe('renderer/components/Sidebar.tsx', () => {
7778

7879
await userEvent.click(screen.getByTestId('sidebar-notifications'));
7980

80-
expect(mockOpenExternalLink).toHaveBeenCalledTimes(1);
81-
expect(mockOpenExternalLink).toHaveBeenCalledWith(
81+
expect(openExternalLinkSpy).toHaveBeenCalledTimes(1);
82+
expect(openExternalLinkSpy).toHaveBeenCalledWith(
8283
'https://github.com/notifications',
8384
);
8485
});
@@ -125,7 +126,8 @@ describe('renderer/components/Sidebar.tsx', () => {
125126

126127
await userEvent.click(screen.getByTestId('sidebar-filter-notifications'));
127128

128-
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/filters');
129+
expect(mockNavigate).toHaveBeenCalledTimes(1);
130+
expect(mockNavigate).toHaveBeenCalledWith('/filters');
129131
});
130132

131133
it('go to the home if filters path already shown', async () => {
@@ -140,7 +142,8 @@ describe('renderer/components/Sidebar.tsx', () => {
140142

141143
await userEvent.click(screen.getByTestId('sidebar-filter-notifications'));
142144

143-
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/', { replace: true });
145+
expect(mockNavigate).toHaveBeenCalledTimes(1);
146+
expect(mockNavigate).toHaveBeenCalledWith('/', { replace: true });
144147
});
145148
});
146149

@@ -158,8 +161,8 @@ describe('renderer/components/Sidebar.tsx', () => {
158161

159162
await userEvent.click(screen.getByTestId('sidebar-my-issues'));
160163

161-
expect(mockOpenExternalLink).toHaveBeenCalledTimes(1);
162-
expect(mockOpenExternalLink).toHaveBeenCalledWith(
164+
expect(openExternalLinkSpy).toHaveBeenCalledTimes(1);
165+
expect(openExternalLinkSpy).toHaveBeenCalledWith(
163166
'https://github.com/issues',
164167
);
165168
});
@@ -177,8 +180,8 @@ describe('renderer/components/Sidebar.tsx', () => {
177180

178181
await userEvent.click(screen.getByTestId('sidebar-my-pull-requests'));
179182

180-
expect(mockOpenExternalLink).toHaveBeenCalledTimes(1);
181-
expect(mockOpenExternalLink).toHaveBeenCalledWith(
183+
expect(openExternalLinkSpy).toHaveBeenCalledTimes(1);
184+
expect(openExternalLinkSpy).toHaveBeenCalledWith(
182185
'https://github.com/pulls',
183186
);
184187
});
@@ -235,7 +238,8 @@ describe('renderer/components/Sidebar.tsx', () => {
235238

236239
await userEvent.click(screen.getByTestId('sidebar-settings'));
237240

238-
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/settings');
241+
expect(mockNavigate).toHaveBeenCalledTimes(1);
242+
expect(mockNavigate).toHaveBeenCalledWith('/settings');
239243
});
240244

241245
it('go to the home if settings path already shown', async () => {
@@ -252,12 +256,13 @@ describe('renderer/components/Sidebar.tsx', () => {
252256
await userEvent.click(screen.getByTestId('sidebar-settings'));
253257

254258
expect(mockFetchNotifications).toHaveBeenCalledTimes(1);
255-
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/', { replace: true });
259+
expect(mockNavigate).toHaveBeenCalledTimes(1);
260+
expect(mockNavigate).toHaveBeenCalledWith('/', { replace: true });
256261
});
257262
});
258263

259264
it('should quit the app', async () => {
260-
const mockQuitApp = jest.spyOn(comms, 'quitApp');
265+
const quitAppSpy = jest.spyOn(comms, 'quitApp').mockImplementation();
261266

262267
renderWithAppContext(
263268
<MemoryRouter>
@@ -270,6 +275,6 @@ describe('renderer/components/Sidebar.tsx', () => {
270275

271276
await userEvent.click(screen.getByTestId('sidebar-quit'));
272277

273-
expect(mockQuitApp).toHaveBeenCalledTimes(1);
278+
expect(quitAppSpy).toHaveBeenCalledTimes(1);
274279
});
275280
});

src/renderer/components/notifications/AccountNotifications.test.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('renderer/components/notifications/AccountNotifications.tsx', () => {
113113
});
114114

115115
it('should open profile when clicked', async () => {
116-
const mockOpenAccountProfile = jest
116+
const openAccountProfileSpy = jest
117117
.spyOn(links, 'openAccountProfile')
118118
.mockImplementation();
119119

@@ -128,12 +128,12 @@ describe('renderer/components/notifications/AccountNotifications.tsx', () => {
128128

129129
await userEvent.click(screen.getByTestId('account-profile'));
130130

131-
expect(mockOpenAccountProfile).toHaveBeenCalledTimes(1);
132-
expect(mockOpenAccountProfile).toHaveBeenCalledWith(mockGitHubCloudAccount);
131+
expect(openAccountProfileSpy).toHaveBeenCalledTimes(1);
132+
expect(openAccountProfileSpy).toHaveBeenCalledWith(mockGitHubCloudAccount);
133133
});
134134

135135
it('should open my issues when clicked', async () => {
136-
const mockOpenGitHubIssues = jest
136+
const openGitHubIssuesSpy = jest
137137
.spyOn(links, 'openGitHubIssues')
138138
.mockImplementation();
139139

@@ -148,14 +148,14 @@ describe('renderer/components/notifications/AccountNotifications.tsx', () => {
148148

149149
await userEvent.click(screen.getByTestId('account-issues'));
150150

151-
expect(mockOpenGitHubIssues).toHaveBeenCalledTimes(1);
152-
expect(mockOpenGitHubIssues).toHaveBeenCalledWith(
151+
expect(openGitHubIssuesSpy).toHaveBeenCalledTimes(1);
152+
expect(openGitHubIssuesSpy).toHaveBeenCalledWith(
153153
mockGitHubCloudAccount.hostname,
154154
);
155155
});
156156

157157
it('should open my pull requests when clicked', async () => {
158-
const mockOpenGitHubPulls = jest
158+
const openGitHubPullsSpy = jest
159159
.spyOn(links, 'openGitHubPulls')
160160
.mockImplementation();
161161

@@ -170,8 +170,8 @@ describe('renderer/components/notifications/AccountNotifications.tsx', () => {
170170

171171
await userEvent.click(screen.getByTestId('account-pull-requests'));
172172

173-
expect(mockOpenGitHubPulls).toHaveBeenCalledTimes(1);
174-
expect(mockOpenGitHubPulls).toHaveBeenCalledWith(
173+
expect(openGitHubPullsSpy).toHaveBeenCalledTimes(1);
174+
expect(openGitHubPullsSpy).toHaveBeenCalledWith(
175175
mockGitHubCloudAccount.hostname,
176176
);
177177
});

src/renderer/components/notifications/NotificationFooter.test.tsx

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import * as comms from '../../utils/comms';
1010
import { NotificationFooter } from './NotificationFooter';
1111

1212
describe('renderer/components/notifications/NotificationFooter.tsx', () => {
13+
jest
14+
.spyOn(globalThis.Date, 'now')
15+
.mockImplementation(() => new Date('2024').valueOf());
16+
1317
afterEach(() => {
1418
jest.clearAllMocks();
1519
});
1620

1721
it('should render itself & its children', async () => {
18-
jest
19-
.spyOn(globalThis.Date, 'now')
20-
.mockImplementation(() => new Date('2024').valueOf());
21-
2222
const props = {
2323
notification: mockSingleNotification,
2424
};
@@ -29,10 +29,6 @@ describe('renderer/components/notifications/NotificationFooter.tsx', () => {
2929
});
3030

3131
it('should render itself & its children when last_read_at is null', async () => {
32-
jest
33-
.spyOn(globalThis.Date, 'now')
34-
.mockImplementation(() => new Date('2024').valueOf());
35-
3632
const mockNotification = mockSingleNotification;
3733
mockNotification.last_read_at = null;
3834

@@ -47,10 +43,6 @@ describe('renderer/components/notifications/NotificationFooter.tsx', () => {
4743

4844
describe('security alerts should use github icon for avatar', () => {
4945
it('Repository Dependabot Alerts Thread', async () => {
50-
jest
51-
.spyOn(globalThis.Date, 'now')
52-
.mockImplementation(() => new Date('2024').valueOf());
53-
5446
const mockNotification = mockSingleNotification;
5547
mockNotification.subject.type = 'RepositoryDependabotAlertsThread';
5648

@@ -64,10 +56,6 @@ describe('renderer/components/notifications/NotificationFooter.tsx', () => {
6456
});
6557

6658
it('Repository Vulnerability Alert', async () => {
67-
jest
68-
.spyOn(globalThis.Date, 'now')
69-
.mockImplementation(() => new Date('2024').valueOf());
70-
7159
const mockNotification = mockSingleNotification;
7260
mockNotification.subject.type = 'RepositoryVulnerabilityAlert';
7361

@@ -82,10 +70,6 @@ describe('renderer/components/notifications/NotificationFooter.tsx', () => {
8270
});
8371

8472
it('should default to known avatar if no user found', async () => {
85-
jest
86-
.spyOn(globalThis.Date, 'now')
87-
.mockImplementation(() => new Date('2024').valueOf());
88-
8973
const mockNotification = mockSingleNotification;
9074
mockNotification.subject.user = null;
9175

@@ -99,7 +83,7 @@ describe('renderer/components/notifications/NotificationFooter.tsx', () => {
9983
});
10084

10185
it('should open notification user profile', async () => {
102-
const mockOpenExternalLink = jest
86+
const openExternalLinkSpy = jest
10387
.spyOn(comms, 'openExternalLink')
10488
.mockImplementation();
10589

@@ -125,8 +109,8 @@ describe('renderer/components/notifications/NotificationFooter.tsx', () => {
125109

126110
await userEvent.click(screen.getByTestId('view-profile'));
127111

128-
expect(mockOpenExternalLink).toHaveBeenCalledTimes(1);
129-
expect(mockOpenExternalLink).toHaveBeenCalledWith(
112+
expect(openExternalLinkSpy).toHaveBeenCalledTimes(1);
113+
expect(openExternalLinkSpy).toHaveBeenCalledWith(
130114
props.notification.subject.user.html_url,
131115
);
132116
});

src/renderer/components/notifications/NotificationHeader.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('renderer/components/notifications/NotificationHeader.tsx', () => {
7171
});
7272

7373
it('should open notification user profile - group by date', async () => {
74-
const mockOpenExternalLink = jest
74+
const openExternalLinkSpy = jest
7575
.spyOn(comms, 'openExternalLink')
7676
.mockImplementation();
7777

@@ -85,8 +85,8 @@ describe('renderer/components/notifications/NotificationHeader.tsx', () => {
8585

8686
await userEvent.click(screen.getByTestId('view-repository'));
8787

88-
expect(mockOpenExternalLink).toHaveBeenCalledTimes(1);
89-
expect(mockOpenExternalLink).toHaveBeenCalledWith(
88+
expect(openExternalLinkSpy).toHaveBeenCalledTimes(1);
89+
expect(openExternalLinkSpy).toHaveBeenCalledWith(
9090
props.notification.repository.html_url,
9191
);
9292
});

0 commit comments

Comments
 (0)