feat(series): seriesManage Pinia store + pure moveItem reorder helper
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { setActivePinia, createPinia } from 'pinia'
|
||||
import { useSeriesManageStore, moveItem } from '../src/stores/seriesManage.js'
|
||||
|
||||
function stubFetch(handler) {
|
||||
globalThis.fetch = vi.fn(async (url, init) => {
|
||||
const { status, body } = handler(url, init)
|
||||
return {
|
||||
ok: status >= 200 && status < 300,
|
||||
status, statusText: String(status),
|
||||
text: async () => (body == null ? '' : JSON.stringify(body))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
describe('seriesManage', () => {
|
||||
beforeEach(() => setActivePinia(createPinia()))
|
||||
afterEach(() => vi.restoreAllMocks())
|
||||
|
||||
it('moveItem reorders immutably', () => {
|
||||
expect(moveItem([1, 2, 3, 4], 3, 0)).toEqual([4, 1, 2, 3])
|
||||
expect(moveItem([1, 2, 3], 0, 2)).toEqual([2, 3, 1])
|
||||
})
|
||||
|
||||
it('load fetches pages + series', async () => {
|
||||
const s = useSeriesManageStore()
|
||||
stubFetch(() => ({
|
||||
status: 200,
|
||||
body: { series: { id: 7, name: 'V' },
|
||||
pages: [{ image_id: 1, page_number: 1, thumbnail_url: 't' }] }
|
||||
}))
|
||||
await s.load(7)
|
||||
expect(s.series).toEqual({ id: 7, name: 'V' })
|
||||
expect(s.pages.map(p => p.image_id)).toEqual([1])
|
||||
})
|
||||
|
||||
it('reorder posts the full ordered id list', async () => {
|
||||
const s = useSeriesManageStore()
|
||||
s.tagId = 7
|
||||
s.pages = [{ image_id: 1 }, { image_id: 2 }, { image_id: 3 }]
|
||||
const calls = []
|
||||
stubFetch((url, init) => {
|
||||
calls.push({ url, body: JSON.parse(init.body) })
|
||||
if (url.includes('/reorder')) return { status: 200, body: { ok: true } }
|
||||
return { status: 200, body: { series: { id: 7, name: 'V' }, pages: [] } }
|
||||
})
|
||||
await s.reorder([3, 1, 2])
|
||||
const r = calls.find(c => c.url.includes('/reorder'))
|
||||
expect(r.url).toContain('/api/series/7/reorder')
|
||||
expect(r.body).toEqual({ image_ids: [3, 1, 2] })
|
||||
})
|
||||
|
||||
it('addSelected posts picker selection then reloads', async () => {
|
||||
const s = useSeriesManageStore()
|
||||
s.tagId = 7
|
||||
s.pickerSelection = [9, 10]
|
||||
const calls = []
|
||||
stubFetch((url, init) => {
|
||||
calls.push({ url, body: init.body ? JSON.parse(init.body) : null })
|
||||
if (url.includes('/pages') && init.method === 'POST')
|
||||
return { status: 200, body: { added_count: 2 } }
|
||||
return { status: 200, body: { series: { id: 7, name: 'V' }, pages: [] } }
|
||||
})
|
||||
await s.addSelected()
|
||||
const add = calls.find(c => c.url.endsWith('/api/series/7/pages'))
|
||||
expect(add.body).toEqual({ image_ids: [9, 10] })
|
||||
expect(s.pickerSelection).toEqual([])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user