// @vitest-environment nuxt
import { expect, describe, test } from 'vitest';
import { createPage } from '@nuxt/test-utils/e2e';
import { setup, $fetch } from '@nuxt/test-utils/e2e';
import fs from 'node:fs/promises';
import { registerEndpoint } from '@nuxt/test-utils/runtime';
registerEndpoint('/api/document/test-external', {
method: 'POST',
handler: () => {
console.log('test-external called');
return { success: true };
},
});
describe('documents', async () => {
await setup({
env: {
annotationURL: '/api/document/test-external',
},
},
});
test('upload external document', async () => {
const formData = new FormData();
const file = await fs.readFile('tests/data/documents/sample.pdf');
formData.append(
'file',
new File([file], 'sample.pdf', { type: 'application/pdf' }),
);
const res = await $fetch<{ id: string }>('/api/document/prepare', {
method: 'POST',
body: formData,
});
expect(res).toHaveProperty('id');
console.log(res);
const page = await createPage(`/pdf/viewer/${res.id}?external=true`);
await page.mouse.move(650, 350);
await page.mouse.down({ button: 'left' });
await page.mouse.move(750, 1000);
await page.mouse.up({ button: 'left' });
await page.getByRole('button', { name: 'Save' }).click();
await expect(page.getByRole('heading')).toContain(
'PDF successfully saved.',
);
});
});