paste images attachment

This commit is contained in:
Moon.eth
2026-07-29 20:06:01 +09:00
parent 17f385c1c3
commit 081460c7f6
2 changed files with 130 additions and 8 deletions
+77
View File
@@ -102,4 +102,81 @@ describe('Composer', () => {
await waitFor(() => expect(postStatus).toHaveBeenCalledOnce())
expect(postStatus.mock.calls[0][1].status).toBe('Keyboard-posted entry')
})
it('uploads a pasted clipboard image and attaches it to the post', async () => {
const pastedImage = new File(['image bytes'], 'pasted-image.png', { type: 'image/png' })
const uploadMedia = vi.fn().mockResolvedValue({
id: 'pasted-media',
type: 'image',
url: 'https://media.example/pasted-image.png',
preview_url: 'https://media.example/pasted-image-preview.png',
description: null,
})
const postStatus = vi.fn().mockResolvedValue(status())
const services = testServices({
session: session({
token: 'token',
me: account(),
signedIn: true,
instance: {
title: 'Test server',
version: '1.0.0',
configuration: { statuses: { max_media_attachments: 4 } },
},
}),
endpoints: { uploadMedia, postStatus },
})
const view = render(Composer, {
props: { initialText: 'A pasted picture' },
context: new Map([[APP_SERVICES, services]]),
})
const textarea = view.getByRole('textbox', { name: 'Entry text' })
await fireEvent.paste(textarea, {
clipboardData: {
items: [
{
kind: 'file',
type: 'image/png',
getAsFile: () => pastedImage,
},
],
files: [pastedImage],
},
})
await waitFor(() => expect(uploadMedia).toHaveBeenCalledOnce())
expect(uploadMedia.mock.calls[0][1]).toBe(pastedImage)
expect(view.container.querySelector('.composer-attachment img')).toHaveAttribute(
'src',
'https://media.example/pasted-image-preview.png',
)
await fireEvent.click(view.getByRole('button', { name: 'Post Entry' }))
await waitFor(() => expect(postStatus).toHaveBeenCalledOnce())
expect(postStatus.mock.calls[0][1].media_ids).toEqual(['pasted-media'])
})
it('leaves ordinary text-only paste alone', async () => {
const uploadMedia = vi.fn()
const services = testServices({
session: session({ token: 'token', me: account(), signedIn: true }),
endpoints: { uploadMedia },
})
const view = render(Composer, {
context: new Map([[APP_SERVICES, services]]),
})
const textarea = view.getByRole('textbox', { name: 'Entry text' })
const wasNotCancelled = await fireEvent.paste(textarea, {
clipboardData: {
items: [{ kind: 'string', type: 'text/plain', getAsFile: () => null }],
files: [],
},
})
expect(wasNotCancelled).toBe(true)
expect(uploadMedia).not.toHaveBeenCalled()
})
})