Skip to content

refactor(mocks): use consistent naming #1142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions src/__mocks__/mock-state.ts

This file was deleted.

24 changes: 24 additions & 0 deletions src/__mocks__/notifications-mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { AccountNotifications } from '../types';
import {
mockEnterpriseNotifications,
mockGitHubNotifications,
mockSingleNotification,
} from '../utils/api/__mocks__/response-mocks';

export const mockAccountNotifications: AccountNotifications[] = [
{
hostname: 'github.com',
notifications: mockGitHubNotifications,
},
{
hostname: 'github.gitify.io',
notifications: mockEnterpriseNotifications,
},
];

export const mockSingleAccountNotifications: AccountNotifications[] = [
{
hostname: 'github.com',
notifications: [mockSingleNotification],
},
];
40 changes: 40 additions & 0 deletions src/__mocks__/state-mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
type AuthState,
type EnterpriseAccount,
type GitifyUser,
type SettingsState,
Theme,
} from '../types';

export const mockEnterpriseAccounts: EnterpriseAccount[] = [
{
hostname: 'github.gitify.io',
token: '1234568790',
},
];

export const mockUser: GitifyUser = {
login: 'octocat',
name: 'Mona Lisa Octocat',
id: 123456789,
};

export const mockAccounts: AuthState = {
token: 'token-123-456',
enterpriseAccounts: mockEnterpriseAccounts,
user: mockUser,
};

export const mockSettings: SettingsState = {
participating: false,
playSound: true,
showNotifications: true,
showBots: true,
showNotificationsCountInTray: false,
openAtStartup: false,
theme: Theme.SYSTEM,
detailedNotifications: true,
markAsDoneOnOpen: false,
showAccountHostname: false,
delayNotificationState: false,
};
4 changes: 2 additions & 2 deletions src/components/AccountNotifications.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { render } from '@testing-library/react';
import { mockedGitHubNotifications } from '../__mocks__/mockedData';
import { mockGitHubNotifications } from '../utils/api/__mocks__/response-mocks';
import { AccountNotifications } from './AccountNotifications';

jest.mock('./Repository', () => ({
Expand All @@ -10,7 +10,7 @@ describe('components/AccountNotifications.tsx', () => {
it('should render itself (github.com with notifications)', () => {
const props = {
hostname: 'github.com',
notifications: mockedGitHubNotifications,
notifications: mockGitHubNotifications,
showAccountHostname: true,
};

Expand Down
32 changes: 16 additions & 16 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 { mockAccounts, mockSettings } from '../__mocks__/mock-state';
import { mockedSingleNotification } from '../__mocks__/mockedData';
import { mockAccounts, mockSettings } from '../__mocks__/state-mocks';
import { AppContext } from '../context/App';
import type { UserType } from '../typesGitHub';
import { mockSingleNotification } from '../utils/api/__mocks__/response-mocks';
import * as helpers from '../utils/helpers';
import { NotificationRow } from './NotificationRow';

Expand All @@ -22,7 +22,7 @@ describe('components/NotificationRow.tsx', () => {
.mockImplementation(() => new Date('2024').valueOf());

const props = {
notification: mockedSingleNotification,
notification: mockSingleNotification,
hostname: 'github.com',
};

Expand All @@ -35,7 +35,7 @@ describe('components/NotificationRow.tsx', () => {
.spyOn(global.Date, 'now')
.mockImplementation(() => new Date('2024').valueOf());

const mockNotification = mockedSingleNotification;
const mockNotification = mockSingleNotification;
mockNotification.last_read_at = null;

const props = {
Expand All @@ -52,7 +52,7 @@ describe('components/NotificationRow.tsx', () => {
.spyOn(global.Date, 'now')
.mockImplementation(() => new Date('2024').valueOf());

const mockNotification = mockedSingleNotification;
const mockNotification = mockSingleNotification;
mockNotification.subject.user = null;

const props = {
Expand All @@ -70,7 +70,7 @@ describe('components/NotificationRow.tsx', () => {
.spyOn(global.Date, 'now')
.mockImplementation(() => new Date('2024').valueOf());

const mockNotification = mockedSingleNotification;
const mockNotification = mockSingleNotification;
mockNotification.subject.comments = null;

const props = {
Expand All @@ -87,7 +87,7 @@ describe('components/NotificationRow.tsx', () => {
.spyOn(global.Date, 'now')
.mockImplementation(() => new Date('2024').valueOf());

const mockNotification = mockedSingleNotification;
const mockNotification = mockSingleNotification;
mockNotification.subject.comments = 1;

const props = {
Expand All @@ -104,7 +104,7 @@ describe('components/NotificationRow.tsx', () => {
.spyOn(global.Date, 'now')
.mockImplementation(() => new Date('2024').valueOf());

const mockNotification = mockedSingleNotification;
const mockNotification = mockSingleNotification;
mockNotification.subject.comments = 2;

const props = {
Expand All @@ -122,7 +122,7 @@ describe('components/NotificationRow.tsx', () => {
const removeNotificationFromState = jest.fn();

const props = {
notification: mockedSingleNotification,
notification: mockSingleNotification,
hostname: 'github.com',
};

Expand All @@ -147,7 +147,7 @@ describe('components/NotificationRow.tsx', () => {
const removeNotificationFromState = jest.fn();

const props = {
notification: mockedSingleNotification,
notification: mockSingleNotification,
hostname: 'github.com',
};

Expand All @@ -172,7 +172,7 @@ describe('components/NotificationRow.tsx', () => {
const markNotificationDone = jest.fn();

const props = {
notification: mockedSingleNotification,
notification: mockSingleNotification,
hostname: 'github.com',
};

Expand All @@ -197,7 +197,7 @@ describe('components/NotificationRow.tsx', () => {
const markNotificationRead = jest.fn();

const props = {
notification: mockedSingleNotification,
notification: mockSingleNotification,
hostname: 'github.com',
};

Expand All @@ -222,7 +222,7 @@ describe('components/NotificationRow.tsx', () => {
const markNotificationDone = jest.fn();

const props = {
notification: mockedSingleNotification,
notification: mockSingleNotification,
hostname: 'github.com',
};

Expand All @@ -247,7 +247,7 @@ describe('components/NotificationRow.tsx', () => {
const unsubscribeNotification = jest.fn();

const props = {
notification: mockedSingleNotification,
notification: mockSingleNotification,
hostname: 'github.com',
};

Expand All @@ -265,9 +265,9 @@ describe('components/NotificationRow.tsx', () => {
it('should open notification user profile', () => {
const props = {
notification: {
...mockedSingleNotification,
...mockSingleNotification,
subject: {
...mockedSingleNotification.subject,
...mockSingleNotification.subject,
user: {
login: 'some-user',
html_url: 'https://github.com/some-user',
Expand Down
4 changes: 2 additions & 2 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 { mockedGitHubNotifications } from '../__mocks__/mockedData';
import { AppContext } from '../context/App';
import { mockGitHubNotifications } from '../utils/api/__mocks__/response-mocks';
import { RepositoryNotifications } from './Repository';

jest.mock('./NotificationRow', () => ({
Expand All @@ -15,7 +15,7 @@ describe('components/Repository.tsx', () => {
const props = {
hostname: 'github.com',
repoName: 'gitify-app/notifications-test',
repoNotifications: mockedGitHubNotifications,
repoNotifications: mockGitHubNotifications,
};

beforeEach(() => {
Expand Down
12 changes: 6 additions & 6 deletions src/components/Sidebar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { ipcRenderer, shell } from 'electron';
import { MemoryRouter } from 'react-router-dom';
import { mockSettings } from '../__mocks__/mock-state';
import { mockedAccountNotifications } from '../__mocks__/mockedData';
import { mockAccountNotifications } from '../__mocks__/notifications-mocks';
import { mockSettings } from '../__mocks__/state-mocks';
import { AppContext } from '../context/App';
import { Sidebar } from './Sidebar';

Expand Down Expand Up @@ -32,7 +32,7 @@ describe('components/Sidebar.tsx', () => {
<AppContext.Provider
value={{
settings: mockSettings,
notifications: mockedAccountNotifications,
notifications: mockAccountNotifications,
}}
>
<MemoryRouter>
Expand All @@ -46,7 +46,7 @@ describe('components/Sidebar.tsx', () => {
it('should render itself & its children (logged out)', () => {
const tree = render(
<AppContext.Provider
value={{ isLoggedIn: false, notifications: mockedAccountNotifications }}
value={{ isLoggedIn: false, notifications: mockAccountNotifications }}
>
<MemoryRouter>
<Sidebar />
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('components/Sidebar.tsx', () => {
<AppContext.Provider
value={{
isLoggedIn: true,
notifications: mockedAccountNotifications,
notifications: mockAccountNotifications,
}}
>
<MemoryRouter>
Expand Down Expand Up @@ -195,7 +195,7 @@ describe('components/Sidebar.tsx', () => {
<AppContext.Provider
value={{
isLoggedIn: true,
notifications: mockedAccountNotifications,
notifications: mockAccountNotifications,
}}
>
<MemoryRouter>
Expand Down
3 changes: 2 additions & 1 deletion src/context/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { act, fireEvent, render, waitFor } from '@testing-library/react';
import { useContext } from 'react';
import { mockAccounts, mockSettings } from '../__mocks__/mock-state';

import { mockAccounts, mockSettings } from '../__mocks__/state-mocks';
import { useNotifications } from '../hooks/useNotifications';
import type { AuthState, SettingsState } from '../types';
import * as apiRequests from '../utils/api/request';
Expand Down
17 changes: 9 additions & 8 deletions src/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { act, renderHook, waitFor } from '@testing-library/react';
import axios, { AxiosError } from 'axios';
import nock from 'nock';
import { mockAccounts, mockSettings } from '../__mocks__/mock-state';
import { mockedNotificationUser, mockedUser } from '../__mocks__/mockedData';

import { mockAccounts, mockSettings, mockUser } from '../__mocks__/state-mocks';
import type { AuthState } from '../types';
import { mockNotificationUser } from '../utils/api/__mocks__/response-mocks';
import { Errors } from '../utils/constants';
import { useNotifications } from './useNotifications';

Expand Down Expand Up @@ -169,7 +170,7 @@ describe('hooks/useNotifications.ts', () => {
const accounts: AuthState = {
...mockAccounts,
enterpriseAccounts: [],
user: mockedUser,
user: mockUser,
};

const notifications = [
Expand Down Expand Up @@ -234,7 +235,7 @@ describe('hooks/useNotifications.ts', () => {
const accounts: AuthState = {
...mockAccounts,
enterpriseAccounts: [],
user: mockedUser,
user: mockUser,
};

const notifications = [
Expand Down Expand Up @@ -366,24 +367,24 @@ describe('hooks/useNotifications.ts', () => {
.reply(200, {
state: 'closed',
merged: true,
user: mockedNotificationUser,
user: mockNotificationUser,
});
nock('https://api.github.com')
.get('/repos/gitify-app/notifications-test/issues/3/comments')
.reply(200, {
user: mockedNotificationUser,
user: mockNotificationUser,
});
nock('https://api.github.com')
.get('/repos/gitify-app/notifications-test/pulls/4')
.reply(200, {
state: 'closed',
merged: false,
user: mockedNotificationUser,
user: mockNotificationUser,
});
nock('https://api.github.com')
.get('/repos/gitify-app/notifications-test/issues/4/comments')
.reply(200, {
user: mockedNotificationUser,
user: mockNotificationUser,
});

const { result } = renderHook(() => useNotifications());
Expand Down
4 changes: 2 additions & 2 deletions src/routes/LoginWithOAuthApp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fireEvent, render, screen } from '@testing-library/react';
import { ipcRenderer } from 'electron';
import { shell } from 'electron';
import { MemoryRouter } from 'react-router-dom';
import { mockedEnterpriseAccounts } from '../__mocks__/mockedData';
import { mockEnterpriseAccounts } from '../__mocks__/state-mocks';
import { AppContext } from '../context/App';
import type { AuthState } from '../types';
import { LoginWithOAuthApp, validate } from './LoginWithOAuthApp';
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {
<AppContext.Provider
value={{
accounts: {
enterpriseAccounts: mockedEnterpriseAccounts,
enterpriseAccounts: mockEnterpriseAccounts,
user: null,
},
}}
Expand Down
Loading