test: vitest fürs frontend
- Clip-Store: Versionsschutz, Sortierung, Reset, 409 als ClipConflictError - Sync-Store: Verbindungsaufbau, Ereignisse, Backoff, 4401, Watchdog, stop - Formatierung: Geräteerkennung aus dem User-Agent, Datumsformat Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4c5d910f54
commit
998e86c84f
6 files changed
+1099
-1
No files matched your search
@@ -0,0 +1,110 @@
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import type { Clip } from '@/api/types'
|
||||
|
||||
import { ClipConflictError, useClipsStore } from './clips'
|
||||
|
||||
function clip(overrides: Partial<Clip> = {}): Clip {
|
||||
return {
|
||||
id: 1,
|
||||
kind: 'text',
|
||||
title: null,
|
||||
content: 'hello',
|
||||
language: null,
|
||||
pinned: false,
|
||||
version: 1,
|
||||
created_at: '2026-10-25T10:00:00Z',
|
||||
updated_at: '2026-10-25T10:00:00Z',
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
function jsonResponse(status: number, body: unknown): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
}
|
||||
|
||||
describe('clips store', () => {
|
||||
beforeEach(() => setActivePinia(createPinia()))
|
||||
afterEach(() => vi.unstubAllGlobals())
|
||||
|
||||
it('ignores older versions arriving late', () => {
|
||||
const store = useClipsStore()
|
||||
store.upsert(clip({ version: 3, content: 'new' }))
|
||||
store.upsert(clip({ version: 2, content: 'old' }))
|
||||
|
||||
expect(store.items).toHaveLength(1)
|
||||
expect(store.items[0]!.content).toBe('new')
|
||||
})
|
||||
|
||||
it('replaces equal or newer versions', () => {
|
||||
const store = useClipsStore()
|
||||
store.upsert(clip({ version: 1 }))
|
||||
store.upsert(clip({ version: 2, content: 'edited' }))
|
||||
|
||||
expect(store.items[0]!.content).toBe('edited')
|
||||
expect(store.total).toBe(1)
|
||||
})
|
||||
|
||||
it('sorts pinned first, then most recently updated', () => {
|
||||
const store = useClipsStore()
|
||||
store.upsert(clip({ id: 1, updated_at: '2026-10-25T10:00:00Z' }))
|
||||
store.upsert(clip({ id: 2, updated_at: '2026-10-25T12:00:00Z' }))
|
||||
store.upsert(clip({ id: 3, updated_at: '2026-10-25T08:00:00Z', pinned: true }))
|
||||
|
||||
expect(store.items.map((c) => c.id)).toEqual([3, 2, 1])
|
||||
})
|
||||
|
||||
it('removes clips and keeps the total in step', () => {
|
||||
const store = useClipsStore()
|
||||
store.upsert(clip({ id: 1 }))
|
||||
store.upsert(clip({ id: 2 }))
|
||||
store.removeLocal(1)
|
||||
store.removeLocal(99)
|
||||
|
||||
expect(store.items.map((c) => c.id)).toEqual([2])
|
||||
expect(store.total).toBe(1)
|
||||
})
|
||||
|
||||
it('reset forgets everything', () => {
|
||||
const store = useClipsStore()
|
||||
store.upsert(clip())
|
||||
store.loaded = true
|
||||
store.reset()
|
||||
|
||||
expect(store.items).toEqual([])
|
||||
expect(store.total).toBe(0)
|
||||
expect(store.loaded).toBe(false)
|
||||
})
|
||||
|
||||
it('turns a 409 into ClipConflictError and adopts the server version', async () => {
|
||||
const store = useClipsStore()
|
||||
store.upsert(clip({ version: 1 }))
|
||||
const current = clip({ version: 2, content: 'from phone' })
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn(async () => jsonResponse(409, { detail: 'Konflikt', current })),
|
||||
)
|
||||
|
||||
const error = await store.update(1, { content: 'from laptop' }, 1).catch((e: unknown) => e)
|
||||
|
||||
expect(error).toBeInstanceOf(ClipConflictError)
|
||||
expect((error as ClipConflictError).current.content).toBe('from phone')
|
||||
expect(store.items[0]!.content).toBe('from phone')
|
||||
})
|
||||
|
||||
it('sends the expected version with updates', async () => {
|
||||
const store = useClipsStore()
|
||||
const fetchMock = vi.fn<typeof fetch>(async () => jsonResponse(200, clip({ version: 2 })))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
await store.update(1, { pinned: true }, 1)
|
||||
|
||||
const init = fetchMock.mock.calls[0]![1]!
|
||||
expect(init.method).toBe('PATCH')
|
||||
expect(JSON.parse(init.body as string)).toEqual({ pinned: true, version: 1 })
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user