From fb99d49b9843b2450187c3ea2dbadd5283504720 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 10 Jun 2024 13:00:04 -0400 Subject: [PATCH] refactor: mock openExternalLink comms fn (#1210) refactor: shell mocks --- src/components/NotificationRow.test.tsx | 8 +++++--- src/components/Repository.test.tsx | 10 +++++----- src/components/Sidebar.test.tsx | 14 ++++++++------ src/routes/Accounts.test.tsx | 19 ++++++++++++------- src/routes/Settings.test.tsx | 13 ++++++++----- 5 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/components/NotificationRow.test.tsx b/src/components/NotificationRow.test.tsx index 515386923..e6f103df8 100644 --- a/src/components/NotificationRow.test.tsx +++ b/src/components/NotificationRow.test.tsx @@ -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'; @@ -460,6 +460,8 @@ describe('components/NotificationRow.tsx', () => { }); it('should open notification user profile', () => { + const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink'); + const props = { notification: { ...mockSingleNotification, @@ -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, ); }); diff --git a/src/components/Repository.test.tsx b/src/components/Repository.test.tsx index 0e539cc0a..8edcdb2f5 100644 --- a/src/components/Repository.test.tsx +++ b/src/components/Repository.test.tsx @@ -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', () => ({ @@ -20,8 +20,6 @@ describe('components/Repository.tsx', () => { beforeEach(() => { markRepoNotificationsRead.mockReset(); - - jest.spyOn(shell, 'openExternal'); }); it('should render itself & its children', () => { @@ -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( @@ -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', ); }); diff --git a/src/components/Sidebar.test.tsx b/src/components/Sidebar.test.tsx index 216114ce0..8c16f7ade 100644 --- a/src/components/Sidebar.test.tsx +++ b/src/components/Sidebar.test.tsx @@ -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'; @@ -19,7 +18,6 @@ describe('components/Sidebar.tsx', () => { beforeEach(() => { fetchNotifications.mockReset(); - jest.spyOn(shell, 'openExternal'); jest.spyOn(window, 'clearInterval'); }); @@ -127,6 +125,8 @@ describe('components/Sidebar.tsx', () => { }); it('opens github in the notifications page', () => { + const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink'); + render( { , ); 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', ); }); @@ -161,6 +161,8 @@ describe('components/Sidebar.tsx', () => { }); it('should open the gitify repository', () => { + const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink'); + render( @@ -169,8 +171,8 @@ describe('components/Sidebar.tsx', () => { , ); 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', ); }); diff --git a/src/routes/Accounts.test.tsx b/src/routes/Accounts.test.tsx index d8d99dc69..0d7693c02 100644 --- a/src/routes/Accounts.test.tsx +++ b/src/routes/Accounts.test.tsx @@ -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, @@ -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( { 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( { 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( { 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', ); }); diff --git a/src/routes/Settings.test.tsx b/src/routes/Settings.test.tsx index 814da7a8f..35e6b4a73 100644 --- a/src/routes/Settings.test.tsx +++ b/src/routes/Settings.test.tsx @@ -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'; @@ -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( { ), ); - 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', ); }); @@ -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( { 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', ); });