Files
Inventory_frontend/tests/shared/apiHelpers.test.ts
Matthieu 634184c2be test: configure Vitest and add 54 unit tests (F6.1, F6.2)
Set up Vitest with happy-dom, mock Nuxt auto-imports via #imports alias.
Add tests for: inventory-types validators (9), apiHelpers (10),
modelUtils (18), useConfirm (8), useToast (9). All 54 tests pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-09 11:20:28 +01:00

51 lines
1.5 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { extractCollection } from '~/shared/utils/apiHelpers'
describe('extractCollection', () => {
it('returns the input if it is already an array', () => {
const items = [{ id: 1 }, { id: 2 }]
expect(extractCollection(items)).toEqual(items)
})
it('extracts from hydra:member', () => {
const payload = { 'hydra:member': [{ id: 1 }], 'hydra:totalItems': 1 }
expect(extractCollection(payload)).toEqual([{ id: 1 }])
})
it('extracts from member', () => {
const payload = { member: [{ id: 1 }, { id: 2 }] }
expect(extractCollection(payload)).toEqual([{ id: 1 }, { id: 2 }])
})
it('extracts from items', () => {
const payload = { items: [{ id: 1 }] }
expect(extractCollection(payload)).toEqual([{ id: 1 }])
})
it('extracts from data', () => {
const payload = { data: [{ id: 1 }] }
expect(extractCollection(payload)).toEqual([{ id: 1 }])
})
it('prefers member over hydra:member', () => {
const payload = { member: [{ id: 'member' }], 'hydra:member': [{ id: 'hydra' }] }
expect(extractCollection(payload)).toEqual([{ id: 'member' }])
})
it('returns empty array for null', () => {
expect(extractCollection(null)).toEqual([])
})
it('returns empty array for undefined', () => {
expect(extractCollection(undefined)).toEqual([])
})
it('returns empty array for empty object', () => {
expect(extractCollection({})).toEqual([])
})
it('returns empty array for string', () => {
expect(extractCollection('not an object')).toEqual([])
})
})