test: create shared fixtures and mock configuration for all tests

This commit is contained in:
2026-04-18 17:43:09 +00:00
parent d994391834
commit b086fb172e

View File

@@ -1,6 +1,169 @@
import { expect, afterEach } from 'vitest' import { vi, beforeEach } from 'vitest'
import { cleanup } from '@testing-library/react' import '@testing-library/jest-dom'
afterEach(() => { // ============================================================================
cleanup() // VITEST GLOBALS
// ============================================================================
// Globals are configured in vitest.config.ts (globals: true)
// describe, it, expect, beforeEach, afterEach available without imports
// ============================================================================
// MOCK AXIOS
// ============================================================================
vi.mock('axios', () => ({
default: {
create: vi.fn(() => ({
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
interceptors: {
request: { use: vi.fn() },
response: { use: vi.fn() },
},
})),
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
},
}))
// ============================================================================
// MOCK HTML5-QRCODE
// ============================================================================
vi.mock('html5-qrcode', () => ({
Html5Qrcode: class {
constructor() {}
static getCameras = vi.fn().mockResolvedValue([
{ id: 'camera-1', label: 'Front Camera' },
])
requestCameraPermissions = vi.fn().mockResolvedValue(true)
start = vi.fn().mockResolvedValue(undefined)
stop = vi.fn().mockResolvedValue(undefined)
getState = vi.fn(() => 2) // SCANNING state
setZoom = vi.fn()
getZoomSettings = vi.fn(() => ({
maxZoom: 4,
zoomRatios: [1, 2, 3, 4],
}))
},
}))
// ============================================================================
// MOCK DEXIE (IndexedDB)
// ============================================================================
vi.mock('dexie', () => ({
default: class Database {
constructor() {
this.pending_operations = {
add: vi.fn().mockResolvedValue(1),
toArray: vi.fn().mockResolvedValue([]),
clear: vi.fn().mockResolvedValue(undefined),
where: vi.fn(function () {
return {
toArray: vi.fn().mockResolvedValue([]),
delete: vi.fn().mockResolvedValue(0),
}
}),
}
}
},
}))
// ============================================================================
// MOCK NEXT/ROUTER
// ============================================================================
vi.mock('next/router', () => ({
useRouter: () => ({
push: vi.fn(),
pathname: '/',
route: '/',
query: {},
asPath: '/',
}),
}))
// ============================================================================
// SHARED FIXTURES
// ============================================================================
export const mockUserToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
export const mockInvalidToken = 'invalid-token'
export const mockItemListResponse = [
{
id: 'item-1',
name: 'Widget A',
category: 'Electronics',
quantity: 10,
barcode: '1234567890',
partNumber: 'PART-001',
},
{
id: 'item-2',
name: 'Widget B',
category: 'Electronics',
quantity: 5,
barcode: '0987654321',
partNumber: 'PART-002',
},
{
id: 'item-3',
name: 'Component C',
category: 'Parts',
quantity: 0,
barcode: 'QR-12345',
partNumber: 'PART-003',
},
]
export const mockAIExtractionResponseGemini = {
name: 'Widget A',
category: 'Electronics',
quantity: 10,
partNumber: 'PART-001',
confidence: 0.95,
}
export const mockAIExtractionResponseClaude = {
name: 'Widget A',
category: 'Electronics',
quantity: 10,
partNumber: 'PART-001',
confidence: 0.92,
}
export const mockAdminConfig = {
identity: {
ldap_enabled: true,
ldap_server: 'ldap://example.com',
},
ai_provider: 'gemini',
api_key: 'mock-key',
}
export const mockOfflineSyncQueue = [
{
id: 1,
operation: 'checkin',
itemId: 'item-1',
quantity: 5,
timestamp: Date.now(),
},
]
// ============================================================================
// CLEANUP
// ============================================================================
beforeEach(() => {
// Reset all mocks before each test
vi.clearAllMocks()
}) })