Skip to content

Commit 461fcc7

Browse files
feat(conditional): show follow-up questions when answering and in the results
The submit view shows the follow-up questions of the branches the answer activates right below the question and announces when they appear or disappear. Answers to hidden follow-up questions are kept while the form is open, but not submitted. The results list the answers to follow-up questions with their question, in the summary and in each response. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent aa0f52c commit 461fcc7

6 files changed

Lines changed: 707 additions & 98 deletions

File tree

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
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 question of the given type with the options "Yes" and "No"
39+
*
40+
* @param request the request context of the page
41+
* @param type the question type
42+
*/
43+
async function createFormWithQuestion(request: APIRequestContext, type: string) {
44+
const form = await api(request, 'POST', '/forms')
45+
await api(request, 'PATCH', `/forms/${form.id}`, {
46+
keyValuePairs: { title: 'Conditional form' },
47+
})
48+
const question = await api(request, 'POST', `/forms/${form.id}/questions`, {
49+
type,
50+
text: 'Do you have dietary restrictions?',
51+
})
52+
const options = await api(
53+
request,
54+
'POST',
55+
`/forms/${form.id}/questions/${question.id}/options`,
56+
{ optionTexts: ['Yes', 'No'] },
57+
)
58+
return { form, question, options }
59+
}
60+
61+
test.describe('Follow-up questions', () => {
62+
test('Follow-up questions are added in the editor', async ({ page }) => {
63+
const { form, question, options } = await createFormWithQuestion(
64+
page.request,
65+
'multiple_unique',
66+
)
67+
await page.goto(`apps/forms/${form.hash}/edit`)
68+
69+
const trigger = page.getByRole('listitem', { name: 'Question number 1' })
70+
await trigger.getByRole('button', { name: 'Actions', exact: true }).click()
71+
await page
72+
.getByRole('menuitemcheckbox', { name: 'Follow-up questions' })
73+
.click()
74+
await page.keyboard.press('Escape')
75+
76+
await trigger
77+
.getByRole('combobox', { name: 'Follow-up after "Yes"' })
78+
.click()
79+
await page.getByRole('option', { name: 'New follow-up group' }).click()
80+
await expect(trigger.getByText('If "Yes"')).toBeVisible()
81+
82+
await trigger
83+
.getByRole('button', { name: 'Add a question to Follow-up group 1' })
84+
.click()
85+
await page.getByRole('menuitem', { name: 'Short answer' }).click()
86+
await expect(
87+
page.getByRole('listitem', { name: 'Question number 1.1' }),
88+
).toBeVisible()
89+
90+
await expect(async () => {
91+
const stored = await api(
92+
page.request,
93+
'GET',
94+
`/forms/${form.id}/questions/${question.id}`,
95+
)
96+
expect(stored.extraSettings.branches).toHaveLength(1)
97+
expect(stored.extraSettings.branches[0].conditions).toEqual([
98+
{ type: 'option_selected', optionId: options[0].id },
99+
])
100+
expect(stored.extraSettings.branches[0].subQuestions).toHaveLength(1)
101+
}).toPass()
102+
})
103+
104+
test('Deleting an option keeps its follow-up questions and the question editable', async ({
105+
page,
106+
}) => {
107+
const { form, question, options } = await createFormWithQuestion(
108+
page.request,
109+
'multiple_unique',
110+
)
111+
await api(
112+
page.request,
113+
'PATCH',
114+
`/forms/${form.id}/questions/${question.id}`,
115+
{
116+
keyValuePairs: {
117+
extraSettings: {
118+
branches: [
119+
{
120+
id: 'branch-yes',
121+
conditions: [
122+
{
123+
type: 'option_selected',
124+
optionId: options[0].id,
125+
},
126+
],
127+
},
128+
],
129+
},
130+
},
131+
},
132+
)
133+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
134+
type: 'short',
135+
text: 'Which ones?',
136+
parentQuestionId: question.id,
137+
branchId: 'branch-yes',
138+
})
139+
await page.goto(`apps/forms/${form.hash}/edit`)
140+
141+
const trigger = page.getByRole('listitem', { name: 'Question number 1' })
142+
await trigger.getByRole('button', { name: 'Delete answer' }).first().click()
143+
await expect(trigger.getByText('Not reached by any answer')).toBeVisible()
144+
145+
await trigger.getByRole('combobox', { name: 'Follow-up after "No"' }).click()
146+
await page.getByRole('option', { name: 'Follow-up group 1' }).click()
147+
await expect(trigger.getByText('If "No"')).toBeVisible()
148+
149+
await expect(async () => {
150+
const stored = await api(
151+
page.request,
152+
'GET',
153+
`/forms/${form.id}/questions/${question.id}`,
154+
)
155+
expect(stored.extraSettings.branches[0].conditions).toEqual([
156+
{ type: 'option_selected', optionId: options[1].id },
157+
])
158+
expect(stored.extraSettings.branches[0].subQuestions).toHaveLength(1)
159+
}).toPass()
160+
})
161+
162+
test.describe('Submitting', () => {
163+
let formId: number
164+
165+
test.beforeEach(async ({ page }) => {
166+
const request = page.request
167+
const { form, question, options } = await createFormWithQuestion(
168+
request,
169+
'dropdown',
170+
)
171+
formId = form.id
172+
const [yes, no] = options
173+
await api(
174+
request,
175+
'PATCH',
176+
`/forms/${formId}/questions/${question.id}`,
177+
{
178+
keyValuePairs: {
179+
extraSettings: {
180+
branches: [
181+
{
182+
id: 'branch-yes',
183+
conditions: [
184+
{
185+
type: 'option_selected',
186+
optionId: yes.id,
187+
},
188+
],
189+
},
190+
{
191+
id: 'branch-no',
192+
conditions: [
193+
{ type: 'option_selected', optionId: no.id },
194+
],
195+
},
196+
],
197+
},
198+
},
199+
},
200+
)
201+
for (const [branchId, text] of [
202+
['branch-yes', 'Which ones?'],
203+
['branch-no', 'Why not?'],
204+
]) {
205+
await api(request, 'POST', `/forms/${formId}/questions`, {
206+
type: 'short',
207+
text,
208+
parentQuestionId: question.id,
209+
branchId,
210+
})
211+
}
212+
await api(request, 'POST', `/forms/${formId}/questions`, {
213+
type: 'short',
214+
text: 'Favourite word?',
215+
})
216+
217+
await page.goto(`apps/forms/${form.hash}/submit`)
218+
})
219+
220+
test('Only the follow-up questions of the chosen answer are shown and stored', async ({
221+
page,
222+
submitView,
223+
}) => {
224+
await expect(page.getByText('Which ones?')).toBeHidden()
225+
226+
await submitView.selectDropdown(
227+
'Do you have dietary restrictions?',
228+
'Yes',
229+
)
230+
const subQuestion = page.getByRole('listitem', {
231+
name: 'Question number 1.1',
232+
})
233+
await expect(subQuestion.getByRole('textbox')).toHaveAccessibleName(
234+
'Which ones?',
235+
)
236+
await subQuestion.getByRole('textbox').fill('nuts')
237+
238+
await expect(
239+
page
240+
.getByRole('listitem', { name: 'Question number 2' })
241+
.getByRole('textbox'),
242+
).toHaveAccessibleName('Favourite word?')
243+
244+
await submitView.selectDropdown(
245+
'Do you have dietary restrictions?',
246+
'No',
247+
)
248+
await expect(page.getByText('Which ones?')).toBeHidden()
249+
await page
250+
.getByRole('listitem', { name: 'Question number 1.1' })
251+
.getByRole('textbox')
252+
.fill('no reason')
253+
254+
await submitView.submit()
255+
await expect(submitView.successMessage).toBeVisible()
256+
257+
const { submissions } = await api(
258+
page.request,
259+
'GET',
260+
`/forms/${formId}/submissions`,
261+
)
262+
expect(
263+
submissions[0].answers.map((answer) => answer.text).sort(),
264+
).toEqual(['No', 'no reason'])
265+
})
266+
267+
test('Element ids stay unique when follow-up questions are shown', async ({
268+
page,
269+
submitView,
270+
}) => {
271+
await submitView.selectDropdown(
272+
'Do you have dietary restrictions?',
273+
'Yes',
274+
)
275+
await expect(page.getByText('Which ones?')).toBeVisible()
276+
277+
const duplicateIds = await page.evaluate(() => {
278+
const counts: Record<string, number> = {}
279+
document.querySelectorAll('main [id]').forEach((element) => {
280+
counts[element.id] = (counts[element.id] ?? 0) + 1
281+
})
282+
return Object.keys(counts).filter((id) => counts[id] > 1)
283+
})
284+
expect(duplicateIds).toEqual([])
285+
})
286+
})
287+
})

0 commit comments

Comments
 (0)