135 lines
4.1 KiB
TypeScript
135 lines
4.1 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
|
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
|
|
import { setBundle } from '../../src/app/i18n'
|
|
import MediaPickerModal from '../../src/components/media/MediaPickerModal.vue'
|
|
import { langFixture } from '../fixtures/typed'
|
|
import { API, mockApi, resetState } from '../helpers'
|
|
|
|
const bundle = {
|
|
...langFixture.data,
|
|
'backend::lang.media.picker_title': { other: 'Select media' },
|
|
'backend::lang.media.picker_select': { other: 'Select' },
|
|
'backend::lang.media.picker_select_count': { other: 'Select (:count)' },
|
|
'backend::lang.media.alt': { other: 'Alt text' },
|
|
'backend::lang.media.folders': { other: 'Folders' },
|
|
'backend::lang.media.all_files': { other: 'All files' },
|
|
'backend::lang.media.root_folder': { other: 'Unfiled' },
|
|
'backend::lang.media.search_placeholder': { other: 'Search media…' },
|
|
'backend::lang.form.cancel': { other: 'Cancel' },
|
|
}
|
|
|
|
const itemA = {
|
|
id: 11,
|
|
short_id: 'aaa111222333',
|
|
file_name: 'alpha.png',
|
|
slug: 'alpha',
|
|
content_type: 'image/png',
|
|
file_size: 1024,
|
|
sha256: 'a'.repeat(64),
|
|
alt: 'Alpha alt',
|
|
title: 'Alpha',
|
|
caption: '',
|
|
url: '/media/aaa111222333/alpha.png',
|
|
thumb_url: '/media/aaa111222333/alpha-480w.webp',
|
|
is_image: true,
|
|
variants: [],
|
|
created_at: '2026-11-27T10:00:00Z',
|
|
updated_at: '2026-11-27T10:00:00Z',
|
|
}
|
|
|
|
const itemB = {
|
|
...itemA,
|
|
id: 12,
|
|
short_id: 'bbb111222333',
|
|
file_name: 'beta.pdf',
|
|
slug: 'beta',
|
|
content_type: 'application/pdf',
|
|
alt: '',
|
|
title: 'Beta PDF',
|
|
url: '/media/bbb111222333/beta.pdf',
|
|
thumb_url: '',
|
|
is_image: false,
|
|
}
|
|
|
|
beforeEach(() => {
|
|
resetState()
|
|
setBundle(bundle, 'en')
|
|
})
|
|
|
|
afterEach(() => {
|
|
document.body.innerHTML = ''
|
|
})
|
|
|
|
enableAutoUnmount(afterEach)
|
|
|
|
describe('MediaPickerModal', () => {
|
|
it('filters by defaultFolder slug and emits selected item with alt text', async () => {
|
|
const calls = mockApi({
|
|
[`GET ${API}/media/folders`]: {
|
|
body: {
|
|
data: [
|
|
{ id: 7, name: 'Journal', slug: 'journal', created_at: '', updated_at: '' },
|
|
],
|
|
},
|
|
},
|
|
[`GET ${API}/media/items`]: {
|
|
body: {
|
|
data: [itemA],
|
|
meta: { page: 1, per_page: 24, total: 1, last_page: 1, can_manage: false },
|
|
},
|
|
},
|
|
})
|
|
|
|
const wrapper = mount(MediaPickerModal, {
|
|
props: { open: true, mode: 'image', defaultFolder: 'journal', showAlt: true },
|
|
attachTo: document.body,
|
|
})
|
|
await flushPromises()
|
|
|
|
expect(calls.some((c) => c.url.includes('folder_id=7'))).toBe(true)
|
|
|
|
const confirm = document.querySelector<HTMLButtonElement>('[data-media-picker-confirm]')
|
|
expect(confirm?.disabled).toBe(true)
|
|
|
|
document.querySelector<HTMLElement>('[data-media-item-id="11"]')!.click()
|
|
await flushPromises()
|
|
|
|
expect(confirm?.disabled).toBe(false)
|
|
const altInput = document.querySelector<HTMLInputElement>('[data-media-picker-alt]')
|
|
expect(altInput?.value).toBe('Alpha alt')
|
|
|
|
confirm!.click()
|
|
await flushPromises()
|
|
expect(wrapper.emitted('select')?.at(-1)).toEqual([{ items: [itemA], alt: 'Alpha alt' }])
|
|
expect(wrapper.emitted('update:open')?.at(-1)).toEqual([false])
|
|
})
|
|
|
|
it('supports multiple selection in file mode', async () => {
|
|
mockApi({
|
|
[`GET ${API}/media/folders`]: { body: { data: [] } },
|
|
[`GET ${API}/media/items`]: {
|
|
body: {
|
|
data: [itemA, itemB],
|
|
meta: { page: 1, per_page: 24, total: 2, last_page: 1, can_manage: false },
|
|
},
|
|
},
|
|
})
|
|
|
|
const wrapper = mount(MediaPickerModal, {
|
|
props: { open: true, mode: 'file', multiple: true },
|
|
attachTo: document.body,
|
|
})
|
|
await flushPromises()
|
|
|
|
document.querySelector<HTMLElement>('[data-media-item-id="11"]')!.click()
|
|
document.querySelector<HTMLElement>('[data-media-item-id="12"]')!.click()
|
|
await flushPromises()
|
|
|
|
const confirm = document.querySelector<HTMLButtonElement>('[data-media-picker-confirm]')
|
|
expect(confirm?.textContent).toContain('2')
|
|
confirm!.click()
|
|
await flushPromises()
|
|
expect(wrapper.emitted('select')?.at(-1)).toEqual([{ items: [itemA, itemB], alt: '' }])
|
|
})
|
|
})
|