Skip to content

Commit

Permalink
refactor: mock openExternalLink comms fn (#1210)
Browse files Browse the repository at this point in the history
refactor: shell mocks
  • Loading branch information
setchy authored Jun 10, 2024
1 parent 9568069 commit fb99d49
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
8 changes: 5 additions & 3 deletions src/components/NotificationRow.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { shell } from 'electron';
import { mockAuth, mockSettings } from '../__mocks__/state-mocks';
import { AppContext } from '../context/App';
import type { Milestone, UserType } from '../typesGitHub';
import { mockSingleNotification } from '../utils/api/__mocks__/response-mocks';
import * as comms from '../utils/comms';
import * as helpers from '../utils/helpers';
import { NotificationRow } from './NotificationRow';

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

it('should open notification user profile', () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

const props = {
notification: {
...mockSingleNotification,
Expand Down Expand Up @@ -490,8 +492,8 @@ describe('components/NotificationRow.tsx', () => {
);

fireEvent.click(screen.getByTitle('View User Profile'));
expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith(
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
props.notification.subject.user.html_url,
);
});
Expand Down
10 changes: 5 additions & 5 deletions src/components/Repository.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { shell } from 'electron';
import { AppContext } from '../context/App';
import { mockGitHubNotifications } from '../utils/api/__mocks__/response-mocks';
import * as comms from '../utils/comms';
import { RepositoryNotifications } from './Repository';

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

beforeEach(() => {
markRepoNotificationsRead.mockReset();

jest.spyOn(shell, 'openExternal');
});

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

it('should open the browser when clicking on the repo name', () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

render(
<AppContext.Provider value={{}}>
<RepositoryNotifications {...props} />
Expand All @@ -42,8 +42,8 @@ describe('components/Repository.tsx', () => {

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

expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith(
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/gitify-app/notifications-test',
);
});
Expand Down
14 changes: 8 additions & 6 deletions src/components/Sidebar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { shell } from 'electron';
import { MemoryRouter } from 'react-router-dom';
import { mockAccountNotifications } from '../__mocks__/notifications-mocks';
import { mockSettings } from '../__mocks__/state-mocks';
Expand All @@ -19,7 +18,6 @@ describe('components/Sidebar.tsx', () => {
beforeEach(() => {
fetchNotifications.mockReset();

jest.spyOn(shell, 'openExternal');
jest.spyOn(window, 'clearInterval');
});

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

it('opens github in the notifications page', () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

render(
<AppContext.Provider
value={{
Expand All @@ -140,8 +140,8 @@ describe('components/Sidebar.tsx', () => {
</AppContext.Provider>,
);
fireEvent.click(screen.getByLabelText('4 Unread Notifications'));
expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith(
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/notifications',
);
});
Expand All @@ -161,6 +161,8 @@ describe('components/Sidebar.tsx', () => {
});

it('should open the gitify repository', () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

render(
<AppContext.Provider value={{ isLoggedIn: false, notifications: [] }}>
<MemoryRouter>
Expand All @@ -169,8 +171,8 @@ describe('components/Sidebar.tsx', () => {
</AppContext.Provider>,
);
fireEvent.click(screen.getByTestId('gitify-logo'));
expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith(
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/gitify-app/gitify',
);
});
Expand Down
19 changes: 12 additions & 7 deletions src/routes/Accounts.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { act, fireEvent, render, screen } from '@testing-library/react';
import { shell } from 'electron';
import { MemoryRouter } from 'react-router-dom';
import {
mockAuth,
Expand Down Expand Up @@ -72,6 +71,8 @@ describe('routes/Accounts.tsx', () => {

describe('Account interactions', () => {
it('open profile in external browser', async () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

await act(async () => {
render(
<AppContext.Provider
Expand All @@ -91,13 +92,15 @@ describe('routes/Accounts.tsx', () => {

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

expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith(
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/octocat',
);
});

it('open host in external browser', async () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

await act(async () => {
render(
<AppContext.Provider
Expand All @@ -117,11 +120,13 @@ describe('routes/Accounts.tsx', () => {

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

expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith('https://github.com');
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith('https://github.com');
});

it('open developer settings in external browser', async () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

await act(async () => {
render(
<AppContext.Provider
Expand All @@ -141,8 +146,8 @@ describe('routes/Accounts.tsx', () => {

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

expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith(
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/settings/tokens',
);
});
Expand Down
13 changes: 8 additions & 5 deletions src/routes/Settings.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { act, fireEvent, render, screen } from '@testing-library/react';
import { shell } from 'electron';
import { MemoryRouter } from 'react-router-dom';
import { mockAuth, mockSettings } from '../__mocks__/state-mocks';
import { mockPlatform } from '../__mocks__/utils';
Expand Down Expand Up @@ -201,6 +200,8 @@ describe('routes/Settings.tsx', () => {
});

it('should open official docs for showOnlyParticipating tooltip', async () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

await act(async () => {
render(
<AppContext.Provider
Expand Down Expand Up @@ -229,8 +230,8 @@ describe('routes/Settings.tsx', () => {
),
);

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

describe('Footer section', () => {
it('should open release notes', async () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

await act(async () => {
render(
<AppContext.Provider
Expand All @@ -498,8 +501,8 @@ describe('routes/Settings.tsx', () => {

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

expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(shell.openExternal).toHaveBeenCalledWith(
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/gitify-app/gitify/releases/tag/v0.0.1',
);
});
Expand Down

0 comments on commit fb99d49

Please sign in to comment.