Skip to content

Commit fb99d49

Browse files
authored
refactor: mock openExternalLink comms fn (#1210)
refactor: shell mocks
1 parent 9568069 commit fb99d49

File tree

5 files changed

+38
-26
lines changed

5 files changed

+38
-26
lines changed

src/components/NotificationRow.test.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { fireEvent, render, screen } from '@testing-library/react';
2-
import { shell } from 'electron';
32
import { mockAuth, mockSettings } from '../__mocks__/state-mocks';
43
import { AppContext } from '../context/App';
54
import type { Milestone, UserType } from '../typesGitHub';
65
import { mockSingleNotification } from '../utils/api/__mocks__/response-mocks';
6+
import * as comms from '../utils/comms';
77
import * as helpers from '../utils/helpers';
88
import { NotificationRow } from './NotificationRow';
99

@@ -460,6 +460,8 @@ describe('components/NotificationRow.tsx', () => {
460460
});
461461

462462
it('should open notification user profile', () => {
463+
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
464+
463465
const props = {
464466
notification: {
465467
...mockSingleNotification,
@@ -490,8 +492,8 @@ describe('components/NotificationRow.tsx', () => {
490492
);
491493

492494
fireEvent.click(screen.getByTitle('View User Profile'));
493-
expect(shell.openExternal).toHaveBeenCalledTimes(1);
494-
expect(shell.openExternal).toHaveBeenCalledWith(
495+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
496+
expect(openExternalLinkMock).toHaveBeenCalledWith(
495497
props.notification.subject.user.html_url,
496498
);
497499
});

src/components/Repository.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { fireEvent, render, screen } from '@testing-library/react';
2-
import { shell } from 'electron';
32
import { AppContext } from '../context/App';
43
import { mockGitHubNotifications } from '../utils/api/__mocks__/response-mocks';
4+
import * as comms from '../utils/comms';
55
import { RepositoryNotifications } from './Repository';
66

77
jest.mock('./NotificationRow', () => ({
@@ -20,8 +20,6 @@ describe('components/Repository.tsx', () => {
2020

2121
beforeEach(() => {
2222
markRepoNotificationsRead.mockReset();
23-
24-
jest.spyOn(shell, 'openExternal');
2523
});
2624

2725
it('should render itself & its children', () => {
@@ -34,6 +32,8 @@ describe('components/Repository.tsx', () => {
3432
});
3533

3634
it('should open the browser when clicking on the repo name', () => {
35+
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
36+
3737
render(
3838
<AppContext.Provider value={{}}>
3939
<RepositoryNotifications {...props} />
@@ -42,8 +42,8 @@ describe('components/Repository.tsx', () => {
4242

4343
fireEvent.click(screen.getByText(props.repoName));
4444

45-
expect(shell.openExternal).toHaveBeenCalledTimes(1);
46-
expect(shell.openExternal).toHaveBeenCalledWith(
45+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
46+
expect(openExternalLinkMock).toHaveBeenCalledWith(
4747
'https://github.com/gitify-app/notifications-test',
4848
);
4949
});

src/components/Sidebar.test.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { fireEvent, render, screen } from '@testing-library/react';
2-
import { shell } from 'electron';
32
import { MemoryRouter } from 'react-router-dom';
43
import { mockAccountNotifications } from '../__mocks__/notifications-mocks';
54
import { mockSettings } from '../__mocks__/state-mocks';
@@ -19,7 +18,6 @@ describe('components/Sidebar.tsx', () => {
1918
beforeEach(() => {
2019
fetchNotifications.mockReset();
2120

22-
jest.spyOn(shell, 'openExternal');
2321
jest.spyOn(window, 'clearInterval');
2422
});
2523

@@ -127,6 +125,8 @@ describe('components/Sidebar.tsx', () => {
127125
});
128126

129127
it('opens github in the notifications page', () => {
128+
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
129+
130130
render(
131131
<AppContext.Provider
132132
value={{
@@ -140,8 +140,8 @@ describe('components/Sidebar.tsx', () => {
140140
</AppContext.Provider>,
141141
);
142142
fireEvent.click(screen.getByLabelText('4 Unread Notifications'));
143-
expect(shell.openExternal).toHaveBeenCalledTimes(1);
144-
expect(shell.openExternal).toHaveBeenCalledWith(
143+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
144+
expect(openExternalLinkMock).toHaveBeenCalledWith(
145145
'https://github.com/notifications',
146146
);
147147
});
@@ -161,6 +161,8 @@ describe('components/Sidebar.tsx', () => {
161161
});
162162

163163
it('should open the gitify repository', () => {
164+
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
165+
164166
render(
165167
<AppContext.Provider value={{ isLoggedIn: false, notifications: [] }}>
166168
<MemoryRouter>
@@ -169,8 +171,8 @@ describe('components/Sidebar.tsx', () => {
169171
</AppContext.Provider>,
170172
);
171173
fireEvent.click(screen.getByTestId('gitify-logo'));
172-
expect(shell.openExternal).toHaveBeenCalledTimes(1);
173-
expect(shell.openExternal).toHaveBeenCalledWith(
174+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
175+
expect(openExternalLinkMock).toHaveBeenCalledWith(
174176
'https://github.com/gitify-app/gitify',
175177
);
176178
});

src/routes/Accounts.test.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react';
2-
import { shell } from 'electron';
32
import { MemoryRouter } from 'react-router-dom';
43
import {
54
mockAuth,
@@ -72,6 +71,8 @@ describe('routes/Accounts.tsx', () => {
7271

7372
describe('Account interactions', () => {
7473
it('open profile in external browser', async () => {
74+
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
75+
7576
await act(async () => {
7677
render(
7778
<AppContext.Provider
@@ -91,13 +92,15 @@ describe('routes/Accounts.tsx', () => {
9192

9293
fireEvent.click(screen.getByTitle('Open Profile'));
9394

94-
expect(shell.openExternal).toHaveBeenCalledTimes(1);
95-
expect(shell.openExternal).toHaveBeenCalledWith(
95+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
96+
expect(openExternalLinkMock).toHaveBeenCalledWith(
9697
'https://github.com/octocat',
9798
);
9899
});
99100

100101
it('open host in external browser', async () => {
102+
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
103+
101104
await act(async () => {
102105
render(
103106
<AppContext.Provider
@@ -117,11 +120,13 @@ describe('routes/Accounts.tsx', () => {
117120

118121
fireEvent.click(screen.getByTitle('Open Host'));
119122

120-
expect(shell.openExternal).toHaveBeenCalledTimes(1);
121-
expect(shell.openExternal).toHaveBeenCalledWith('https://github.com');
123+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
124+
expect(openExternalLinkMock).toHaveBeenCalledWith('https://github.com');
122125
});
123126

124127
it('open developer settings in external browser', async () => {
128+
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
129+
125130
await act(async () => {
126131
render(
127132
<AppContext.Provider
@@ -141,8 +146,8 @@ describe('routes/Accounts.tsx', () => {
141146

142147
fireEvent.click(screen.getByTitle('Open Developer Settings'));
143148

144-
expect(shell.openExternal).toHaveBeenCalledTimes(1);
145-
expect(shell.openExternal).toHaveBeenCalledWith(
149+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
150+
expect(openExternalLinkMock).toHaveBeenCalledWith(
146151
'https://github.com/settings/tokens',
147152
);
148153
});

src/routes/Settings.test.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react';
2-
import { shell } from 'electron';
32
import { MemoryRouter } from 'react-router-dom';
43
import { mockAuth, mockSettings } from '../__mocks__/state-mocks';
54
import { mockPlatform } from '../__mocks__/utils';
@@ -201,6 +200,8 @@ describe('routes/Settings.tsx', () => {
201200
});
202201

203202
it('should open official docs for showOnlyParticipating tooltip', async () => {
203+
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
204+
204205
await act(async () => {
205206
render(
206207
<AppContext.Provider
@@ -229,8 +230,8 @@ describe('routes/Settings.tsx', () => {
229230
),
230231
);
231232

232-
expect(shell.openExternal).toHaveBeenCalledTimes(1);
233-
expect(shell.openExternal).toHaveBeenCalledWith(
233+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
234+
expect(openExternalLinkMock).toHaveBeenCalledWith(
234235
'https://docs.github.com/en/account-and-profile/managing-subscriptions-and-notifications-on-github/setting-up-notifications/configuring-notifications#about-participating-and-watching-notifications',
235236
);
236237
});
@@ -481,6 +482,8 @@ describe('routes/Settings.tsx', () => {
481482

482483
describe('Footer section', () => {
483484
it('should open release notes', async () => {
485+
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
486+
484487
await act(async () => {
485488
render(
486489
<AppContext.Provider
@@ -498,8 +501,8 @@ describe('routes/Settings.tsx', () => {
498501

499502
fireEvent.click(screen.getByTitle('View release notes'));
500503

501-
expect(shell.openExternal).toHaveBeenCalledTimes(1);
502-
expect(shell.openExternal).toHaveBeenCalledWith(
504+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
505+
expect(openExternalLinkMock).toHaveBeenCalledWith(
503506
'https://github.com/gitify-app/gitify/releases/tag/v0.0.1',
504507
);
505508
});

0 commit comments

Comments
 (0)