test: improve test infrastructure and fix test assertions

- Add mockPapaParse helper to centralize CSV mocking
- Fix test assertions to match actual output (German month names)
- Add missing DOM elements to test setup (projectFile, csvFile, etc.)
- Update vitest to v4.0.14 for improved testing capabilities
- Make setupEventHandlers globally accessible for testing
- Use textContent instead of innerText for better consistency
- Fix autoLoadDefaultProject test to check all three fallback paths
- Add proper event handler setup in integration tests
- Improve error handling test assertions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-27 08:59:23 +01:00
parent bc756fa0cd
commit cf86b45b93
9 changed files with 414 additions and 37 deletions

View File

@@ -1,6 +1,6 @@
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { setupBasicDOM } from './setup.js'
import { createSampleProject, createSampleCSV, createSampleTemplate, mockFetch } from './testHelpers.js'
import { createSampleProject, createSampleCSV, createSampleTemplate, mockFetch, mockPapaParse } from './testHelpers.js'
// Import both engine and generator
const fs = await import('fs/promises')
@@ -33,21 +33,7 @@ describe('Timeline Integration', () => {
// Mock fetch calls in order: template, CSV
mockFetch(template)
mockFetch(csvData)
// Mock Papa.parse to process the CSV
global.Papa.parse.mockImplementation((text, options) => {
const lines = text.trim().split('\n')
const headers = lines[0].split(',')
const data = lines.slice(1).map(line => {
const values = line.split(',')
const obj = {}
headers.forEach((header, i) => {
obj[header] = values[i]
})
return obj
})
options.complete({ data })
})
mockPapaParse()
await timelineEngine.loadProjectConfigObject(config)
@@ -124,11 +110,11 @@ describe('Timeline Integration', () => {
// Mock fetch failures
global.fetch.mockRejectedValue(new Error('Network error'))
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await timelineEngine.autoLoadDefaultProject()
// Should not crash, just log warnings
// Should not crash, just log messages
expect(consoleSpy).toHaveBeenCalled()
consoleSpy.mockRestore()
@@ -142,6 +128,9 @@ describe('Timeline Integration', () => {
projectInput.id = 'projectInput'
document.body.appendChild(projectInput)
// Setup event handlers
window.setupEventHandlers()
// Mock file reading
const mockFile = new File([JSON.stringify(config)], 'project.json', { type: 'application/json' })
mockFile.text = vi.fn().mockResolvedValue(JSON.stringify(config))
@@ -149,10 +138,7 @@ describe('Timeline Integration', () => {
// Mock the fetch calls that loadProjectConfigObject will make
mockFetch(createSampleTemplate())
mockFetch(createSampleCSV())
global.Papa.parse.mockImplementation((text, options) => {
options.complete({ data: [] })
})
mockPapaParse()
// Simulate file selection
Object.defineProperty(projectInput, 'files', {
@@ -178,6 +164,9 @@ describe('Timeline Integration', () => {
csvInput.id = 'csvInput'
document.body.appendChild(csvInput)
// Setup event handlers
window.setupEventHandlers()
const csvContent = createSampleCSV()
const mockFile = new File([csvContent], 'data.csv', { type: 'text/csv' })
mockFile.text = vi.fn().mockResolvedValue(csvContent)