|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { expect, mergeTests } from '@playwright/test' |
| 7 | +import { test as formTest } from '../support/fixtures/form.ts' |
| 8 | +import { test as appNavigationTest } from '../support/fixtures/navigation.ts' |
| 9 | +import { test as randomUserTest } from '../support/fixtures/random-user.ts' |
| 10 | +import { test as submitTest } from '../support/fixtures/submit.ts' |
| 11 | +import { test as topBarTest } from '../support/fixtures/topBar.ts' |
| 12 | +import { QuestionType } from '../support/sections/QuestionType.ts' |
| 13 | +import { FormsView } from '../support/sections/TopBarSection.ts' |
| 14 | + |
| 15 | +const test = mergeTests( |
| 16 | + randomUserTest, |
| 17 | + appNavigationTest, |
| 18 | + formTest, |
| 19 | + topBarTest, |
| 20 | + submitTest, |
| 21 | +) |
| 22 | + |
| 23 | +test.describe('Ranking question', () => { |
| 24 | + test.beforeEach(async ({ page, appNavigation, form }) => { |
| 25 | + await page.goto('apps/forms') |
| 26 | + await page.waitForURL(/apps\/forms\/?$/) |
| 27 | + await appNavigation.clickNewForm() |
| 28 | + await form.fillTitle('Ranking test form') |
| 29 | + |
| 30 | + await form.addQuestion(QuestionType.Ranking) |
| 31 | + const questions = await form.getQuestions() |
| 32 | + await questions[0].fillTitle('Rank snacks') |
| 33 | + await questions[0].addAnswer('Pretzels') |
| 34 | + await questions[0].addAnswer('Popcorn') |
| 35 | + await questions[0].addAnswer('Nuts') |
| 36 | + }) |
| 37 | + |
| 38 | + test('Restores unsubmitted ranking from local storage on reload', async ({ |
| 39 | + topBar, |
| 40 | + submitView, |
| 41 | + page, |
| 42 | + }) => { |
| 43 | + await topBar.toggleView(FormsView.View) |
| 44 | + |
| 45 | + await submitView.rankOption('Rank snacks', 'Pretzels') |
| 46 | + await submitView.rankOption('Rank snacks', 'Popcorn') |
| 47 | + |
| 48 | + await page.reload() |
| 49 | + |
| 50 | + const question = submitView.getQuestion('Rank snacks') |
| 51 | + await expect( |
| 52 | + question.getByRole('button', { name: 'Remove from ranking' }), |
| 53 | + ).toHaveCount(2) |
| 54 | + }) |
| 55 | + |
| 56 | + test('Clear form resets ranked options', async ({ topBar, submitView }) => { |
| 57 | + await topBar.toggleView(FormsView.View) |
| 58 | + |
| 59 | + await submitView.rankOption('Rank snacks', 'Pretzels') |
| 60 | + await submitView.rankOption('Rank snacks', 'Popcorn') |
| 61 | + await submitView.clearForm() |
| 62 | + |
| 63 | + const question = submitView.getQuestion('Rank snacks') |
| 64 | + await expect( |
| 65 | + question.getByRole('button', { name: 'Remove from ranking' }), |
| 66 | + ).toHaveCount(0) |
| 67 | + await expect( |
| 68 | + question.getByRole('button', { name: 'Pretzels' }), |
| 69 | + ).toBeVisible() |
| 70 | + await expect(question.getByRole('button', { name: 'Popcorn' })).toBeVisible() |
| 71 | + }) |
| 72 | + |
| 73 | + test('Required ranking blocks submit until all options are ranked', async ({ |
| 74 | + topBar, |
| 75 | + submitView, |
| 76 | + form, |
| 77 | + }) => { |
| 78 | + const questions = await form.getQuestions() |
| 79 | + await questions[0].toggleRequired() |
| 80 | + |
| 81 | + await topBar.toggleView(FormsView.View) |
| 82 | + |
| 83 | + await submitView.submitButton.click() |
| 84 | + await expect(submitView.successMessage).not.toBeVisible() |
| 85 | + |
| 86 | + await submitView.rankOption('Rank snacks', 'Pretzels') |
| 87 | + await submitView.submitButton.click() |
| 88 | + await expect(submitView.successMessage).not.toBeVisible() |
| 89 | + |
| 90 | + await submitView.rankOption('Rank snacks', 'Popcorn') |
| 91 | + await submitView.rankOption('Rank snacks', 'Nuts') |
| 92 | + await submitView.submit() |
| 93 | + await expect(submitView.successMessage).toBeVisible() |
| 94 | + }) |
| 95 | + |
| 96 | + test('Partial ranking submission is blocked by required validation', async ({ |
| 97 | + topBar, |
| 98 | + submitView, |
| 99 | + }) => { |
| 100 | + await topBar.toggleView(FormsView.View) |
| 101 | + |
| 102 | + // Rank only 2 out of 3 items |
| 103 | + await submitView.rankOption('Rank snacks', 'Pretzels') |
| 104 | + await submitView.rankOption('Rank snacks', 'Popcorn') |
| 105 | + |
| 106 | + // Try to submit — should fail |
| 107 | + await submitView.submitButton.click() |
| 108 | + |
| 109 | + // Verify error prevents submission (success message hidden) |
| 110 | + await expect(submitView.successMessage).not.toBeVisible() |
| 111 | + }) |
| 112 | + |
| 113 | + test('Complete ranking submission succeeds after partial attempt', async ({ |
| 114 | + topBar, |
| 115 | + submitView, |
| 116 | + }) => { |
| 117 | + await topBar.toggleView(FormsView.View) |
| 118 | + |
| 119 | + // Rank first 2 items |
| 120 | + await submitView.rankOption('Rank snacks', 'Pretzels') |
| 121 | + await submitView.rankOption('Rank snacks', 'Popcorn') |
| 122 | + |
| 123 | + // Submit attempt fails (partial ranking) |
| 124 | + await submitView.submitButton.click() |
| 125 | + await expect(submitView.successMessage).not.toBeVisible() |
| 126 | + |
| 127 | + // Complete the ranking |
| 128 | + await submitView.rankOption('Rank snacks', 'Nuts') |
| 129 | + |
| 130 | + // Now submit should succeed |
| 131 | + await submitView.submit() |
| 132 | + await expect(submitView.successMessage).toBeVisible() |
| 133 | + }) |
| 134 | + |
| 135 | + test('Multiple ranking questions maintain separate drag contexts', async ({ |
| 136 | + form, |
| 137 | + topBar, |
| 138 | + submitView, |
| 139 | + }) => { |
| 140 | + // Add a second ranking question |
| 141 | + await form.addQuestion(QuestionType.Ranking) |
| 142 | + const questions = await form.getQuestions() |
| 143 | + await questions[1].fillTitle('Rank preferences') |
| 144 | + await questions[1].addAnswer('Option X') |
| 145 | + await questions[1].addAnswer('Option Y') |
| 146 | + await questions[1].addAnswer('Option Z') |
| 147 | + |
| 148 | + await topBar.toggleView(FormsView.View) |
| 149 | + |
| 150 | + // Rank first question completely |
| 151 | + await submitView.rankOption('Rank snacks', 'Pretzels') |
| 152 | + await submitView.rankOption('Rank snacks', 'Popcorn') |
| 153 | + await submitView.rankOption('Rank snacks', 'Nuts') |
| 154 | + |
| 155 | + // Rank second question partially |
| 156 | + await submitView.rankOption('Rank preferences', 'Option X') |
| 157 | + await submitView.rankOption('Rank preferences', 'Option Z') |
| 158 | + |
| 159 | + // Verify both rankings are correct |
| 160 | + const q1 = submitView.getQuestion('Rank snacks') |
| 161 | + const q2 = submitView.getQuestion('Rank preferences') |
| 162 | + |
| 163 | + await expect( |
| 164 | + q1.getByRole('button', { name: 'Remove from ranking' }), |
| 165 | + ).toHaveCount(3) |
| 166 | + await expect( |
| 167 | + q2.getByRole('button', { name: 'Remove from ranking' }), |
| 168 | + ).toHaveCount(2) |
| 169 | + |
| 170 | + // Submit should require q2 to be complete |
| 171 | + await submitView.submitButton.click() |
| 172 | + await expect(submitView.successMessage).not.toBeVisible() |
| 173 | + |
| 174 | + // Complete q2 |
| 175 | + await submitView.rankOption('Rank preferences', 'Option Y') |
| 176 | + await submitView.submit() |
| 177 | + await expect(submitView.successMessage).toBeVisible() |
| 178 | + }) |
| 179 | +}) |
0 commit comments