Files
cc-switch/tests/msw/tauriMocks.ts
Jason 001ac14c85 test: add SettingsDialog integration tests and enhance MSW infrastructure
- Add comprehensive SettingsDialog integration tests with 3 test cases:
  * Load default settings from MSW
  * Import configuration and trigger success callback
  * Save settings and handle restart prompt
- Extend MSW handlers with settings-related endpoints:
  * get_settings/save_settings for settings management
  * app_config_dir_override for custom config directory
  * apply_claude_plugin_config for plugin integration
  * import/export config file operations
  * file/directory dialog mocks
- Add settings state management to MSW mock state:
  * Settings state with default values
  * appConfigDirOverride state
  * Reset logic in resetProviderState()
- Mock @tauri-apps/api/path for DirectorySettings tests
- Refactor App.test.tsx to focus on happy path scenarios:
  * Remove delete functionality test (covered in useProviderActions unit tests)
  * Reorganize test flow: settings → switch → usage → create → edit → switch → duplicate
  * Remove unnecessary state verifications
  * Simplify event testing

All tests passing: 4 integration tests + 12 unit tests
2025-10-25 19:59:31 +08:00

66 lines
1.6 KiB
TypeScript

import "cross-fetch/polyfill";
import { vi } from "vitest";
import { server } from "./server";
const TAURI_ENDPOINT = "http://tauri.local";
vi.mock("@tauri-apps/api/core", () => ({
invoke: async (command: string, payload: Record<string, unknown> = {}) => {
const response = await fetch(`${TAURI_ENDPOINT}/${command}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload ?? {}),
});
if (!response.ok) {
const text = await response.text();
throw new Error(text || `Invoke failed for ${command}`);
}
const text = await response.text();
if (!text) return undefined;
try {
return JSON.parse(text);
} catch {
return text;
}
},
}));
const listeners = new Map<
string,
Set<(event: { payload: unknown }) => void>
>();
const ensureListenerSet = (event: string) => {
if (!listeners.has(event)) {
listeners.set(event, new Set());
}
return listeners.get(event)!;
};
export const emitTauriEvent = (event: string, payload: unknown) => {
const handlers = listeners.get(event);
handlers?.forEach((handler) => handler({ payload }));
};
vi.mock("@tauri-apps/api/event", () => ({
listen: async (event: string, handler: (event: { payload: unknown }) => void) => {
const set = ensureListenerSet(event);
set.add(handler);
return () => {
set.delete(handler);
};
},
}));
// Ensure the MSW server is referenced so tree shaking doesn't remove imports
void server;
vi.mock("@tauri-apps/api/path", () => ({
homeDir: async () => "/home/mock",
join: async (...segments: string[]) => segments.join("/"),
}));