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>
This commit is contained in:
81
tests/composables/useConfirm.test.ts
Normal file
81
tests/composables/useConfirm.test.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { useConfirm } from '~/composables/useConfirm'
|
||||
|
||||
describe('useConfirm', () => {
|
||||
it('returns confirm function and state', () => {
|
||||
const { confirm, confirmState, handleConfirm, handleCancel } = useConfirm()
|
||||
expect(typeof confirm).toBe('function')
|
||||
expect(typeof handleConfirm).toBe('function')
|
||||
expect(typeof handleCancel).toBe('function')
|
||||
expect(confirmState.open).toBe(false)
|
||||
})
|
||||
|
||||
it('opens modal with correct options', () => {
|
||||
const { confirm, confirmState } = useConfirm()
|
||||
// Don't await — we'll manually resolve
|
||||
confirm({ message: 'Delete this item?' })
|
||||
expect(confirmState.open).toBe(true)
|
||||
expect(confirmState.message).toBe('Delete this item?')
|
||||
expect(confirmState.title).toBe('Confirmation')
|
||||
expect(confirmState.confirmText).toBe('Supprimer')
|
||||
expect(confirmState.cancelText).toBe('Annuler')
|
||||
expect(confirmState.dangerous).toBe(true)
|
||||
// Clean up by canceling
|
||||
const { handleCancel } = useConfirm()
|
||||
handleCancel()
|
||||
})
|
||||
|
||||
it('resolves true on confirm', async () => {
|
||||
const { confirm, handleConfirm } = useConfirm()
|
||||
const promise = confirm({ message: 'Confirm?' })
|
||||
handleConfirm()
|
||||
const result = await promise
|
||||
expect(result).toBe(true)
|
||||
})
|
||||
|
||||
it('resolves false on cancel', async () => {
|
||||
const { confirm, handleCancel } = useConfirm()
|
||||
const promise = confirm({ message: 'Cancel?' })
|
||||
handleCancel()
|
||||
const result = await promise
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
|
||||
it('closes modal after confirm', async () => {
|
||||
const { confirm, confirmState, handleConfirm } = useConfirm()
|
||||
confirm({ message: 'Test' })
|
||||
expect(confirmState.open).toBe(true)
|
||||
handleConfirm()
|
||||
expect(confirmState.open).toBe(false)
|
||||
})
|
||||
|
||||
it('closes modal after cancel', async () => {
|
||||
const { confirm, confirmState, handleCancel } = useConfirm()
|
||||
confirm({ message: 'Test' })
|
||||
expect(confirmState.open).toBe(true)
|
||||
handleCancel()
|
||||
expect(confirmState.open).toBe(false)
|
||||
})
|
||||
|
||||
it('supports custom options', () => {
|
||||
const { confirm, confirmState, handleCancel } = useConfirm()
|
||||
confirm({
|
||||
title: 'Custom Title',
|
||||
message: 'Custom message',
|
||||
confirmText: 'Yes',
|
||||
cancelText: 'No',
|
||||
dangerous: false,
|
||||
})
|
||||
expect(confirmState.title).toBe('Custom Title')
|
||||
expect(confirmState.confirmText).toBe('Yes')
|
||||
expect(confirmState.cancelText).toBe('No')
|
||||
expect(confirmState.dangerous).toBe(false)
|
||||
handleCancel()
|
||||
})
|
||||
|
||||
it('shares state across calls (singleton)', () => {
|
||||
const a = useConfirm()
|
||||
const b = useConfirm()
|
||||
expect(a.confirmState).toBe(b.confirmState)
|
||||
})
|
||||
})
|
||||
83
tests/composables/useToast.test.ts
Normal file
83
tests/composables/useToast.test.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||
import { useToast } from '~/composables/useToast'
|
||||
|
||||
describe('useToast', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
const { clearAll } = useToast()
|
||||
clearAll()
|
||||
})
|
||||
|
||||
it('returns all expected functions', () => {
|
||||
const toast = useToast()
|
||||
expect(typeof toast.showToast).toBe('function')
|
||||
expect(typeof toast.showSuccess).toBe('function')
|
||||
expect(typeof toast.showError).toBe('function')
|
||||
expect(typeof toast.showWarning).toBe('function')
|
||||
expect(typeof toast.showInfo).toBe('function')
|
||||
expect(typeof toast.removeToast).toBe('function')
|
||||
expect(typeof toast.clearAll).toBe('function')
|
||||
})
|
||||
|
||||
it('adds a toast with correct properties', () => {
|
||||
const { showToast, toasts } = useToast()
|
||||
const id = showToast('Hello', 'info')
|
||||
expect(toasts.value).toHaveLength(1)
|
||||
expect(toasts.value[0].message).toBe('Hello')
|
||||
expect(toasts.value[0].type).toBe('info')
|
||||
expect(toasts.value[0].visible).toBe(true)
|
||||
expect(toasts.value[0].id).toBe(id)
|
||||
})
|
||||
|
||||
it('showSuccess creates a success toast', () => {
|
||||
const { showSuccess, toasts } = useToast()
|
||||
showSuccess('Saved!')
|
||||
expect(toasts.value[0].type).toBe('success')
|
||||
expect(toasts.value[0].message).toBe('Saved!')
|
||||
})
|
||||
|
||||
it('showError creates an error toast', () => {
|
||||
const { showError, toasts } = useToast()
|
||||
showError('Failed!')
|
||||
expect(toasts.value[0].type).toBe('error')
|
||||
})
|
||||
|
||||
it('showWarning creates a warning toast', () => {
|
||||
const { showWarning, toasts } = useToast()
|
||||
showWarning('Caution!')
|
||||
expect(toasts.value[0].type).toBe('warning')
|
||||
})
|
||||
|
||||
it('limits to MAX_TOASTS (3)', () => {
|
||||
const { showToast, toasts } = useToast()
|
||||
showToast('A', 'info')
|
||||
showToast('B', 'info')
|
||||
showToast('C', 'info')
|
||||
showToast('D', 'info')
|
||||
expect(toasts.value).toHaveLength(3)
|
||||
expect(toasts.value[0].message).toBe('B')
|
||||
expect(toasts.value[2].message).toBe('D')
|
||||
})
|
||||
|
||||
it('clearAll removes all toasts', () => {
|
||||
const { showToast, toasts, clearAll } = useToast()
|
||||
showToast('A', 'info')
|
||||
showToast('B', 'info')
|
||||
expect(toasts.value.length).toBeGreaterThan(0)
|
||||
clearAll()
|
||||
expect(toasts.value).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('shares state across calls (singleton)', () => {
|
||||
const a = useToast()
|
||||
const b = useToast()
|
||||
expect(a.toasts).toBe(b.toasts)
|
||||
})
|
||||
|
||||
it('removeToast sets visible to false', () => {
|
||||
const { showToast, toasts, removeToast } = useToast()
|
||||
const id = showToast('Test', 'info')
|
||||
removeToast(id)
|
||||
expect(toasts.value[0].visible).toBe(false)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user