Skip to content

Commit 4d0c3a0

Browse files
authored
refactor: clear separation between mock utils (#2503)
refactor: mock utils Signed-off-by: Adam Setch <[email protected]>
1 parent f06c9ea commit 4d0c3a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+597
-597
lines changed

src/renderer/__mocks__/account-mocks.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ import type {
88
} from '../types';
99
import { mockGitifyUser } from './user-mocks';
1010

11+
export const mockGitHubAppAccount: Account = {
12+
platform: 'GitHub Cloud',
13+
method: 'GitHub App',
14+
token: 'token-987654321' as Token,
15+
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
16+
user: mockGitifyUser,
17+
hasRequiredScopes: true,
18+
};
19+
1120
export const mockPersonalAccessTokenAccount: Account = {
1221
platform: 'GitHub Cloud',
1322
method: 'Personal Access Token',
@@ -20,7 +29,7 @@ export const mockPersonalAccessTokenAccount: Account = {
2029
export const mockOAuthAccount: Account = {
2130
platform: 'GitHub Enterprise Server',
2231
method: 'OAuth App',
23-
token: '1234568790' as Token,
32+
token: 'token-1234568790' as Token,
2433
hostname: 'github.gitify.io' as Hostname,
2534
user: mockGitifyUser,
2635
hasRequiredScopes: true,
@@ -39,24 +48,13 @@ export const mockGitHubCloudAccount: Account = {
3948
export const mockGitHubEnterpriseServerAccount: Account = {
4049
platform: 'GitHub Enterprise Server',
4150
method: 'Personal Access Token',
42-
token: '1234568790' as Token,
51+
token: 'token-1234568790' as Token,
4352
hostname: 'github.gitify.io' as Hostname,
4453
user: mockGitifyUser,
4554
hasRequiredScopes: true,
4655
};
4756

48-
export const mockGitHubAppAccount: Account = {
49-
platform: 'GitHub Cloud',
50-
method: 'GitHub App',
51-
token: '987654321' as Token,
52-
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
53-
user: mockGitifyUser,
54-
hasRequiredScopes: true,
55-
};
56-
57-
export function createMockAccountWithError(
58-
error: GitifyError,
59-
): AccountNotifications {
57+
export function mockAccountWithError(error: GitifyError): AccountNotifications {
6058
return {
6159
account: mockGitHubCloudAccount,
6260
notifications: [],

src/renderer/__mocks__/notifications-mocks.ts

Lines changed: 168 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,209 @@
1-
import { Constants } from '../constants';
21
import type {
32
AccountNotifications,
43
GitifyNotification,
4+
GitifyNotificationUser,
5+
GitifyOwner,
56
GitifyReason,
67
GitifyRepository,
78
GitifySubject,
8-
Hostname,
99
Link,
1010
} from '../types';
1111
import {
12-
mockEnterpriseNotifications,
13-
mockGitHubNotifications,
14-
mockSingleNotification,
15-
} from '../utils/api/__mocks__/response-mocks';
16-
import {
12+
mockGitHubAppAccount,
1713
mockGitHubCloudAccount,
1814
mockGitHubEnterpriseServerAccount,
1915
} from './account-mocks';
20-
import { mockToken } from './state-mocks';
21-
import { mockGitifyUser } from './user-mocks';
2216

23-
export const mockAccountNotifications: AccountNotifications[] = [
17+
const mockGitHubOwner: GitifyOwner = {
18+
login: 'gitify-app',
19+
avatarUrl:
20+
'https://avatars.githubusercontent.com/u/133795385?s=200&v=4' as Link,
21+
type: 'User',
22+
};
23+
24+
const mockGitHubRepository: GitifyRepository = {
25+
name: 'notifications-test',
26+
fullName: 'gitify-app/notifications-test',
27+
owner: mockGitHubOwner,
28+
htmlUrl: 'https://github.com/gitify-app/notifications-test' as Link,
29+
};
30+
31+
const mockSubjectUser: GitifyNotificationUser = {
32+
login: 'gitify-app',
33+
htmlUrl: 'https://github.com/gitify-app' as Link,
34+
avatarUrl:
35+
'https://avatars.githubusercontent.com/u/133795385?s=200&v=4' as Link,
36+
type: 'User',
37+
};
38+
39+
/**
40+
* Mock Gitify Notifications for GitHub Cloud account
41+
*
42+
* 2 Notifications
43+
* Hostname: 'github.com'
44+
* Repository: 'gitify-app/notifications-test'
45+
*/
46+
export const mockGitHubCloudGitifyNotifications: GitifyNotification[] = [
2447
{
2548
account: mockGitHubCloudAccount,
26-
notifications: mockGitHubNotifications,
49+
order: 0,
50+
id: '138661096',
51+
unread: true,
52+
reason: {
53+
code: 'subscribed',
54+
title: 'Updated',
55+
description: "You're watching the repository.",
56+
},
57+
updatedAt: '2017-05-20T17:51:57Z',
58+
subject: {
59+
title: 'I am a robot and this is a test!',
60+
url: 'https://api.github.com/repos/gitify-app/notifications-test/issues/1' as Link,
61+
latestCommentUrl:
62+
'https://api.github.com/repos/gitify-app/notifications-test/issues/comments/302888448' as Link,
63+
type: 'Issue',
64+
state: 'OPEN',
65+
user: mockSubjectUser,
66+
reviews: [
67+
{
68+
state: 'APPROVED',
69+
users: ['octocat'],
70+
},
71+
{
72+
state: 'CHANGES_REQUESTED',
73+
users: ['gitify-app'],
74+
},
75+
{
76+
state: 'PENDING',
77+
users: ['gitify-user'],
78+
},
79+
],
80+
},
81+
repository: mockGitHubRepository,
82+
},
83+
{
84+
account: mockGitHubCloudAccount,
85+
order: 1,
86+
id: '148827438',
87+
unread: true,
88+
reason: {
89+
code: 'author',
90+
title: 'Authored',
91+
description: 'You created the thread.',
92+
},
93+
updatedAt: '2017-05-20T17:06:34Z',
94+
subject: {
95+
title: 'Improve the UI',
96+
url: 'https://api.github.com/repos/gitify-app/notifications-test/issues/4' as Link,
97+
latestCommentUrl:
98+
'https://api.github.com/repos/gitify-app/notifications-test/issues/comments/302885965' as Link,
99+
type: 'Issue',
100+
reviews: null,
101+
},
102+
repository: mockGitHubRepository,
103+
},
104+
];
105+
106+
const mockEnterpriseOwner: GitifyOwner = {
107+
login: 'myorg',
108+
avatarUrl: 'https://github.gitify.io/avatars/u/4?' as Link,
109+
type: 'Organization',
110+
};
111+
112+
const mockEnterpriseRepository: GitifyRepository = {
113+
name: 'notifications-test',
114+
fullName: 'myorg/notifications-test',
115+
owner: mockEnterpriseOwner,
116+
htmlUrl: 'https://github.gitify.io/myorg/notifications-test' as Link,
117+
};
118+
119+
/**
120+
* Mock Gitify Notifications for GitHub Enterprise account
121+
*
122+
* 2 Notifications
123+
* Hostname: 'github.gitify.io'
124+
* Repository: 'myorg/notifications-test'
125+
*/
126+
export const mockGithubEnterpriseGitifyNotifications: GitifyNotification[] = [
127+
{
128+
account: mockGitHubEnterpriseServerAccount,
129+
order: 0,
130+
id: '3',
131+
unread: true,
132+
reason: {
133+
code: 'subscribed',
134+
title: 'Updated',
135+
description: "You're watching the repository.",
136+
},
137+
updatedAt: '2017-05-20T13:02:48Z',
138+
subject: {
139+
title: 'Release 0.0.1',
140+
url: 'https://github.gitify.io/api/v3/repos/myorg/notifications-test/releases/3' as Link,
141+
latestCommentUrl:
142+
'https://github.gitify.io/api/v3/repos/myorg/notifications-test/releases/3' as Link,
143+
type: 'Release',
144+
reviews: null,
145+
},
146+
repository: mockEnterpriseRepository,
147+
},
148+
{
149+
account: mockGitHubEnterpriseServerAccount,
150+
order: 1,
151+
id: '4',
152+
unread: true,
153+
reason: {
154+
code: 'subscribed',
155+
title: 'Updated',
156+
description: "You're watching the repository.",
157+
},
158+
updatedAt: '2017-05-20T15:52:20Z',
159+
subject: {
160+
title: 'Bump Version',
161+
url: 'https://github.gitify.io/api/v3/repos/myorg/notifications-test/pulls/4' as Link,
162+
latestCommentUrl:
163+
'https://github.gitify.io/api/v3/repos/myorg/notifications-test/issues/comments/21' as Link,
164+
type: 'PullRequest',
165+
reviews: null,
166+
},
167+
repository: mockEnterpriseRepository,
168+
},
169+
];
170+
171+
export const mockGitifyNotification: GitifyNotification =
172+
mockGitHubCloudGitifyNotifications[0];
173+
174+
export const mockMultipleAccountNotifications: AccountNotifications[] = [
175+
{
176+
account: mockGitHubCloudAccount,
177+
notifications: mockGitHubCloudGitifyNotifications,
27178
error: null,
28179
},
29180
{
30181
account: mockGitHubEnterpriseServerAccount,
31-
notifications: mockEnterpriseNotifications,
182+
notifications: mockGithubEnterpriseGitifyNotifications,
32183
error: null,
33184
},
34185
];
35186

36187
export const mockSingleAccountNotifications: AccountNotifications[] = [
37188
{
38189
account: mockGitHubCloudAccount,
39-
notifications: [mockSingleNotification],
190+
notifications: [mockGitifyNotification],
40191
error: null,
41192
},
42193
];
43194

44-
export function createPartialMockNotification(
195+
export function mockPartialGitifyNotification(
45196
subject: Partial<GitifySubject>,
46197
repository?: Partial<GitifyRepository>,
47198
): GitifyNotification {
48199
const mockNotification: Partial<GitifyNotification> = {
49-
account: {
50-
method: 'Personal Access Token',
51-
platform: 'GitHub Cloud',
52-
hostname: Constants.GITHUB_API_BASE_URL as Hostname,
53-
token: mockToken,
54-
user: mockGitifyUser,
55-
hasRequiredScopes: true,
56-
},
200+
account: mockGitHubAppAccount,
57201
reason: {
58202
code: 'subscribed',
59203
title: 'Updated',
60204
description: "You're watching the repository.",
61205
} as GitifyReason,
206+
updatedAt: '2026-01-01T17:00:00Z',
62207
subject: subject as GitifySubject,
63208
repository: {
64209
name: 'notifications-test',
@@ -76,7 +221,7 @@ export function createPartialMockNotification(
76221
return mockNotification as GitifyNotification;
77222
}
78223

79-
export function createMockNotificationForRepoName(
224+
export function mockGitifyNotificationForRepoName(
80225
id: string,
81226
repoFullName: string | null,
82227
): GitifyNotification {
Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import type { GitifyNotificationUser, GitifyUser, Link } from '../types';
2-
import type { AuthorFieldsFragment } from '../utils/api/graphql/generated/graphql';
3-
import type { RawUser } from '../utils/api/types';
42

53
export const mockGitifyUser: GitifyUser = {
64
login: 'octocat',
@@ -9,18 +7,7 @@ export const mockGitifyUser: GitifyUser = {
97
avatar: 'https://avatars.githubusercontent.com/u/583231?v=4' as Link,
108
};
119

12-
export function createPartialMockUser(login: string): RawUser {
13-
const mockUser: Partial<RawUser> = {
14-
login: login,
15-
html_url: `https://github.com/${login}` as Link,
16-
avatar_url: 'https://avatars.githubusercontent.com/u/583231?v=4' as Link,
17-
type: 'User',
18-
};
19-
20-
return mockUser as RawUser;
21-
}
22-
23-
export function createMockGitifyNotificationUser(
10+
export function mockGitifyNotificationUser(
2411
login: string,
2512
): GitifyNotificationUser {
2613
return {
@@ -30,13 +17,3 @@ export function createMockGitifyNotificationUser(
3017
type: 'User',
3118
};
3219
}
33-
34-
/**
35-
* Creates a mock author for use in GraphQL response mocks.
36-
*/
37-
export function createMockGraphQLAuthor(login: string): AuthorFieldsFragment {
38-
return {
39-
...createMockGitifyNotificationUser(login),
40-
__typename: 'User',
41-
} as AuthorFieldsFragment;
42-
}

src/renderer/components/Sidebar.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import userEvent from '@testing-library/user-event';
33
import { MemoryRouter } from 'react-router-dom';
44

55
import { renderWithAppContext } from '../__helpers__/test-utils';
6-
import { mockAccountNotifications } from '../__mocks__/notifications-mocks';
6+
import { mockMultipleAccountNotifications } from '../__mocks__/notifications-mocks';
77
import { mockSettings } from '../__mocks__/state-mocks';
88
import * as comms from '../utils/comms';
99
import { Sidebar } from './Sidebar';
@@ -98,7 +98,7 @@ describe('renderer/components/Sidebar.tsx', () => {
9898
<Sidebar />
9999
</MemoryRouter>,
100100
{
101-
notifications: mockAccountNotifications,
101+
notifications: mockMultipleAccountNotifications,
102102
},
103103
);
104104

@@ -159,7 +159,7 @@ describe('renderer/components/Sidebar.tsx', () => {
159159
<Sidebar />
160160
</MemoryRouter>,
161161
{
162-
notifications: mockAccountNotifications,
162+
notifications: mockMultipleAccountNotifications,
163163
},
164164
);
165165

@@ -177,7 +177,7 @@ describe('renderer/components/Sidebar.tsx', () => {
177177
<Sidebar />
178178
</MemoryRouter>,
179179
{
180-
notifications: mockAccountNotifications,
180+
notifications: mockMultipleAccountNotifications,
181181
},
182182
);
183183

src/renderer/components/filters/FilterSection.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import userEvent from '@testing-library/user-event';
44
import { MarkGithubIcon } from '@primer/octicons-react';
55

66
import { renderWithAppContext } from '../../__helpers__/test-utils';
7-
import { mockAccountNotifications } from '../../__mocks__/notifications-mocks';
7+
import { mockMultipleAccountNotifications } from '../../__mocks__/notifications-mocks';
88
import { mockSettings } from '../../__mocks__/state-mocks';
99
import { stateFilter } from '../../utils/notifications/filters';
1010
import { FilterSection } from './FilterSection';
@@ -33,7 +33,7 @@ describe('renderer/components/filters/FilterSection.tsx', () => {
3333
...mockSettings,
3434
detailedNotifications: true,
3535
},
36-
notifications: mockAccountNotifications,
36+
notifications: mockMultipleAccountNotifications,
3737
},
3838
);
3939

@@ -57,7 +57,7 @@ describe('renderer/components/filters/FilterSection.tsx', () => {
5757
...mockSettings,
5858
detailedNotifications: false,
5959
},
60-
notifications: mockAccountNotifications,
60+
notifications: mockMultipleAccountNotifications,
6161
},
6262
);
6363

0 commit comments

Comments
 (0)