Makelore 2.0 initial clean snapshot
This commit is contained in:
100
tests/setup.ts
Normal file
100
tests/setup.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Vitest Test Setup
|
||||
* Global test configuration and mocks
|
||||
*/
|
||||
import { vi } from 'vitest';
|
||||
import '@testing-library/jest-dom';
|
||||
|
||||
// Provide a minimal `electron` mock so tests that transitively import
|
||||
// main-process code (logger, store, etc.) don't blow up when the Electron
|
||||
// binary is not present (e.g. CI with ELECTRON_SKIP_BINARY_DOWNLOAD=1).
|
||||
// Individual test files can override with their own vi.mock('electron', ...).
|
||||
vi.mock('electron', () => ({
|
||||
app: {
|
||||
getPath: vi.fn().mockReturnValue('/tmp/niancode-test'),
|
||||
getVersion: vi.fn().mockReturnValue('0.0.0-test'),
|
||||
getName: vi.fn().mockReturnValue('niancode-test'),
|
||||
isPackaged: false,
|
||||
isReady: vi.fn().mockResolvedValue(true),
|
||||
on: vi.fn(),
|
||||
off: vi.fn(),
|
||||
quit: vi.fn(),
|
||||
whenReady: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
BrowserWindow: vi.fn(),
|
||||
ipcMain: { on: vi.fn(), handle: vi.fn(), removeHandler: vi.fn() },
|
||||
dialog: { showOpenDialog: vi.fn(), showMessageBox: vi.fn() },
|
||||
shell: { openExternal: vi.fn() },
|
||||
session: { defaultSession: { webRequest: { onBeforeSendHeaders: vi.fn() } } },
|
||||
utilityProcess: {},
|
||||
}));
|
||||
|
||||
// Mock window.electron API
|
||||
const mockElectron = {
|
||||
ipcRenderer: {
|
||||
invoke: vi.fn(),
|
||||
on: vi.fn(),
|
||||
once: vi.fn(),
|
||||
off: vi.fn(),
|
||||
},
|
||||
openExternal: vi.fn(),
|
||||
platform: 'darwin',
|
||||
isDev: true,
|
||||
imageWorkspaceLocalDevelopment: false,
|
||||
};
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
Object.defineProperty(window, 'electron', {
|
||||
value: mockElectron,
|
||||
writable: true,
|
||||
});
|
||||
}
|
||||
|
||||
function createStorageMock(): Storage {
|
||||
const data = new Map<string, string>();
|
||||
return {
|
||||
get length() {
|
||||
return data.size;
|
||||
},
|
||||
clear: () => {
|
||||
data.clear();
|
||||
},
|
||||
getItem: (key) => data.get(key) ?? null,
|
||||
key: (index) => Array.from(data.keys())[index] ?? null,
|
||||
removeItem: (key) => {
|
||||
data.delete(key);
|
||||
},
|
||||
setItem: (key, value) => {
|
||||
data.set(key, value);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
Object.defineProperty(window, 'localStorage', {
|
||||
configurable: true,
|
||||
value: createStorageMock(),
|
||||
});
|
||||
}
|
||||
|
||||
// Mock matchMedia
|
||||
if (typeof window !== 'undefined') {
|
||||
Object.defineProperty(window, 'matchMedia', {
|
||||
writable: true,
|
||||
value: vi.fn().mockImplementation((query) => ({
|
||||
matches: false,
|
||||
media: query,
|
||||
onchange: null,
|
||||
addListener: vi.fn(),
|
||||
removeListener: vi.fn(),
|
||||
addEventListener: vi.fn(),
|
||||
removeEventListener: vi.fn(),
|
||||
dispatchEvent: vi.fn(),
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
// Reset mocks after each test
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
Reference in New Issue
Block a user