Skip to content

Commit 3af635c

Browse files
committed
test: update test infrastructure for Tauri environment
- Add Tauri internals mock to vitest.setup.ts - Create mock for @tauri-apps/plugin-http - Add isTauriEnvironment and decryptValue mocks to tests using axios - Update snapshots for emoji rendering changes
1 parent 01a4b16 commit 3af635c

File tree

17 files changed

+155
-91
lines changed

17 files changed

+155
-91
lines changed

src/__helpers__/vitest.setup.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ class MockIntersectionObserver {
3434
(globalThis as Record<string, unknown>).IntersectionObserver =
3535
MockIntersectionObserver;
3636

37+
/**
38+
* Mock Tauri internals to make isTauriEnvironment() return true in tests
39+
*/
40+
(window as Record<string, unknown>).__TAURI_INTERNALS__ = {
41+
invoke: vi.fn(),
42+
};
43+
3744
/**
3845
* Gitify context bridge API
3946
*/
40-
(globalThis as Record<string, unknown>).gitify = {
47+
(window as Record<string, unknown>).gitify = {
4148
app: {
4249
version: vi.fn().mockResolvedValue('v0.0.1'),
4350
hide: vi.fn(),
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { vi } from 'vitest';
2+
3+
// Mock the Tauri HTTP plugin fetch function
4+
export const fetch = vi
5+
.fn()
6+
.mockImplementation(async (_url: string, _options?: RequestInit) => {
7+
// Return a mock Response object
8+
return {
9+
ok: true,
10+
status: 200,
11+
statusText: 'OK',
12+
headers: new Headers({
13+
'content-type': 'application/json',
14+
}),
15+
json: vi.fn().mockResolvedValue({}),
16+
text: vi.fn().mockResolvedValue(''),
17+
};
18+
});

src/components/__snapshots__/AllRead.test.tsx.snap

Lines changed: 4 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/__snapshots__/Oops.test.tsx.snap

Lines changed: 4 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/layout/__snapshots__/EmojiSplash.test.tsx.snap

Lines changed: 4 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/notifications/__snapshots__/AccountNotifications.test.tsx.snap

Lines changed: 6 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/primitives/__snapshots__/EmojiText.test.tsx.snap

Lines changed: 2 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/context/App.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ import * as tray from '../utils/tray';
1717
import { AppContext, type AppContextState, AppProvider } from './App';
1818
import { defaultSettings } from './defaults';
1919

20+
// Mock isTauriEnvironment to return false so axios is used instead of Tauri fetch
21+
vi.mock('../utils/environment', () => ({
22+
isTauriEnvironment: () => false,
23+
}));
24+
25+
// Mock decryptValue since isTauriEnvironment is false
26+
vi.mock('../utils/comms', async (importOriginal) => {
27+
const actual = await importOriginal<typeof import('../utils/comms')>();
28+
return {
29+
...actual,
30+
decryptValue: vi.fn().mockResolvedValue('decrypted'),
31+
encryptValue: vi.fn().mockResolvedValue('encrypted'),
32+
};
33+
});
34+
2035
vi.mock('../hooks/useNotifications');
2136

2237
// Helper to render a button that calls a context method when clicked

src/hooks/useNotifications.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ import { Errors } from '../utils/errors';
1414
import * as logger from '../utils/logger';
1515
import { useNotifications } from './useNotifications';
1616

17+
// Mock isTauriEnvironment to return false so axios is used instead of Tauri fetch
18+
vi.mock('../utils/environment', () => ({
19+
isTauriEnvironment: () => false,
20+
}));
21+
22+
// Mock decryptValue since isTauriEnvironment is false, it would return unchanged value
23+
vi.mock('../utils/comms', async (importOriginal) => {
24+
const actual = await importOriginal<typeof import('../utils/comms')>();
25+
return {
26+
...actual,
27+
decryptValue: vi.fn().mockResolvedValue('decrypted'),
28+
};
29+
});
30+
1731
describe('renderer/hooks/useNotifications.ts', () => {
1832
const rendererLogErrorSpy = vi
1933
.spyOn(logger, 'rendererLogError')

src/utils/api/client.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ import * as apiRequests from './request';
2020

2121
vi.mock('axios');
2222

23+
// Mock isTauriEnvironment to return false so axios is used instead of Tauri fetch
24+
vi.mock('../environment', () => ({
25+
isTauriEnvironment: () => false,
26+
}));
27+
28+
// Mock decryptValue since isTauriEnvironment is false, it would return unchanged value
29+
vi.mock('../comms', () => ({
30+
decryptValue: vi.fn().mockResolvedValue('decrypted'),
31+
}));
32+
2333
const mockGitHubHostname = 'github.com' as Hostname;
2434
const mockThreadId = '1234';
2535

0 commit comments

Comments
 (0)