Skip to content

Commit 1d5e0d9

Browse files
committed
refactor(vitest): render utils under node project
Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent e82bc9d commit 1d5e0d9

14 files changed

Lines changed: 78 additions & 3 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Minimal DOM-globals setup for renderer .ts test files that run in the node project
3+
* but opt into a DOM environment via `// @vitest-environment happy-dom`.
4+
*
5+
* This file is added to the node project's `setupFiles` so that it runs for
6+
* every test in that project. The `typeof window !== 'undefined'` guard makes
7+
* it a no-op for tests that stay in the real node environment, while tests
8+
* that declare `// @vitest-environment happy-dom` at the top of their file
9+
* get the full `window.gitify` context bridge mock they need.
10+
*/
11+
12+
if (typeof window !== 'undefined') {
13+
window.gitify = {
14+
app: {
15+
version: vi.fn().mockResolvedValue('v0.0.1'),
16+
hide: vi.fn(),
17+
quit: vi.fn(),
18+
show: vi.fn(),
19+
},
20+
twemojiDirectory: vi.fn().mockResolvedValue('/mock/images/assets'),
21+
openExternalLink: vi.fn(),
22+
decryptValue: vi.fn().mockResolvedValue('decrypted'),
23+
encryptValue: vi.fn().mockResolvedValue('encrypted'),
24+
platform: {
25+
isLinux: vi.fn().mockReturnValue(false),
26+
isMacOS: vi.fn().mockReturnValue(true),
27+
isWindows: vi.fn().mockReturnValue(false),
28+
},
29+
zoom: {
30+
getLevel: vi.fn(),
31+
setLevel: vi.fn(),
32+
},
33+
tray: {
34+
updateColor: vi.fn(),
35+
updateTitle: vi.fn(),
36+
useAlternateIdleIcon: vi.fn(),
37+
useUnreadActiveIcon: vi.fn(),
38+
},
39+
notificationSoundPath: vi.fn(),
40+
onAuthCallback: vi.fn(),
41+
onResetApp: vi.fn(),
42+
onSystemThemeUpdate: vi.fn(),
43+
setAutoLaunch: vi.fn(),
44+
applyKeyboardShortcut: vi.fn().mockResolvedValue({ success: true }),
45+
raiseNativeNotification: vi.fn(),
46+
};
47+
48+
Object.defineProperty(navigator, 'clipboard', {
49+
value: {
50+
writeText: vi.fn().mockResolvedValue(undefined),
51+
readText: vi.fn().mockResolvedValue(''),
52+
},
53+
configurable: true,
54+
});
55+
}

src/renderer/hooks/useNotifications.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @vitest-environment happy-dom
12
import { act, renderHook, waitFor } from '@testing-library/react';
23

34
import {

src/renderer/stores/useFiltersStore.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @vitest-environment happy-dom
12
import { act, renderHook } from '@testing-library/react';
23

34
import type { SearchToken } from '../types';

src/renderer/utils/api/octokit.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @vitest-environment happy-dom
12
import {
23
mockGitHubAppAccount,
34
mockGitHubCloudAccount,

src/renderer/utils/auth/flows.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @vitest-environment happy-dom
12
// Use a hoist-safe mock factory for '@octokit/oauth-methods'
23
vi.mock('@octokit/oauth-methods', async () => {
34
const actual = await vi.importActual<typeof import('@octokit/oauth-methods')>(

src/renderer/utils/auth/utils.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @vitest-environment happy-dom
12
import { mockGitHubCloudAccount } from '../../__mocks__/account-mocks';
23
import { mockAuth } from '../../__mocks__/state-mocks';
34
import { mockRawUser } from '../api/__mocks__/response-mocks';

src/renderer/utils/notifications/filters/filter.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @vitest-environment happy-dom
12
import { mockPartialGitifyNotification } from '../../../__mocks__/notifications-mocks';
23
import { mockSettings } from '../../../__mocks__/state-mocks';
34

@@ -8,6 +9,10 @@ import type { GitifyOwner, Link, SearchToken } from '../../../types';
89
import { filterBaseNotifications, filterDetailedNotifications } from './filter';
910

1011
describe('renderer/utils/notifications/filters/filter.ts', () => {
12+
beforeEach(() => {
13+
useFiltersStore.getState().reset();
14+
});
15+
1116
describe('filterNotifications', () => {
1217
const mockNotifications = [
1318
mockPartialGitifyNotification(

src/renderer/utils/system/audio.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @vitest-environment happy-dom
12
import type { Percentage } from '../../types';
23

34
import { raiseSoundNotification } from './audio';

src/renderer/utils/system/comms.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @vitest-environment happy-dom
12
import { mockSettings } from '../../__mocks__/state-mocks';
23

34
import { type Link, OpenPreference } from '../../types';

src/renderer/utils/system/keyboardShortcut.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @vitest-environment happy-dom
12
import {
23
formatAcceleratorForDisplay,
34
keyboardEventToAccelerator,

0 commit comments

Comments
 (0)