129 lines
5.1 KiB
TypeScript
129 lines
5.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 MailImagePicker from '../../src/components/mail/MailImagePicker.vue'
|
|
import { langFixture } from '../fixtures/typed'
|
|
import { API, mockApi, resetState, type Reply } from '../helpers'
|
|
|
|
const bundle = {
|
|
...langFixture.data,
|
|
'backend::lang.mail.picker.insert_button': { other: 'Insert image' },
|
|
'backend::lang.mail.picker.insert_title': { other: 'Insert an image from the library' },
|
|
'backend::lang.mail.picker.choose_title': { other: 'Choose an image' },
|
|
'backend::lang.mail.picker.search': { other: 'Search images' },
|
|
'backend::lang.mail.picker.insert': { other: 'Insert image' },
|
|
'backend::lang.mail.picker.use': { other: 'Use this image' },
|
|
'backend::lang.mail.picker.upload': { other: 'Upload image' },
|
|
'backend::lang.mail.picker.uploading': { other: 'Uploading…' },
|
|
'backend::lang.mail.picker.empty_title': { other: 'No images yet' },
|
|
'backend::lang.mail.picker.empty_upload': { other: 'Upload an image to use it in mail.' },
|
|
'backend::lang.mail.picker.empty_pick_only': { other: 'Images are added in Settings.' },
|
|
'backend::lang.mail.picker.no_results': { other: 'No images match “:query”.' },
|
|
'backend::lang.mail.picker.load_failed': { other: 'The image library could not be loaded.' },
|
|
'backend::lang.mail.picker.alt': { other: 'Alternative text' },
|
|
'backend::lang.mail.picker.alt_comment': { other: 'Describes the image.' },
|
|
'backend::lang.form.cancel': { other: 'Cancel' },
|
|
}
|
|
|
|
function listReply(opts: {
|
|
canUpload?: boolean
|
|
data?: unknown[]
|
|
status?: number
|
|
message?: string
|
|
} = {}): Reply {
|
|
if (opts.status && opts.status >= 400) {
|
|
return {
|
|
status: opts.status,
|
|
body: { error: { code: 'x', message: opts.message ?? 'failed', details: {} } },
|
|
}
|
|
}
|
|
return {
|
|
body: {
|
|
data: opts.data ?? [],
|
|
meta: { canUpload: opts.canUpload ?? false, total: (opts.data ?? []).length, page: 1, perPage: 40 },
|
|
},
|
|
}
|
|
}
|
|
|
|
beforeEach(() => {
|
|
resetState()
|
|
setBundle(bundle, 'en')
|
|
})
|
|
|
|
afterEach(() => {
|
|
document.body.innerHTML = ''
|
|
})
|
|
|
|
enableAutoUnmount(afterEach)
|
|
|
|
async function openPicker(mode: 'insert' | 'choose') {
|
|
const wrapper = mount(MailImagePicker, {
|
|
props: { open: false, mode },
|
|
attachTo: document.body,
|
|
})
|
|
await flushPromises()
|
|
await wrapper.setProps({ open: true })
|
|
await flushPromises()
|
|
return wrapper
|
|
}
|
|
|
|
describe('MailImagePicker', () => {
|
|
it('shows the upload row only when meta.canUpload is true', async () => {
|
|
mockApi({ [`GET ${API}/mail/images`]: listReply({ canUpload: false }) })
|
|
const wrapper = await openPicker('insert')
|
|
expect(document.querySelector('[data-mail-image-upload]')).toBeNull()
|
|
expect(document.body.textContent).toContain('Images are added in Settings.')
|
|
|
|
mockApi({ [`GET ${API}/mail/images`]: listReply({ canUpload: true }) })
|
|
await wrapper.setProps({ open: false })
|
|
await flushPromises()
|
|
await wrapper.setProps({ open: true })
|
|
await flushPromises()
|
|
expect(document.querySelector('[data-mail-image-upload]')).not.toBeNull()
|
|
expect(document.body.textContent).toContain('Upload an image to use it in mail.')
|
|
})
|
|
|
|
it('shows load failed copy when the library request fails', async () => {
|
|
mockApi({ [`GET ${API}/mail/images`]: listReply({ status: 500, message: 'boom' }) })
|
|
await openPicker('choose')
|
|
expect(document.querySelector('[data-mail-image-load-failed]')?.textContent).toContain(
|
|
'The image library could not be loaded.',
|
|
)
|
|
})
|
|
|
|
it('disables primary until a tile is chosen; insert prefills alt from title', async () => {
|
|
mockApi({
|
|
[`GET ${API}/mail/images`]: listReply({
|
|
canUpload: false,
|
|
data: [{ id: 9, fileName: 'logo.png', title: 'Logo', url: 'https://cdn.example.com/logo.png' }],
|
|
}),
|
|
})
|
|
const wrapper = await openPicker('insert')
|
|
const confirm = document.querySelector<HTMLButtonElement>('[data-mail-image-confirm]')
|
|
expect(confirm?.disabled).toBe(true)
|
|
document.querySelector<HTMLElement>('[data-mail-image-id="9"]')!.click()
|
|
await flushPromises()
|
|
expect(confirm?.disabled).toBe(false)
|
|
const alt = document.querySelector<HTMLInputElement>('[data-mail-image-alt]')
|
|
expect(alt?.value).toBe('Logo')
|
|
confirm!.click()
|
|
await flushPromises()
|
|
expect(wrapper.emitted('insert')?.at(-1)).toEqual([{ url: 'https://cdn.example.com/logo.png', alt: 'Logo' }])
|
|
})
|
|
|
|
it('choose mode emits the id and has no alt field', async () => {
|
|
mockApi({
|
|
[`GET ${API}/mail/images`]: listReply({
|
|
data: [{ id: 4, fileName: 'a.png', title: 'A', url: 'https://cdn.example.com/a.png' }],
|
|
}),
|
|
})
|
|
const wrapper = await openPicker('choose')
|
|
expect(document.querySelector('[data-mail-image-alt]')).toBeNull()
|
|
document.querySelector<HTMLElement>('[data-mail-image-id="4"]')!.click()
|
|
await flushPromises()
|
|
document.querySelector<HTMLButtonElement>('[data-mail-image-confirm]')!.click()
|
|
await flushPromises()
|
|
expect(wrapper.emitted('choose')?.at(-1)).toEqual([4])
|
|
})
|
|
})
|