Skip to content
Draft
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
55 changes: 55 additions & 0 deletions src/renderer/__helpers__/vitest.dom.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Minimal DOM-globals setup for renderer .ts test files that run in the node project
* but opt into a DOM environment via `// @vitest-environment happy-dom`.
*
* This file is added to the node project's `setupFiles` so that it runs for
* every test in that project. The `typeof window !== 'undefined'` guard makes
* it a no-op for tests that stay in the real node environment, while tests
* that declare `// @vitest-environment happy-dom` at the top of their file
* get the full `window.gitify` context bridge mock they need.
*/

if (typeof window !== 'undefined') {
window.gitify = {
app: {
version: vi.fn().mockResolvedValue('v0.0.1'),
hide: vi.fn(),
quit: vi.fn(),
show: vi.fn(),
},
twemojiDirectory: vi.fn().mockResolvedValue('/mock/images/assets'),
openExternalLink: vi.fn(),
decryptValue: vi.fn().mockResolvedValue('decrypted'),
encryptValue: vi.fn().mockResolvedValue('encrypted'),
platform: {
isLinux: vi.fn().mockReturnValue(false),
isMacOS: vi.fn().mockReturnValue(true),
isWindows: vi.fn().mockReturnValue(false),
},
zoom: {
getLevel: vi.fn(),
setLevel: vi.fn(),
},
tray: {
updateColor: vi.fn(),
updateTitle: vi.fn(),
useAlternateIdleIcon: vi.fn(),
useUnreadActiveIcon: vi.fn(),
},
notificationSoundPath: vi.fn(),
onAuthCallback: vi.fn(),
onResetApp: vi.fn(),
onSystemThemeUpdate: vi.fn(),
setAutoLaunch: vi.fn(),
applyKeyboardShortcut: vi.fn().mockResolvedValue({ success: true }),
raiseNativeNotification: vi.fn(),
};

Object.defineProperty(navigator, 'clipboard', {
value: {
writeText: vi.fn().mockResolvedValue(undefined),
readText: vi.fn().mockResolvedValue(''),
},
configurable: true,
});
}
1 change: 1 addition & 0 deletions src/renderer/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { act, renderHook, waitFor } from '@testing-library/react';

import {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/stores/useFiltersStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { act, renderHook } from '@testing-library/react';

import type { SearchToken } from '../types';
Expand Down
1 change: 1 addition & 0 deletions src/renderer/utils/api/octokit.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import {
mockGitHubAppAccount,
mockGitHubCloudAccount,
Expand Down
1 change: 1 addition & 0 deletions src/renderer/utils/auth/flows.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
// Use a hoist-safe mock factory for '@octokit/oauth-methods'
vi.mock('@octokit/oauth-methods', async () => {
const actual = await vi.importActual<typeof import('@octokit/oauth-methods')>(
Expand Down
1 change: 1 addition & 0 deletions src/renderer/utils/auth/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { mockGitHubCloudAccount } from '../../__mocks__/account-mocks';
import { mockAuth } from '../../__mocks__/state-mocks';
import { mockRawUser } from '../api/__mocks__/response-mocks';
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/utils/notifications/filters/filter.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { mockPartialGitifyNotification } from '../../../__mocks__/notifications-mocks';
import { mockSettings } from '../../../__mocks__/state-mocks';

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

describe('renderer/utils/notifications/filters/filter.ts', () => {
beforeEach(() => {
useFiltersStore.getState().reset();
});

describe('filterNotifications', () => {
const mockNotifications = [
mockPartialGitifyNotification(
Expand Down
1 change: 1 addition & 0 deletions src/renderer/utils/system/audio.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import type { Percentage } from '../../types';

import { raiseSoundNotification } from './audio';
Expand Down
1 change: 1 addition & 0 deletions src/renderer/utils/system/comms.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { mockSettings } from '../../__mocks__/state-mocks';

import { type Link, OpenPreference } from '../../types';
Expand Down
1 change: 1 addition & 0 deletions src/renderer/utils/system/keyboardShortcut.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import {
formatAcceleratorForDisplay,
keyboardEventToAccelerator,
Expand Down
1 change: 1 addition & 0 deletions src/renderer/utils/system/native.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { waitFor } from '@testing-library/react';

import {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/utils/system/tray.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { mockSettings } from '../../__mocks__/state-mocks';

import type { SettingsState } from '../../types';
Expand Down
1 change: 1 addition & 0 deletions src/renderer/utils/ui/zoom.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import type { Percentage } from '../../types';

import {
Expand Down
10 changes: 7 additions & 3 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,25 @@ export default defineConfig({
css: true,
include: [
'src/preload/**/*.test.{ts,tsx}',
'src/renderer/**/*.test.{ts,tsx}',
'src/renderer/**/*.test.tsx',
],
setupFiles: ['./src/renderer/__helpers__/vitest.setup.ts'],
},
},
{
// TODO - Opportunity in future to move some of the renderer util tests to node environment
extends: true,
test: {
name: 'node [main, shared]',
name: 'node [main, shared, renderer utils]',
environment: 'node',
include: [
'src/shared/**/*.test.{ts,tsx}',
'src/main/**/*.test.{ts,tsx}',
'src/renderer/**/*.test.ts',
],
// Conditionally sets up window.gitify for .ts files that opt into
// happy-dom via the `// @vitest-environment happy-dom` file docblock.
// The guard in the setup file makes it a no-op for pure node tests.
setupFiles: ['./src/renderer/__helpers__/vitest.dom.setup.ts'],
},
},
],
Expand Down
Loading