|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import type { APIRequestContext } from '@playwright/test' |
| 7 | + |
| 8 | +import { expect, mergeTests } from '@playwright/test' |
| 9 | +import { test as randomUserTest } from '../support/fixtures/random-user.ts' |
| 10 | +import { test as submitTest } from '../support/fixtures/submit.ts' |
| 11 | + |
| 12 | +const test = mergeTests(randomUserTest, submitTest) |
| 13 | + |
| 14 | +/** |
| 15 | + * Call the Forms OCS API as the logged in user |
| 16 | + * |
| 17 | + * @param request the request context of the page |
| 18 | + * @param method the HTTP method |
| 19 | + * @param path the path below `api/v3` |
| 20 | + * @param data the JSON body |
| 21 | + */ |
| 22 | +async function api( |
| 23 | + request: APIRequestContext, |
| 24 | + method: string, |
| 25 | + path: string, |
| 26 | + data?: unknown, |
| 27 | +) { |
| 28 | + const response = await request.fetch(`../ocs/v2.php/apps/forms/api/v3${path}`, { |
| 29 | + method, |
| 30 | + data, |
| 31 | + headers: { 'OCS-APIRequest': 'true', Accept: 'application/json' }, |
| 32 | + }) |
| 33 | + expect(response.ok()).toBeTruthy() |
| 34 | + return (await response.json()).ocs.data |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Create a form with a radio question "Do you have a pet?" (Yes, No), a required short question "Which pet?" and a short question "Comments" |
| 39 | + * |
| 40 | + * @param request the request context of the page |
| 41 | + */ |
| 42 | +async function createPetForm(request: APIRequestContext) { |
| 43 | + const form = await api(request, 'POST', '/forms') |
| 44 | + await api(request, 'PATCH', `/forms/${form.id}`, { |
| 45 | + keyValuePairs: { title: 'Pet form' }, |
| 46 | + }) |
| 47 | + const pet = await api(request, 'POST', `/forms/${form.id}/questions`, { |
| 48 | + type: 'multiple_unique', |
| 49 | + text: 'Do you have a pet?', |
| 50 | + }) |
| 51 | + const options = await api( |
| 52 | + request, |
| 53 | + 'POST', |
| 54 | + `/forms/${form.id}/questions/${pet.id}/options`, |
| 55 | + { optionTexts: ['Yes', 'No'] }, |
| 56 | + ) |
| 57 | + const which = await api(request, 'POST', `/forms/${form.id}/questions`, { |
| 58 | + type: 'short', |
| 59 | + text: 'Which pet?', |
| 60 | + }) |
| 61 | + await api(request, 'PATCH', `/forms/${form.id}/questions/${which.id}`, { |
| 62 | + keyValuePairs: { isRequired: true }, |
| 63 | + }) |
| 64 | + const comments = await api(request, 'POST', `/forms/${form.id}/questions`, { |
| 65 | + type: 'short', |
| 66 | + text: 'Comments', |
| 67 | + }) |
| 68 | + return { form, pet, options, which, comments } |
| 69 | +} |
| 70 | + |
| 71 | +test.describe('Jumps', () => { |
| 72 | + test('An answer jumps to a later question, set in the editor', async ({ |
| 73 | + page, |
| 74 | + }) => { |
| 75 | + const { form, pet, options, comments } = await createPetForm(page.request) |
| 76 | + await page.goto(`apps/forms/${form.hash}/edit`) |
| 77 | + |
| 78 | + const question = page.getByRole('listitem', { name: 'Question number 1' }) |
| 79 | + await question.getByRole('button', { name: 'Actions', exact: true }).click() |
| 80 | + await page |
| 81 | + .getByRole('checkbox', { name: 'Go to based on answer' }) |
| 82 | + .click({ force: true }) |
| 83 | + await page.keyboard.press('Escape') |
| 84 | + |
| 85 | + await question.getByRole('combobox', { name: 'Question 1: after "No" go to' }).click() |
| 86 | + await page |
| 87 | + .getByRole('option') |
| 88 | + .filter({ hasText: 'Question 3: Comments' }) |
| 89 | + .click() |
| 90 | + |
| 91 | + await expect(async () => { |
| 92 | + const stored = await api( |
| 93 | + page.request, |
| 94 | + 'GET', |
| 95 | + `/forms/${form.id}/questions/${pet.id}`, |
| 96 | + ) |
| 97 | + expect(stored.extraSettings.goTo).toEqual([ |
| 98 | + { optionId: options[1].id, target: comments.id }, |
| 99 | + ]) |
| 100 | + }).toPass() |
| 101 | + }) |
| 102 | + |
| 103 | + test('Skipped questions are hidden, not required and not submitted', async ({ |
| 104 | + page, |
| 105 | + submitView, |
| 106 | + }) => { |
| 107 | + const { form, pet, options, comments } = await createPetForm(page.request) |
| 108 | + await api(page.request, 'PATCH', `/forms/${form.id}/questions/${pet.id}`, { |
| 109 | + keyValuePairs: { |
| 110 | + extraSettings: { |
| 111 | + goTo: [{ optionId: options[1].id, target: comments.id }], |
| 112 | + }, |
| 113 | + }, |
| 114 | + }) |
| 115 | + await page.goto(`apps/forms/${form.hash}/submit`) |
| 116 | + |
| 117 | + await expect(submitView.getQuestion(/Which pet\?/)).toBeVisible() |
| 118 | + await submitView.checkRadio('Do you have a pet?', 'Yes') |
| 119 | + await submitView.fillText(/Which pet\?/, 'Rex') |
| 120 | + await submitView.checkRadio('Do you have a pet?', 'No') |
| 121 | + await expect(submitView.getQuestion(/Which pet\?/)).toBeHidden() |
| 122 | + await submitView.fillText('Comments', 'None') |
| 123 | + |
| 124 | + await submitView.submit() |
| 125 | + await expect(submitView.successMessage).toBeVisible() |
| 126 | + |
| 127 | + const { submissions } = await api( |
| 128 | + page.request, |
| 129 | + 'GET', |
| 130 | + `/forms/${form.id}/submissions`, |
| 131 | + ) |
| 132 | + expect(submissions[0].answers.map((answer) => answer.text).sort()).toEqual([ |
| 133 | + 'No', |
| 134 | + 'None', |
| 135 | + ]) |
| 136 | + }) |
| 137 | + |
| 138 | + test('"End of the form" after a question hides the questions after it', async ({ |
| 139 | + page, |
| 140 | + submitView, |
| 141 | + }) => { |
| 142 | + const { form, pet, options } = await createPetForm(page.request) |
| 143 | + await api(page.request, 'PATCH', `/forms/${form.id}/questions/${pet.id}`, { |
| 144 | + keyValuePairs: { |
| 145 | + extraSettings: { |
| 146 | + goTo: [{ optionId: options[1].id, target: 'submit' }], |
| 147 | + }, |
| 148 | + }, |
| 149 | + }) |
| 150 | + await page.goto(`apps/forms/${form.hash}/submit`) |
| 151 | + |
| 152 | + await expect(submitView.getQuestion(/Which pet\?/)).toBeVisible() |
| 153 | + await expect(submitView.getQuestion('Comments')).toBeVisible() |
| 154 | + await submitView.checkRadio('Do you have a pet?', 'No') |
| 155 | + await expect(submitView.getQuestion(/Which pet\?/)).toBeHidden() |
| 156 | + await expect(submitView.getQuestion('Comments')).toBeHidden() |
| 157 | + await submitView.checkRadio('Do you have a pet?', 'Yes') |
| 158 | + await expect(submitView.getQuestion('Comments')).toBeVisible() |
| 159 | + }) |
| 160 | +}) |
0 commit comments