Skip to content
Merged
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
22 changes: 22 additions & 0 deletions playwright/e2e/a11y-question-inputs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { test as formTest } from '../support/fixtures/form.ts'
import { test as appNavigationTest } from '../support/fixtures/navigation.ts'
import { test as randomUserTest } from '../support/fixtures/random-user.ts'
import { test as topBarTest } from '../support/fixtures/topBar.ts'
import { waitForApiResponse } from '../support/helpers.ts'
import { GridSubtype, QuestionType } from '../support/sections/QuestionType.ts'
import { FormsView } from '../support/sections/TopBarSection.ts'

Expand Down Expand Up @@ -212,6 +213,17 @@ test.describe('Accessibility: aria attributes on question inputs', () => {
exact: true,
}),
).toBeVisible()
const savedLabel = waitForApiResponse(page, 'PATCH')
await questions[0].section
.getByRole('textbox', { name: 'Label for lowest value', exact: true })
.fill("Don't agree & leave")
await savedLabel
await expect(
questions[0].section.locator('fieldset.question-linear-scale__options'),
).not.toHaveAttribute('aria-labelledby')
await expect(
questions[0].section.getByRole('radio').first(),
).not.toHaveAttribute('aria-describedby')

await topBar.toggleView(FormsView.View)

Expand All @@ -220,6 +232,16 @@ test.describe('Accessibility: aria attributes on question inputs', () => {

await expect(fieldset).toHaveAttribute('aria-labelledby', 'q1_title')
await expect(fieldset).toHaveAttribute('aria-describedby', 'q1_desc')
await expect(fieldset.locator('legend')).toHaveText(
"From Don't agree & leave to Strongly agree",
)
const radios = fieldset.getByRole('radio')
await expect(radios.first()).toHaveAccessibleName('1')
await expect(radios.first()).not.toHaveAttribute('aria-labelledby')
await expect(radios.first()).toHaveAccessibleDescription(
"Don't agree & leave",
)
await expect(radios.last()).toHaveAccessibleDescription('Strongly agree')

await expect(
page.getByRole('heading', { name: 'Rate your experience' }),
Expand Down
26 changes: 26 additions & 0 deletions playwright/e2e/required-fields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { test as appNavigationTest } from '../support/fixtures/navigation.ts'
import { test as randomUserTest } from '../support/fixtures/random-user.ts'
import { test as submitTest } from '../support/fixtures/submit.ts'
import { test as topBarTest } from '../support/fixtures/topBar.ts'
import { api } from '../support/helpers.ts'
import { QuestionType } from '../support/sections/QuestionType.ts'
import { FormsView } from '../support/sections/TopBarSection.ts'

Expand Down Expand Up @@ -83,3 +84,28 @@ test.describe('Required field validation', () => {
await expect(submitView.successMessage).toBeVisible()
})
})

test('A required linear scale clears its error once a value is picked', async ({
page,
submitView,
}) => {
const form = await api(page.request, 'POST', '/forms')
const scale = await api(page.request, 'POST', `/forms/${form.id}/questions`, {
type: 'linearscale',
text: 'Rate the event',
})
await api(page.request, 'PATCH', `/forms/${form.id}/questions/${scale.id}`, {
keyValuePairs: { isRequired: true },
})
await page.goto(`apps/forms/${form.hash}/submit`)

await submitView.submitButton.click()
const question = submitView.getQuestion('Rate the event')
await expect(question.getByText('You must answer this question')).toBeVisible()

await submitView.checkRadio('Rate the event', '3')
await expect(question.getByText('You must answer this question')).toBeHidden()
await expect(question.getByRole('radio').first()).not.toHaveAttribute(
'aria-invalid',
)
})
54 changes: 47 additions & 7 deletions src/components/Questions/QuestionLinearScale.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,21 @@
</div>
<fieldset
class="question-linear-scale__options"
:aria-labelledby="titleId"
:aria-describedby="description ? descriptionId : undefined">
:aria-labelledby="readOnly ? titleId : undefined"
:aria-describedby="
readOnly && description ? descriptionId : undefined
">
<legend class="hidden-visually">
{{
t('forms', 'From {firstOption} to {lastOption}', {
firstOption: optionsLabelLowest,
lastOption: optionsLabelHighest,
})
t(
'forms',
'From {firstOption} to {lastOption}',
{
firstOption: optionsLabelLowest,
lastOption: optionsLabelHighest,
},
{ escape: false, sanitize: false },
)
}}
</legend>
<div
Expand All @@ -90,7 +97,8 @@
<label :for="`linear-scale-${id}-${option}`">{{ option }}</label>
<NcCheckboxRadioSwitch
:id="`linear-scale-${id}-${option}`"
:aria-describedby="index === 0 ? labelId : undefined"
:aria-label="option.toString()"
:aria-describedby="optionLabelId(index)"
:aria-errormessage="hasError ? errorId : undefined"
:aria-invalid="hasError ? 'true' : undefined"
:disabled="!readOnly"
Expand Down Expand Up @@ -123,6 +131,7 @@
</template>
<div
v-else-if="optionsLabelHighest !== ''"
:id="labelHighestId"
class="question-linear-scale__label question-linear-scale__label-highest">
{{ optionsLabelHighest }}
</div>
Expand Down Expand Up @@ -244,6 +253,34 @@ export default defineComponent({
* ID for the label for the lowest option
*/
const labelId = computed(() => 'q' + props.index + '__label_lowest')

/**
* ID for the label for the highest option
*/
const labelHighestId = computed(() => 'q' + props.index + '__label_highest')

/**
* ID of the shown label that describes an option: the lowest label for the first option,
* the highest label for the last one
*
* @param index the position of the option
*/
const optionLabelId = (index: number): string | undefined => {
if (!props.readOnly) {
return undefined
}
if (index === 0 && optionsLabelLowest.value !== '') {
return labelId.value
}
if (
index === scaleOptions.value.length - 1
&& optionsLabelHighest.value !== ''
) {
return labelHighestId.value
}
return undefined
}

const isUnique = computed(() => props.answerType.unique === true)

/**
Expand Down Expand Up @@ -289,6 +326,7 @@ export default defineComponent({

const onChange = (option: string): void => {
emit('update:values', [option])
question.errorMessage.value = null
}

const onOptionsLowestChange = (value: number): void => {
Expand Down Expand Up @@ -338,6 +376,8 @@ export default defineComponent({
isUnique,
questionValues,
labelId,
labelHighestId,
optionLabelId,
optionsLowest,
optionsHighest,
optionsLabelLowest,
Expand Down
Loading