|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { expect, mergeTests } from '@playwright/test' |
| 7 | +import { test as formTest } from '../support/fixtures/form.ts' |
| 8 | +import { test as appNavigationTest } from '../support/fixtures/navigation.ts' |
| 9 | +import { test as randomUserTest } from '../support/fixtures/random-user.ts' |
| 10 | +import { QuestionType } from '../support/sections/QuestionType.ts' |
| 11 | + |
| 12 | +const test = mergeTests(randomUserTest, appNavigationTest, formTest) |
| 13 | + |
| 14 | +test.describe('Download form', () => { |
| 15 | + test.beforeEach(async ({ page }) => { |
| 16 | + await page.goto('apps/forms', { waitUntil: 'networkidle' }) |
| 17 | + await page.waitForURL(/apps\/forms\/$/) |
| 18 | + }) |
| 19 | + |
| 20 | + test('Download a form as JSON file', async ({ page, appNavigation, form }) => { |
| 21 | + await appNavigation.clickNewForm() |
| 22 | + await form.fillTitle('Download test') |
| 23 | + await page.getByRole('button', { name: 'Add a question' }).click() |
| 24 | + await page.getByRole('menuitem', { name: 'Checkboxes' }).click() |
| 25 | + await page |
| 26 | + .getByRole('textbox', { name: 'Title of question number 1' }) |
| 27 | + .fill('A') |
| 28 | + await page |
| 29 | + .getByRole('listitem', { name: 'Question number 1' }) |
| 30 | + .getByPlaceholder('Add a new answer option') |
| 31 | + .fill('B') |
| 32 | + await page |
| 33 | + .getByRole('listitem', { name: 'Question number 1' }) |
| 34 | + .getByPlaceholder('Add a new answer option') |
| 35 | + .press('Enter') |
| 36 | + await page.waitForTimeout(100) |
| 37 | + |
| 38 | + await page |
| 39 | + .getByRole('listitem', { name: 'Question number 1' }) |
| 40 | + .getByPlaceholder('Add a new answer option') |
| 41 | + .fill('C') |
| 42 | + await page |
| 43 | + .getByRole('listitem', { name: 'Question number 1' }) |
| 44 | + .getByPlaceholder('Add a new answer option') |
| 45 | + .press('Enter') |
| 46 | + await page.waitForTimeout(100) |
| 47 | + |
| 48 | + await page |
| 49 | + .getByRole('listitem', { name: 'Question number 1' }) |
| 50 | + .getByPlaceholder('Add a new answer option') |
| 51 | + .fill('D') |
| 52 | + await page |
| 53 | + .getByRole('listitem', { name: 'Question number 1' }) |
| 54 | + .getByPlaceholder('Add a new answer option') |
| 55 | + .press('Enter') |
| 56 | + await page.waitForTimeout(100) |
| 57 | + |
| 58 | + const downloadPromise = page.waitForEvent('download') |
| 59 | + |
| 60 | + // Hover over the form to make the actions button visible |
| 61 | + await appNavigation.getOwnForm('Download test').hover() |
| 62 | + await page.getByRole('button', { name: 'Form actions' }).click() |
| 63 | + |
| 64 | + // Click Download form in the popover menu |
| 65 | + await page.getByRole('menuitem', { name: 'Download form' }).click() |
| 66 | + |
| 67 | + const download = await downloadPromise |
| 68 | + expect(download.suggestedFilename()).toMatch(/^Download test.*\.json$/) |
| 69 | + |
| 70 | + const stream = await download.createReadStream() |
| 71 | + const json = await new Promise<any>((resolve, reject) => { |
| 72 | + let raw = '' |
| 73 | + stream.on('data', (chunk) => (raw += chunk)) |
| 74 | + stream.on('end', () => resolve(JSON.parse(raw))) |
| 75 | + stream.on('error', reject) |
| 76 | + }) |
| 77 | + expect(json.form).toBeDefined() |
| 78 | + expect(json.form.questions).toHaveLength(1) |
| 79 | + expect(json.appVersion).toBeDefined() |
| 80 | + // Deepen: verify the JSON content matches what we created |
| 81 | + expect(json.form.title).toBe('Download test') |
| 82 | + expect(json.form.questions[0].type).toBe('multiple') |
| 83 | + expect(json.form.questions[0].text).toBe('A') |
| 84 | + expect(json.form.questions[0].options).toHaveLength(3) |
| 85 | + expect(json.form.questions[0].options[0].text).toBe('B') |
| 86 | + expect(json.form.questions[0].options[1].text).toBe('C') |
| 87 | + expect(json.form.questions[0].options[2].text).toBe('D') |
| 88 | + // Verify stripped fields are absent |
| 89 | + expect(json.form.id).toBeUndefined() |
| 90 | + expect(json.form.hash).toBeUndefined() |
| 91 | + expect(json.form.ownerId).toBeUndefined() |
| 92 | + }) |
| 93 | + |
| 94 | + test('Download a form with multiple question types', async ({ |
| 95 | + page, |
| 96 | + appNavigation, |
| 97 | + form, |
| 98 | + }) => { |
| 99 | + await appNavigation.clickNewForm() |
| 100 | + await form.fillTitle('Multi type form') |
| 101 | + await form.addQuestion(QuestionType.ShortAnswer) |
| 102 | + const q1 = await form.getQuestions() |
| 103 | + await q1[0].fillTitle('Name') |
| 104 | + await form.addQuestion(QuestionType.Dropdown) |
| 105 | + const q2 = await form.getQuestions() |
| 106 | + await q2[1].fillTitle('Country') |
| 107 | + await q2[1].addAnswer('DE') |
| 108 | + await q2[1].addAnswer('FR') |
| 109 | + await q2[1].addAnswer('IT') |
| 110 | + |
| 111 | + await page.waitForLoadState('networkidle') |
| 112 | + const downloadPromise = page.waitForEvent('download') |
| 113 | + |
| 114 | + await appNavigation.getOwnForm('Multi type form').hover() |
| 115 | + await page.getByRole('button', { name: 'Form actions' }).click() |
| 116 | + await page.getByRole('menuitem', { name: 'Download form' }).click() |
| 117 | + |
| 118 | + const download = await downloadPromise |
| 119 | + const stream = await download.createReadStream() |
| 120 | + const json = await new Promise<any>((resolve, reject) => { |
| 121 | + let raw = '' |
| 122 | + stream.on('data', (chunk) => (raw += chunk)) |
| 123 | + stream.on('end', () => resolve(JSON.parse(raw))) |
| 124 | + stream.on('error', reject) |
| 125 | + }) |
| 126 | + expect(json.form.questions).toHaveLength(2) |
| 127 | + expect(json.form.questions[0].type).toBe('short') |
| 128 | + expect(json.form.questions[0].text).toBe('Name') |
| 129 | + expect(json.form.questions[1].type).toBe('dropdown') |
| 130 | + expect(json.form.questions[1].text).toBe('Country') |
| 131 | + expect(json.form.questions[1].options).toHaveLength(3) |
| 132 | + }) |
| 133 | +}) |
0 commit comments