diff --git a/playwright/e2e/sections.spec.ts b/playwright/e2e/sections.spec.ts index ba0ce36e2..a561b8ef3 100644 --- a/playwright/e2e/sections.spec.ts +++ b/playwright/e2e/sections.spec.ts @@ -92,7 +92,7 @@ test.describe('Sections', () => { ]) }) - test('A respondent sees the section and submits the questions around it', async ({ + test('A respondent goes through the sections page by page', async ({ page, submitView, }) => { @@ -107,14 +107,26 @@ test.describe('Sections', () => { }) await page.goto(`apps/forms/${form.hash}/submit`) + await expect(submitView.submitButton).toBeVisible() await submitView.checkRadio('Do you have a pet?', 'Yes') + await expect(submitView.getQuestion('About the pet')).toBeHidden() + await submitView.nextButton.click() + await expect(submitView.getQuestion('Do you have a pet?')).toBeHidden() await expect(submitView.getQuestion('About the pet')).toBeVisible() await expect( page.getByRole('listitem', { name: 'Question number 2' }), ).toContainText('Which pet?') await submitView.fillText('Which pet?', 'Rex') + + await submitView.backButton.click() + await expect( + submitView + .getQuestion('Do you have a pet?') + .getByRole('radio', { name: 'Yes' }), + ).toBeChecked() await submitView.checkRadio('Do you have a pet?', 'No') - await expect(submitView.getQuestion('About the pet')).toBeHidden() + await expect(submitView.getQuestion('Comments')).toBeVisible() + await expect(submitView.nextButton).toHaveCount(0) await submitView.fillText('Comments', 'None') const response = await submitView.submit() @@ -147,6 +159,7 @@ test.describe('Sections', () => { test('A respondent sees no heading for a section without title', async ({ page, + submitView, }) => { const form = await api(page.request, 'POST', '/forms') for (const [type, text, description] of [ @@ -176,16 +189,146 @@ test.describe('Sections', () => { await page.goto(`apps/forms/${form.hash}/submit`) const questions = page.locator('form') - await expect(questions.getByRole('heading', { level: 3 })).toHaveText([ - 'Intro', - 'Part one', - 'Second', - 'Third', + const level3 = questions.getByRole('heading', { level: 3 }) + const level4 = questions.getByRole('heading', { level: 4 }) + await expect(level3).toHaveText(['Intro']) + await submitView.nextButton.click() + await expect(level3).toHaveText(['Part one']) + await expect(level4).toHaveText(['First']) + + await submitView.nextButton.click() + await expect(questions).toContainText('Only a note') + await expect(level3).toHaveText(['Second']) + await expect(level3).toBeFocused() + await expect(level4).toHaveCount(0) + + await submitView.nextButton.click() + await expect(level3).toHaveText(['Third']) + await expect(level3).toBeFocused() + await expect(questions.locator(':scope > ul > li')).toHaveCount(1) + }) + + test('Every page starts with the form title, questions of a section are headed below it', async ({ + page, + submitView, + }) => { + const { form } = await createSectionForm(page.request) + await page.goto(`apps/forms/${form.hash}/submit`) + + const main = page.getByRole('main') + await expect(main.getByRole('heading', { level: 2 })).toHaveText( + 'Section form', + ) + await expect(main.getByRole('heading', { level: 3 })).toHaveText([ + 'Do you have a pet?', ]) - await expect(questions.getByRole('heading', { level: 4 })).toHaveText([ - 'First', + await expect(main.getByRole('heading', { level: 4 })).toHaveCount(0) + + await submitView.nextButton.click() + await expect(main.getByRole('heading', { level: 2 })).toHaveText( + 'Section form', + ) + const section = main.getByRole('heading', { level: 3 }) + await expect(section).toHaveText(['About the pet']) + await expect(section).toBeFocused() + await expect(main.getByRole('heading', { level: 4 })).toHaveText([ + 'Which pet?', + 'Comments', ]) - await expect(questions).toContainText('Only a note') - await expect(questions.locator(':scope > ul > li')).toHaveCount(6) + }) + + test('Each page checks its required questions, an empty section is an info page', async ({ + page, + submitView, + }) => { + const form = await api(page.request, 'POST', '/forms') + const name = await api(page.request, 'POST', `/forms/${form.id}/questions`, { + type: 'short', + text: 'Your name', + }) + const info = await api(page.request, 'POST', `/forms/${form.id}/questions`, { + type: 'section', + text: 'Before you continue', + }) + await api(page.request, 'POST', `/forms/${form.id}/questions`, { + type: 'section', + text: 'Details', + }) + const city = await api(page.request, 'POST', `/forms/${form.id}/questions`, { + type: 'short', + text: 'City', + }) + for (const question of [name, city]) { + await api( + page.request, + 'PATCH', + `/forms/${form.id}/questions/${question.id}`, + { keyValuePairs: { isRequired: true } }, + ) + } + await api(page.request, 'PATCH', `/forms/${form.id}/questions/${info.id}`, { + keyValuePairs: { description: 'The next page asks for details.' }, + }) + await page.goto(`apps/forms/${form.hash}/submit`) + + await expect(submitView.backButton).toHaveCount(0) + await submitView.nextButton.click() + await expect(submitView.getQuestion('Your name')).toBeVisible() + await expect(submitView.getQuestion('City')).toBeHidden() + await submitView.fillText('Your name', 'Alex') + await submitView.nextButton.click() + + await expect(submitView.getQuestion('Before you continue')).toBeVisible() + await expect(page.getByText('The next page asks for details.')).toBeVisible() + await expect(submitView.getQuestion('Your name')).toBeHidden() + await submitView.nextButton.click() + + await expect(submitView.getQuestion('City')).toBeVisible() + await expect(submitView.nextButton).toHaveCount(0) + await submitView.backButton.click() + await submitView.backButton.click() + await expect( + submitView.getQuestion('Your name').getByRole('textbox'), + ).toHaveValue('Alex') + await submitView.nextButton.click() + await submitView.nextButton.click() + await submitView.fillText('City', 'Berlin') + + const response = await submitView.submit() + const { answers } = response.request().postDataJSON() + expect(answers).toEqual({ + [name.id]: ['Alex'], + [city.id]: ['Berlin'], + }) + await expect(submitView.successMessage).toBeVisible() + }) + + test('Respondents skip a section with no title, no description and no questions', async ({ + page, + submitView, + }) => { + const form = await api(page.request, 'POST', '/forms') + for (const [type, text] of [ + ['short', 'Your name'], + ['section', ''], + ['section', 'Details'], + ['short', 'City'], + ['section', ''], + ]) { + await api(page.request, 'POST', `/forms/${form.id}/questions`, { + type, + text, + }) + } + await page.goto(`apps/forms/${form.hash}/submit`) + + await expect(submitView.getQuestion('Your name')).toBeVisible() + await submitView.nextButton.click() + await expect(submitView.getQuestion('Details')).toBeVisible() + await expect(submitView.getQuestion('City')).toBeVisible() + await expect(submitView.nextButton).toHaveCount(0) + await expect(submitView.submitButton).toBeVisible() + await submitView.backButton.click() + await expect(submitView.getQuestion('Your name')).toBeVisible() }) }) diff --git a/playwright/support/sections/SubmitSection.ts b/playwright/support/sections/SubmitSection.ts index e8f685dd1..d3b204dc4 100644 --- a/playwright/support/sections/SubmitSection.ts +++ b/playwright/support/sections/SubmitSection.ts @@ -6,12 +6,16 @@ import type { Locator, Page, Response } from '@playwright/test' export class SubmitSection { + public readonly backButton: Locator public readonly clearFormButton: Locator + public readonly nextButton: Locator public readonly submitButton: Locator public readonly successMessage: Locator constructor(public readonly page: Page) { + this.backButton = this.page.getByRole('button', { name: 'Back' }) this.clearFormButton = this.page.getByRole('button', { name: 'Clear form' }) + this.nextButton = this.page.getByRole('button', { name: 'Next' }) this.submitButton = this.page.getByRole('button', { name: 'Submit' }) this.successMessage = this.page .getByRole('main') diff --git a/src/utils/Sections.ts b/src/utils/Sections.ts index 9c7786351..5ce34e82b 100644 --- a/src/utils/Sections.ts +++ b/src/utils/Sections.ts @@ -87,3 +87,20 @@ export function getQuestionIdsInSections( .map((question) => question.id), ) } + +/** + * The pages of the questions on a path: a page starts at each section, questions before the first section form the first page + * + * @param questions the questions and sections on the path, in their order + */ +export function getPages(questions: T[]): T[][] { + const pages: T[][] = [] + for (const question of questions) { + if (question.type === QUESTION_TYPE_SECTION || pages.length === 0) { + pages.push([question]) + } else { + pages[pages.length - 1].push(question) + } + } + return pages +} diff --git a/src/views/Submit.vue b/src/views/Submit.vue index 2cb8c1724..445364e72 100644 --- a/src/views/Submit.vue +++ b/src/views/Submit.vue @@ -156,7 +156,7 @@ {{ t('forms', 'Clear form') }} + + + {{ t('forms', 'Back') }} + - {{ t('forms', 'Submit') }} + {{ isLastPage ? t('forms', 'Submit') : t('forms', 'Next') }} @@ -260,6 +274,8 @@ import type { GridQuestionValues, } from '../types/Entities.d.ts' +import IconArrowBack from '@material-symbols/svg-400/outlined/arrow_back.svg?raw' +import IconArrowForward from '@material-symbols/svg-400/outlined/arrow_forward.svg?raw' import IconCancel from '@material-symbols/svg-400/outlined/block.svg?raw' import IconCheck from '@material-symbols/svg-400/outlined/check.svg?raw' import IconRefresh from '@material-symbols/svg-400/outlined/refresh.svg?raw' @@ -314,6 +330,7 @@ import logger from '../utils/Logger.ts' import OcsResponse2Data from '../utils/OcsResponse2Data.ts' import { getNumbers, + getPages, getQuestionIdsInSections, isEmptySection, QUESTION_TYPE_SECTION, @@ -526,6 +543,27 @@ export default defineComponent({ const shownNumbers = computed(() => getNumbers(shownQuestions.value)) + const pages = computed(() => + getPages(shownQuestions.value).filter( + (page) => !page.every(isEmptySection), + ), + ) + const currentPage = ref(0) + const pageIndex = computed(() => + Math.max(0, Math.min(currentPage.value, pages.value.length - 1)), + ) + const pageQuestionIds = computed>( + () => + new Set( + (pages.value[pageIndex.value] ?? []).map( + (question) => question.id, + ), + ), + ) + const isLastPage = computed( + () => pageIndex.value >= pages.value.length - 1, + ) + const submittedAnswers = computed(() => { return Object.fromEntries( Object.entries(answers.value).filter(([questionId]) => @@ -1173,12 +1211,18 @@ export default defineComponent({ * Focus the first shown question whose earlier answer the form no longer offers */ async function focusAnswerToChooseAgain(): Promise { - const index = shownQuestions.value.findIndex((question) => - answersToChooseAgain.value.has(question.id), + const page = pages.value.findIndex((questions) => + questions.some((question) => + answersToChooseAgain.value.has(question.id), + ), ) - if (index === -1) { + if (page === -1) { return } + currentPage.value = page + const index = pages.value[page].findIndex((question) => + answersToChooseAgain.value.has(question.id), + ) await nextTick() formElement.value ?.querySelectorAll(':scope > ul > li') @@ -1258,6 +1302,7 @@ export default defineComponent({ (input) => !(input instanceof HTMLFieldSetElement) && (input as HTMLButtonElement).type !== 'reset' + && !input.classList.contains('back-button') && !( target.type === 'radio' && (input as HTMLInputElement).name === target.name @@ -1266,6 +1311,24 @@ export default defineComponent({ ?.focus() } + /** + * Show a page of the path and move the focus to the heading of its first question or section + * + * @param index the index of the page + */ + async function goToPage(index: number): Promise { + currentPage.value = index + await nextTick() + const heading = formElement.value?.querySelector( + ':scope > ul > li :is(h3, h4)', + ) + if (heading) { + heading.tabIndex = -1 + heading.focus({ preventScroll: true }) + heading.closest('li')?.scrollIntoView({ block: 'start' }) + } + } + let invalidShown = false /** @@ -1360,6 +1423,11 @@ export default defineComponent({ throw new Error('One question did not validate sucessfully') } + if (!isLastPage.value) { + await goToPage(pageIndex.value + 1) + return + } + // in case no answer is set or all are empty show the confirmation dialog if ( Object.keys(submittedAnswers.value).length === 0 @@ -1469,6 +1537,7 @@ export default defineComponent({ */ function resetData(): void { answers.value = {} + currentPage.value = 0 clearPathAnnouncement() loading.value = false showConfirmLeaveDialog.value = false @@ -1635,6 +1704,12 @@ export default defineComponent({ validQuestions, visibleQuestionIds, title, + goToPage, + isLastPage, + pageIndex, + pageQuestionIds, + IconArrowBackSvg: IconArrowBack, + IconArrowForwardSvg: IconArrowForward, IconCheckSvg: IconCheck, IconScheduleSvg: IconSchedule, IconRefreshSvg: IconRefresh,