Files
timeline-svg/test/testHelpers.js
tegwick cf86b45b93 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>
2025-11-27 08:59:23 +01:00

89 lines
2.2 KiB
JavaScript

// Test helpers and utilities
export const createSampleProject = () => ({
name: "Test Project",
description: "A test project for unit testing",
dataSource: "test.csv",
stylesheet: "test.css",
svgTemplate: "test.svg",
settings: {
timelineMonths: 12
},
fieldMapping: {
id: "ID",
title: "Title",
lane: "Lane",
due: ["Due"]
}
})
export const createSampleItems = () => [
{
id: "T-1",
title: "First Task",
lane: "Development",
due: new Date("2025-01-15")
},
{
id: "T-2",
title: "Second Task",
lane: "Testing",
due: new Date("2025-02-20")
},
{
id: "T-3",
title: "Third Task",
lane: "Development",
due: new Date("2025-03-10")
}
]
export const createSampleCSV = () => `ID,Title,Lane,Due
T-1,First Task,Development,2025-01-15
T-2,Second Task,Testing,2025-02-20
T-3,Third Task,Development,2025-03-10`
export const createSampleTemplate = () => `<svg xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="#FFFFFF"/>
{{MONTHS}}
{{LANES}}
</svg>`
export const mockFetch = (data, ok = true) => {
global.fetch.mockResolvedValueOnce({
ok,
status: ok ? 200 : 404,
statusText: ok ? 'OK' : 'Not Found',
json: () => Promise.resolve(data),
text: () => Promise.resolve(typeof data === 'string' ? data : JSON.stringify(data))
})
}
// Helper to mock Papa.parse with proper CSV parsing
export const mockPapaParse = () => {
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 })
})
}
export const expectElementToHaveText = (selector, text) => {
const element = document.querySelector(selector)
expect(element).toBeTruthy()
expect(element.textContent).toContain(text)
}
export const expectSVGToContain = (selector, expectedContent) => {
const svg = document.querySelector(selector)
expect(svg).toBeTruthy()
expect(svg.outerHTML).toContain(expectedContent)
}