Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 154 additions & 11 deletions playwright/e2e/sections.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}) => {
Expand All @@ -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()
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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()
})
})
4 changes: 4 additions & 0 deletions playwright/support/sections/SubmitSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
17 changes: 17 additions & 0 deletions src/utils/Sections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends { type: string }>(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
}
Loading
Loading