diff --git a/src/components/AllRead.test.tsx b/src/components/AllRead.test.tsx index da53fc8cd..cd12d4a19 100644 --- a/src/components/AllRead.test.tsx +++ b/src/components/AllRead.test.tsx @@ -2,18 +2,12 @@ import { render } from '@testing-library/react'; import { AllRead } from './AllRead'; describe('components/AllRead.tsx', () => { - const originalMathRandom = Math.random; - // The read emoji randomly rotates, but then the snapshots would never match // Have to make it consistent so the emojis are always the same beforeEach(() => { global.Math.random = jest.fn(() => 0.1); }); - afterEach(() => { - global.Math.random = originalMathRandom; - }); - it('should render itself & its children', () => { const tree = render(); diff --git a/src/components/RepositoryNotifications.test.tsx b/src/components/RepositoryNotifications.test.tsx index 7f1024d94..1e67b7ee7 100644 --- a/src/components/RepositoryNotifications.test.tsx +++ b/src/components/RepositoryNotifications.test.tsx @@ -23,8 +23,8 @@ describe('components/Repository.tsx', () => { repoNotifications: mockGitHubNotifications, }; - beforeEach(() => { - markRepoNotificationsRead.mockReset(); + afterEach(() => { + jest.clearAllMocks(); }); it('should render itself & its children', () => { diff --git a/src/components/Sidebar.test.tsx b/src/components/Sidebar.test.tsx index 66ebbf244..0d2588e7b 100644 --- a/src/components/Sidebar.test.tsx +++ b/src/components/Sidebar.test.tsx @@ -15,12 +15,7 @@ jest.mock('react-router-dom', () => ({ describe('components/Sidebar.tsx', () => { const fetchNotifications = jest.fn(); - - beforeEach(() => { - fetchNotifications.mockReset(); - - jest.spyOn(window, 'clearInterval'); - }); + const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink'); afterEach(() => { jest.clearAllMocks(); @@ -39,6 +34,7 @@ describe('components/Sidebar.tsx', () => { , ); + expect(tree).toMatchSnapshot(); }); @@ -52,9 +48,127 @@ describe('components/Sidebar.tsx', () => { , ); + expect(tree).toMatchSnapshot(); }); + it('should open the gitify repository', () => { + render( + + + + + , + ); + + fireEvent.click(screen.getByTestId('gitify-logo')); + + expect(openExternalLinkMock).toHaveBeenCalledTimes(1); + expect(openExternalLinkMock).toHaveBeenCalledWith( + 'https://github.com/gitify-app/gitify', + ); + }); + + describe('quick links', () => { + describe('notifications icon', () => { + it('when there are 0 notifications', () => { + render( + + + + + , + ); + + const notificationsIcon = screen.getByTitle('0 Unread Notifications'); + + expect(notificationsIcon.className).toContain('text-white'); + expect(notificationsIcon.childNodes.length).toBe(1); + expect(notificationsIcon.childNodes[0].nodeName).toBe('svg'); + + fireEvent.click(screen.getByLabelText('0 Unread Notifications')); + + expect(openExternalLinkMock).toHaveBeenCalledTimes(1); + expect(openExternalLinkMock).toHaveBeenCalledWith( + 'https://github.com/notifications', + ); + }); + + it('when there are more than 0 notifications', () => { + render( + + + + + , + ); + + const notificationsIcon = screen.getByTitle('4 Unread Notifications'); + + expect(notificationsIcon.className).toContain(IconColor.GREEN); + expect(notificationsIcon.childNodes.length).toBe(2); + expect(notificationsIcon.childNodes[0].nodeName).toBe('svg'); + expect(notificationsIcon.childNodes[1].nodeValue).toBe('4'); + + fireEvent.click(screen.getByLabelText('4 Unread Notifications')); + + expect(openExternalLinkMock).toHaveBeenCalledTimes(1); + expect(openExternalLinkMock).toHaveBeenCalledWith( + 'https://github.com/notifications', + ); + }); + }); + }); + + it('opens my github issues page', () => { + render( + + + + + , + ); + + fireEvent.click(screen.getByLabelText('My Issues')); + + expect(openExternalLinkMock).toHaveBeenCalledTimes(1); + expect(openExternalLinkMock).toHaveBeenCalledWith( + 'https://github.com/issues', + ); + }); + + it('opens my github pull requests page', () => { + render( + + + + + , + ); + + fireEvent.click(screen.getByLabelText('My Pull Requests')); + + expect(openExternalLinkMock).toHaveBeenCalledTimes(1); + expect(openExternalLinkMock).toHaveBeenCalledWith( + 'https://github.com/pulls', + ); + }); + describe('Refresh Notifications', () => { it('should refresh the notifications when status is not loading', () => { render( @@ -71,7 +185,7 @@ describe('components/Sidebar.tsx', () => { , ); - fetchNotifications.mockReset(); + fireEvent.click(screen.getByTitle('Refresh Notifications')); expect(fetchNotifications).toHaveBeenCalledTimes(1); @@ -92,7 +206,7 @@ describe('components/Sidebar.tsx', () => { , ); - fetchNotifications.mockReset(); + fireEvent.click(screen.getByTitle('Refresh Notifications')); expect(fetchNotifications).not.toHaveBeenCalled(); @@ -108,7 +222,9 @@ describe('components/Sidebar.tsx', () => { , ); + fireEvent.click(screen.getByTitle('Settings')); + expect(mockNavigate).toHaveBeenCalledWith('/settings'); }); @@ -120,77 +236,13 @@ describe('components/Sidebar.tsx', () => { , ); + fireEvent.click(screen.getByTitle('Settings')); + expect(mockNavigate).toHaveBeenCalledWith('/', { replace: true }); }); }); - it('opens github in the notifications page', () => { - const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink'); - - render( - - - - - , - ); - fireEvent.click(screen.getByLabelText('4 Unread Notifications')); - expect(openExternalLinkMock).toHaveBeenCalledTimes(1); - expect(openExternalLinkMock).toHaveBeenCalledWith( - 'https://github.com/notifications', - ); - }); - - it('opens my github issues page', () => { - const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink'); - - render( - - - - - , - ); - fireEvent.click(screen.getByLabelText('My Issues')); - expect(openExternalLinkMock).toHaveBeenCalledTimes(1); - expect(openExternalLinkMock).toHaveBeenCalledWith( - 'https://github.com/issues', - ); - }); - - it('opens my github pull requests page', () => { - const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink'); - - render( - - - - - , - ); - fireEvent.click(screen.getByLabelText('My Pull Requests')); - expect(openExternalLinkMock).toHaveBeenCalledTimes(1); - expect(openExternalLinkMock).toHaveBeenCalledWith( - 'https://github.com/pulls', - ); - }); - it('should quit the app', () => { const quitAppMock = jest.spyOn(comms, 'quitApp'); @@ -201,62 +253,9 @@ describe('components/Sidebar.tsx', () => { , ); - fireEvent.click(screen.getByTitle('Quit Gitify')); - expect(quitAppMock).toHaveBeenCalledTimes(1); - }); - - it('should open the gitify repository', () => { - const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink'); - - render( - - - - - , - ); - fireEvent.click(screen.getByTestId('gitify-logo')); - expect(openExternalLinkMock).toHaveBeenCalledTimes(1); - expect(openExternalLinkMock).toHaveBeenCalledWith( - 'https://github.com/gitify-app/gitify', - ); - }); - - describe('should render the notifications icon', () => { - it('when there are 0 notifications', () => { - render( - - - - - , - ); - - const notificationsIcon = screen.getByTitle('0 Unread Notifications'); - expect(notificationsIcon.className).toContain('text-white'); - expect(notificationsIcon.childNodes.length).toBe(1); - expect(notificationsIcon.childNodes[0].nodeName).toBe('svg'); - }); - it('when there are more than 0 notifications', () => { - render( - - - - - , - ); + fireEvent.click(screen.getByTitle('Quit Gitify')); - const notificationsIcon = screen.getByTitle('4 Unread Notifications'); - expect(notificationsIcon.className).toContain(IconColor.GREEN); - expect(notificationsIcon.childNodes.length).toBe(2); - expect(notificationsIcon.childNodes[0].nodeName).toBe('svg'); - expect(notificationsIcon.childNodes[1].nodeValue).toBe('4'); - }); + expect(quitAppMock).toHaveBeenCalledTimes(1); }); }); diff --git a/src/components/buttons/Button.test.tsx b/src/components/buttons/Button.test.tsx index 54b2e3827..b4fb3d490 100644 --- a/src/components/buttons/Button.test.tsx +++ b/src/components/buttons/Button.test.tsx @@ -13,8 +13,8 @@ describe('components/buttons/Button.tsx', () => { size: Size.MEDIUM, }; - beforeEach(() => { - openExternalMock.mockReset(); + afterEach(() => { + jest.clearAllMocks(); }); it('should render without icon', () => { diff --git a/src/context/App.test.tsx b/src/context/App.test.tsx index d68d37ff8..bc0e2d675 100644 --- a/src/context/App.test.tsx +++ b/src/context/App.test.tsx @@ -65,6 +65,10 @@ describe('context/App.tsx', () => { }); }); + afterEach(() => { + jest.clearAllMocks(); + }); + it('fetch notifications every minute', async () => { customRender(null); @@ -74,25 +78,23 @@ describe('context/App.tsx', () => { expect(fetchNotificationsMock).toHaveBeenCalledTimes(1), ); - fetchNotificationsMock.mockReset(); - act(() => { jest.advanceTimersByTime(Constants.FETCH_INTERVAL); return; }); - expect(fetchNotificationsMock).toHaveBeenCalledTimes(1); + expect(fetchNotificationsMock).toHaveBeenCalledTimes(2); act(() => { jest.advanceTimersByTime(Constants.FETCH_INTERVAL); return; }); - expect(fetchNotificationsMock).toHaveBeenCalledTimes(2); + expect(fetchNotificationsMock).toHaveBeenCalledTimes(3); act(() => { jest.advanceTimersByTime(Constants.FETCH_INTERVAL); return; }); - expect(fetchNotificationsMock).toHaveBeenCalledTimes(3); + expect(fetchNotificationsMock).toHaveBeenCalledTimes(4); }); it('should call fetchNotifications', async () => { @@ -131,8 +133,6 @@ describe('context/App.tsx', () => { const { getByText } = customRender(); - markNotificationReadMock.mockReset(); - fireEvent.click(getByText('Test Case')); expect(markNotificationReadMock).toHaveBeenCalledTimes(1); @@ -158,8 +158,6 @@ describe('context/App.tsx', () => { const { getByText } = customRender(); - markNotificationDoneMock.mockReset(); - fireEvent.click(getByText('Test Case')); expect(markNotificationDoneMock).toHaveBeenCalledTimes(1); @@ -185,8 +183,6 @@ describe('context/App.tsx', () => { const { getByText } = customRender(); - unsubscribeNotificationMock.mockReset(); - fireEvent.click(getByText('Test Case')); expect(unsubscribeNotificationMock).toHaveBeenCalledTimes(1); @@ -212,8 +208,6 @@ describe('context/App.tsx', () => { const { getByText } = customRender(); - markRepoNotificationsReadMock.mockReset(); - fireEvent.click(getByText('Test Case')); expect(markRepoNotificationsReadMock).toHaveBeenCalledTimes(1); @@ -239,8 +233,6 @@ describe('context/App.tsx', () => { const { getByText } = customRender(); - markRepoNotificationsDoneMock.mockReset(); - fireEvent.click(getByText('Test Case')); expect(markRepoNotificationsDoneMock).toHaveBeenCalledTimes(1); diff --git a/src/routes/Accounts.test.tsx b/src/routes/Accounts.test.tsx index dc9e3b923..573df05a9 100644 --- a/src/routes/Accounts.test.tsx +++ b/src/routes/Accounts.test.tsx @@ -20,7 +20,7 @@ jest.mock('react-router-dom', () => ({ })); describe('routes/Accounts.tsx', () => { - beforeEach(() => { + afterEach(() => { jest.clearAllMocks(); }); diff --git a/src/routes/Login.test.tsx b/src/routes/Login.test.tsx index 8c9855527..64e440a96 100644 --- a/src/routes/Login.test.tsx +++ b/src/routes/Login.test.tsx @@ -11,8 +11,8 @@ jest.mock('react-router-dom', () => ({ })); describe('routes/Login.tsx', () => { - beforeEach(() => { - mockNavigate.mockReset(); + afterEach(() => { + jest.clearAllMocks(); }); it('should render itself & its children', () => { diff --git a/src/routes/LoginWithOAuthApp.test.tsx b/src/routes/LoginWithOAuthApp.test.tsx index 011562677..4a4320ef0 100644 --- a/src/routes/LoginWithOAuthApp.test.tsx +++ b/src/routes/LoginWithOAuthApp.test.tsx @@ -18,9 +18,8 @@ describe('routes/LoginWithOAuthApp.tsx', () => { accounts: [], }; - beforeEach(() => { - openExternalMock.mockReset(); - mockNavigate.mockReset(); + afterEach(() => { + jest.clearAllMocks(); }); it('renders correctly', () => { diff --git a/src/routes/LoginWithPersonalAccessToken.test.tsx b/src/routes/LoginWithPersonalAccessToken.test.tsx index a6ea100c6..83c9c284d 100644 --- a/src/routes/LoginWithPersonalAccessToken.test.tsx +++ b/src/routes/LoginWithPersonalAccessToken.test.tsx @@ -24,10 +24,8 @@ describe('routes/LoginWithPersonalAccessToken.tsx', () => { const mockValidateToken = jest.fn(); - beforeEach(() => { - mockValidateToken.mockReset(); - openExternalMock.mockReset(); - mockNavigate.mockReset(); + afterEach(() => { + jest.clearAllMocks(); }); it('renders correctly', () => { diff --git a/src/routes/Settings.test.tsx b/src/routes/Settings.test.tsx index d6c2a2f23..67b876a88 100644 --- a/src/routes/Settings.test.tsx +++ b/src/routes/Settings.test.tsx @@ -22,7 +22,7 @@ describe('routes/Settings.tsx', () => { mockPlatform('darwin'); }); - beforeEach(() => { + afterEach(() => { jest.clearAllMocks(); }); diff --git a/src/utils/api/client.test.ts b/src/utils/api/client.test.ts index 7065150a3..930ee9c0d 100644 --- a/src/utils/api/client.test.ts +++ b/src/utils/api/client.test.ts @@ -26,7 +26,7 @@ const mockRepoSlug = 'gitify-app/notifications-test'; describe('utils/api/client.ts', () => { afterEach(() => { - jest.resetAllMocks(); + jest.clearAllMocks(); }); describe('getAuthenticatedUser', () => { diff --git a/src/utils/api/request.test.ts b/src/utils/api/request.test.ts index 6be72fb55..29937c585 100644 --- a/src/utils/api/request.test.ts +++ b/src/utils/api/request.test.ts @@ -9,7 +9,7 @@ const method = 'get'; describe('utils/api/request.ts', () => { afterEach(() => { - jest.resetAllMocks(); + jest.clearAllMocks(); }); it('should make a request with the correct parameters', async () => { @@ -44,7 +44,7 @@ describe('apiRequestAuth', () => { const token = 'yourAuthToken' as Token; afterEach(() => { - jest.resetAllMocks(); + jest.clearAllMocks(); }); it('should make an authenticated request with the correct parameters', async () => { diff --git a/src/utils/auth/utils.test.ts b/src/utils/auth/utils.test.ts index 9f839959e..59f01750a 100644 --- a/src/utils/auth/utils.test.ts +++ b/src/utils/auth/utils.test.ts @@ -20,8 +20,8 @@ describe('utils/auth/utils.ts', () => { describe('authGitHub', () => { const loadURLMock = jest.spyOn(browserWindow, 'loadURL'); - beforeEach(() => { - loadURLMock.mockReset(); + afterEach(() => { + jest.clearAllMocks(); }); it('should call authGitHub - success', async () => { diff --git a/src/utils/icons.test.ts b/src/utils/icons.test.ts index 7f1f7747f..9e991f512 100644 --- a/src/utils/icons.test.ts +++ b/src/utils/icons.test.ts @@ -263,6 +263,7 @@ describe('utils/icons.ts', () => { describe('getPullRequestReviewIcon', () => { let mockReviewSingleReviewer: GitifyPullRequestReview; let mockReviewMultipleReviewer: GitifyPullRequestReview; + beforeEach(() => { mockReviewSingleReviewer = { state: 'APPROVED', diff --git a/src/utils/subject.test.ts b/src/utils/subject.test.ts index 9bd3a3af6..5b030539f 100644 --- a/src/utils/subject.test.ts +++ b/src/utils/subject.test.ts @@ -400,6 +400,7 @@ describe('utils/subject.ts', () => { describe('Issues', () => { let mockNotification: Notification; + beforeEach(() => { mockNotification = partialMockNotification({ title: 'This is a mock issue',