fix(admin): enforce image mode on drag-and-drop and update total on folder move

This commit is contained in:
Jakub Zych
2026-11-27 14:36:34 +01:00
parent eb0be8b8ad
commit 48c71d9225
6 changed files with 105 additions and 13 deletions

View File

@@ -199,4 +199,71 @@ describe('MediaBrowser', () => {
await flushPromises()
expect(calls.some((c) => c.method === 'DELETE' && c.url.includes('/media/items/1'))).toBe(true)
})
it('rejects non-image drag-and-drop uploads when mode is image', async () => {
const calls = mockApi({
[`GET ${API}/media/folders`]: { body: { data: [sampleFolder] } },
[`GET ${API}/media/items`]: {
body: {
data: [sampleItem],
meta: { page: 1, per_page: 24, total: 1, last_page: 1, can_manage: true },
},
},
[`POST ${API}/media/items`]: {
status: 201,
body: { data: { ...sampleItem, id: 9, file_name: 'manual.pdf', content_type: 'application/pdf', is_image: false } },
},
})
const wrapper = mount(MediaBrowser, {
props: { mode: 'image', selectable: true },
attachTo: document.body,
})
await flushPromises()
const pdfFile = new File(['%PDF-1.4'], 'manual.pdf', { type: 'application/pdf' })
await wrapper.find('[data-media-browser]').trigger('drop', {
dataTransfer: { files: [pdfFile] },
})
await flushPromises()
expect(calls.some((c) => c.method === 'POST' && c.url.includes('/media/items'))).toBe(false)
expect(wrapper.find('[data-media-upload-error]').exists()).toBe(true)
expect(wrapper.emitted('update:selected')).toBeUndefined()
})
it('decrements meta total when moving an item out of the active folder', async () => {
mockApi({
[`GET ${API}/media/folders`]: { body: { data: [sampleFolder] } },
[`GET ${API}/media/items`]: {
body: {
data: [sampleItem],
meta: { page: 1, per_page: 1, total: 2, last_page: 2, can_manage: true },
},
},
[`PUT ${API}/media/items/1`]: {
body: { data: { ...sampleItem, folder_id: null } },
},
})
const wrapper = mount(MediaBrowser, { attachTo: document.body })
await flushPromises()
// Navigate into folder 10
await wrapper.find('[data-media-folder="10"]').trigger('click')
await flushPromises()
expect(wrapper.find('[data-media-pagination]').text()).toContain('2')
// Open inspector, move item to Unfiled (clear folder), and save
await wrapper.find('[data-media-item-id="1"]').trigger('click')
await flushPromises()
await wrapper.find('[data-media-inspector-folder]').setValue('')
await wrapper.find('[data-media-save]').trigger('submit')
await flushPromises()
expect(wrapper.find('[data-media-item-id="1"]').exists()).toBe(false)
expect(wrapper.find('[data-media-inspector]').exists()).toBe(false)
// Total decremented from 2 to 1
expect(wrapper.find('[data-media-pagination]').text()).not.toContain('2')
})
})