Skip to content

Commit aba8450

Browse files
feat(section): show one page per section
A form with sections becomes a sequence of pages: each section starts a page, questions before the first section form the first one. Next checks only the questions of the current page, Back keeps the answers, and Submit appears on the last page of the respondent's path, which follows the answers as before. A form without sections stays a single page. The form title heads every page, so the heading of a section and those of its questions keep their place in the outline. A jump into the middle of a section adds its questions to the current page, so existing jumps keep working until jump targets are limited to the start of a section. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent 093f8cc commit aba8450

4 files changed

Lines changed: 304 additions & 17 deletions

File tree

‎playwright/e2e/sections.spec.ts‎

Lines changed: 202 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ test.describe('Sections', () => {
9696
])
9797
})
9898

99-
test('A respondent sees the section and submits the questions around it', async ({
99+
test('A respondent goes through the sections page by page', async ({
100100
page,
101101
submitView,
102102
}) => {
@@ -111,14 +111,26 @@ test.describe('Sections', () => {
111111
})
112112
await page.goto(`apps/forms/${form.hash}/submit`)
113113

114+
await expect(submitView.submitButton).toBeVisible()
114115
await submitView.checkRadio('Do you have a pet?', 'Yes')
116+
await expect(submitView.getQuestion('About the pet')).toBeHidden()
117+
await submitView.nextButton.click()
118+
await expect(submitView.getQuestion('Do you have a pet?')).toBeHidden()
115119
await expect(submitView.getQuestion('About the pet')).toBeVisible()
116120
await expect(
117121
page.getByRole('listitem', { name: 'Question number 2' }),
118122
).toContainText('Which pet?')
119123
await submitView.fillText('Which pet?', 'Rex')
124+
125+
await submitView.backButton.click()
126+
await expect(
127+
submitView
128+
.getQuestion('Do you have a pet?')
129+
.getByRole('radio', { name: 'Yes' }),
130+
).toBeChecked()
120131
await submitView.checkRadio('Do you have a pet?', 'No')
121-
await expect(submitView.getQuestion('About the pet')).toBeHidden()
132+
await expect(submitView.getQuestion('Comments')).toBeVisible()
133+
await expect(submitView.nextButton).toHaveCount(0)
122134
await submitView.fillText('Comments', 'None')
123135

124136
const response = await submitView.submit()
@@ -151,6 +163,7 @@ test.describe('Sections', () => {
151163

152164
test('A respondent sees no heading for a section without title', async ({
153165
page,
166+
submitView,
154167
}) => {
155168
const form = await api(page.request, 'POST', '/forms')
156169
for (const [type, text, description] of [
@@ -181,17 +194,195 @@ test.describe('Sections', () => {
181194
await page.goto(`apps/forms/${form.hash}/submit`)
182195

183196
const questions = page.locator('form')
184-
await expect(questions.getByRole('heading', { level: 3 })).toHaveText([
185-
'Intro',
186-
'Part one',
187-
'Second',
188-
'Third',
197+
const level3 = questions.getByRole('heading', { level: 3 })
198+
const level4 = questions.getByRole('heading', { level: 4 })
199+
await expect(level3).toHaveText(['Intro'])
200+
await submitView.nextButton.click()
201+
await expect(level3).toHaveText(['Part one'])
202+
await expect(level4).toHaveText(['First'])
203+
204+
await submitView.nextButton.click()
205+
await expect(questions).toContainText('Only a note')
206+
await expect(level3).toHaveText(['Second'])
207+
await expect(level3).toBeFocused()
208+
await expect(level4).toHaveCount(0)
209+
210+
await submitView.nextButton.click()
211+
await expect(level3).toHaveText(['Third'])
212+
await expect(level3).toBeFocused()
213+
await expect(questions.locator(':scope > ul > li')).toHaveCount(1)
214+
})
215+
216+
test('Every page starts with the form title, questions of a section are headed below it', async ({
217+
page,
218+
submitView,
219+
}) => {
220+
const { form } = await createSectionForm(page.request)
221+
await page.goto(`apps/forms/${form.hash}/submit`)
222+
223+
const main = page.getByRole('main')
224+
await expect(main.getByRole('heading', { level: 2 })).toHaveText(
225+
'Section form',
226+
)
227+
await expect(main.getByRole('heading', { level: 3 })).toHaveText([
228+
'Do you have a pet?',
189229
])
190-
await expect(questions.getByRole('heading', { level: 4 })).toHaveText([
191-
'First',
230+
await expect(main.getByRole('heading', { level: 4 })).toHaveCount(0)
231+
232+
await submitView.nextButton.click()
233+
await expect(main.getByRole('heading', { level: 2 })).toHaveText(
234+
'Section form',
235+
)
236+
const section = main.getByRole('heading', { level: 3 })
237+
await expect(section).toHaveText(['About the pet'])
238+
await expect(section).toBeFocused()
239+
await expect(main.getByRole('heading', { level: 4 })).toHaveText([
240+
'Which pet?',
241+
'Comments',
192242
])
193-
await expect(questions).toContainText('Only a note')
194-
await expect(questions.locator(':scope > ul > li')).toHaveCount(6)
243+
})
244+
245+
test('Each page checks its required questions, an empty section is an info page', async ({
246+
page,
247+
submitView,
248+
}) => {
249+
const form = await api(page.request, 'POST', '/forms')
250+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
251+
type: 'section',
252+
text: '',
253+
})
254+
const name = await api(page.request, 'POST', `/forms/${form.id}/questions`, {
255+
type: 'short',
256+
text: 'Your name',
257+
})
258+
const info = await api(page.request, 'POST', `/forms/${form.id}/questions`, {
259+
type: 'section',
260+
text: 'Before you continue',
261+
})
262+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
263+
type: 'section',
264+
text: 'Details',
265+
})
266+
const city = await api(page.request, 'POST', `/forms/${form.id}/questions`, {
267+
type: 'short',
268+
text: 'City',
269+
})
270+
for (const question of [name, city]) {
271+
await api(
272+
page.request,
273+
'PATCH',
274+
`/forms/${form.id}/questions/${question.id}`,
275+
{ keyValuePairs: { isRequired: true } },
276+
)
277+
}
278+
await api(page.request, 'PATCH', `/forms/${form.id}/questions/${info.id}`, {
279+
keyValuePairs: { description: 'The next page asks for details.' },
280+
})
281+
await page.goto(`apps/forms/${form.hash}/submit`)
282+
283+
await expect(submitView.backButton).toHaveCount(0)
284+
await submitView.nextButton.click()
285+
await expect(submitView.getQuestion('Your name')).toBeVisible()
286+
await expect(submitView.getQuestion('City')).toBeHidden()
287+
await submitView.fillText('Your name', 'Alex')
288+
await submitView.nextButton.click()
289+
290+
await expect(submitView.getQuestion('Before you continue')).toBeVisible()
291+
await expect(page.getByText('The next page asks for details.')).toBeVisible()
292+
await expect(submitView.getQuestion('Your name')).toBeHidden()
293+
await submitView.nextButton.click()
294+
295+
await expect(submitView.getQuestion('City')).toBeVisible()
296+
await expect(submitView.nextButton).toHaveCount(0)
297+
await submitView.backButton.click()
298+
await submitView.backButton.click()
299+
await expect(
300+
submitView.getQuestion('Your name').getByRole('textbox'),
301+
).toHaveValue('Alex')
302+
await submitView.nextButton.click()
303+
await submitView.nextButton.click()
304+
await submitView.fillText('City', 'Berlin')
305+
306+
const response = await submitView.submit()
307+
const { answers } = response.request().postDataJSON()
308+
expect(answers).toEqual({
309+
[name.id]: ['Alex'],
310+
[city.id]: ['Berlin'],
311+
})
312+
await expect(submitView.successMessage).toBeVisible()
313+
})
314+
315+
test('Respondents skip a section with no title, no description and no questions', async ({
316+
page,
317+
submitView,
318+
}) => {
319+
const form = await api(page.request, 'POST', '/forms')
320+
for (const [type, text] of [
321+
['section', ''],
322+
['short', 'Your name'],
323+
['section', ''],
324+
['section', 'Details'],
325+
['short', 'City'],
326+
['section', ''],
327+
]) {
328+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
329+
type,
330+
text,
331+
})
332+
}
333+
await page.goto(`apps/forms/${form.hash}/submit`)
334+
335+
await expect(submitView.getQuestion('Your name')).toBeVisible()
336+
await submitView.nextButton.click()
337+
await expect(submitView.getQuestion('Details')).toBeVisible()
338+
await expect(submitView.getQuestion('City')).toBeVisible()
339+
await expect(submitView.nextButton).toHaveCount(0)
340+
await expect(submitView.submitButton).toBeVisible()
341+
await submitView.backButton.click()
342+
await expect(submitView.getQuestion('Your name')).toBeVisible()
343+
})
344+
345+
test('A last section with a title but no questions is a page with the submit button', async ({
346+
page,
347+
submitView,
348+
}) => {
349+
const form = await api(page.request, 'POST', '/forms')
350+
for (const [type, text] of [
351+
['section', ''],
352+
['short', 'Your name'],
353+
]) {
354+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
355+
type,
356+
text,
357+
})
358+
}
359+
const closing = await api(
360+
page.request,
361+
'POST',
362+
`/forms/${form.id}/questions`,
363+
{
364+
type: 'section',
365+
text: 'Thank you',
366+
},
367+
)
368+
await api(
369+
page.request,
370+
'PATCH',
371+
`/forms/${form.id}/questions/${closing.id}`,
372+
{
373+
keyValuePairs: { description: 'We will get back to you.' },
374+
},
375+
)
376+
await page.goto(`apps/forms/${form.hash}/submit`)
377+
378+
await expect(submitView.getQuestion('Your name')).toBeVisible()
379+
await expect(submitView.submitButton).toHaveCount(0)
380+
await submitView.nextButton.click()
381+
await expect(submitView.getQuestion('Thank you')).toBeVisible()
382+
await expect(page.getByText('We will get back to you.')).toBeVisible()
383+
await expect(submitView.getQuestion('Your name')).toBeHidden()
384+
await expect(submitView.nextButton).toHaveCount(0)
385+
await expect(submitView.submitButton).toBeVisible()
195386
})
196387

197388
test('Adding a section below questions puts them into a section "Untitled section"', async ({

‎playwright/support/sections/SubmitSection.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
import type { Locator, Page, Response } from '@playwright/test'
77

88
export class SubmitSection {
9+
public readonly backButton: Locator
910
public readonly clearFormButton: Locator
11+
public readonly nextButton: Locator
1012
public readonly submitButton: Locator
1113
public readonly successMessage: Locator
1214

1315
constructor(public readonly page: Page) {
16+
this.backButton = this.page.getByRole('button', { name: 'Back' })
1417
this.clearFormButton = this.page.getByRole('button', { name: 'Clear form' })
18+
this.nextButton = this.page.getByRole('button', { name: 'Next' })
1519
this.submitButton = this.page.getByRole('button', { name: 'Submit' })
1620
this.successMessage = this.page
1721
.getByRole('main')

‎src/utils/Sections.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,20 @@ export function withNextSectionMovedUp<T extends { type: string }>(
131131
...questions.slice(first + 1),
132132
]
133133
}
134+
135+
/**
136+
* The pages of the questions on a path: a page starts at each section, questions before the first section form the first page
137+
*
138+
* @param questions the questions and sections on the path, in their order
139+
*/
140+
export function getPages<T extends { type: string }>(questions: T[]): T[][] {
141+
const pages: T[][] = []
142+
for (const question of questions) {
143+
if (question.type === QUESTION_TYPE_SECTION || pages.length === 0) {
144+
pages.push([question])
145+
} else {
146+
pages[pages.length - 1].push(question)
147+
}
148+
}
149+
return pages
150+
}

0 commit comments

Comments
 (0)