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
168 changes: 168 additions & 0 deletions playwright/e2e/submit-form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { APIRequestContext } from '@playwright/test'

import { expect, mergeTests } from '@playwright/test'
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 submitTest } from '../support/fixtures/submit.ts'
import { test as topBarTest } from '../support/fixtures/topBar.ts'
import { api, waitForApiResponse } from '../support/helpers.ts'
import { QuestionType } from '../support/sections/QuestionType.ts'
import { FormsView } from '../support/sections/TopBarSection.ts'

Expand Down Expand Up @@ -79,4 +82,169 @@ test.describe('Form submission', () => {
await submitView.submit()
await expect(submitView.successMessage).toBeVisible()
})

test('A restored draft leaves out deleted options', async ({
page,
form,
topBar,
submitView,
}) => {
const editUrl = page.url()
await topBar.toggleView(FormsView.View)
await submitView.checkOption('Favorite fruits', 'Apple')
await submitView.checkOption('Favorite fruits', 'Cherry')

page.once('dialog', (dialog) => dialog.accept())
await page.goto(editUrl)
await expect(
page.getByRole('listitem', { name: /Question number \d+/ }),
).toHaveCount(4)
const questions = await form.getQuestions()
const deleted = waitForApiResponse(page, 'DELETE')
await questions[1].section
.getByRole('button', { name: 'Delete answer' })
.nth(2)
.click()
await deleted

await topBar.toggleView(FormsView.View)
const response = await submitView.submit()
expect(response.status()).toBe(201)
await expect(submitView.successMessage).toBeVisible()
})
})

/**
* Create a form with a linear scale "Rating", a ranking question "Order" (First, Second, Third) and a checkbox grid "Skills" (rows Beginner, Expert; column Python)
*
* @param request the request context of the page
*/
async function createOfferedValuesForm(request: APIRequestContext) {
const form = await api(request, 'POST', '/forms')
const scale = await api(request, 'POST', `/forms/${form.id}/questions`, {
type: 'linearscale',
text: 'Rating',
})
const ranking = await api(request, 'POST', `/forms/${form.id}/questions`, {
type: 'ranking',
text: 'Order',
})
const options = await api(
request,
'POST',
`/forms/${form.id}/questions/${ranking.id}/options`,
{ optionTexts: ['First', 'Second', 'Third'] },
)
const grid = await api(request, 'POST', `/forms/${form.id}/questions`, {
type: 'grid',
subtype: 'checkbox',
text: 'Skills',
})
const rows = await api(
request,
'POST',
`/forms/${form.id}/questions/${grid.id}/options`,
{ optionTexts: ['Beginner', 'Expert'], optionType: 'row' },
)
const columns = await api(
request,
'POST',
`/forms/${form.id}/questions/${grid.id}/options`,
{ optionTexts: ['Python'], optionType: 'column' },
)
return { form, scale, ranking, options, grid, rows, columns }
}

test('A restored draft leaves out values the questions no longer offer', async ({
page,
submitView,
}) => {
const { form, scale, ranking, options, grid, rows, columns } =
await createOfferedValuesForm(page.request)
await page.goto(`apps/forms/${form.hash}/submit`)
await page.evaluate(
([key, draft]) => localStorage.setItem(key, draft),
[
`nextcloud_forms_${form.hash}`,
JSON.stringify({
[scale.id]: { value: ['8'], type: 'QuestionLinearScale' },
[ranking.id]: {
value: options.map((option: { id: number }) =>
String(option.id),
),
type: 'QuestionRanking',
},
[grid.id]: {
value: {
[rows[0].id]: [columns[0].id],
[rows[1].id]: [columns[0].id],
},
type: 'QuestionGrid',
},
}),
],
)
await api(
page.request,
'DELETE',
`/forms/${form.id}/questions/${ranking.id}/options/${options[2].id}`,
)
await api(
page.request,
'DELETE',
`/forms/${form.id}/questions/${grid.id}/options/${rows[1].id}`,
)
await page.reload()

const response = await submitView.submit()
expect(response.status()).toBe(201)
const { answers } = response.request().postDataJSON()
expect(answers[scale.id] ?? []).toEqual([])
expect(answers[ranking.id].map(String)).toEqual([
String(options[0].id),
String(options[1].id),
])
expect(answers[grid.id]).toEqual({ [rows[0].id]: [columns[0].id] })
})

test('An edited submission leaves out values the questions no longer offer', async ({
page,
submitView,
}) => {
const { form, scale, ranking, options } = await createOfferedValuesForm(
page.request,
)
await api(page.request, 'PATCH', `/forms/${form.id}/questions/${scale.id}`, {
keyValuePairs: { extraSettings: { optionsHighest: 10 } },
})
await api(page.request, 'POST', `/forms/${form.id}/submissions`, {
answers: {
[scale.id]: ['8'],
[ranking.id]: options.map((option: { id: number }) => String(option.id)),
},
})
const { submissions } = await api(
page.request,
'GET',
`/forms/${form.id}/submissions`,
)
await api(page.request, 'PATCH', `/forms/${form.id}/questions/${scale.id}`, {
keyValuePairs: { extraSettings: { optionsHighest: 5 } },
})
await api(
page.request,
'DELETE',
`/forms/${form.id}/questions/${ranking.id}/options/${options[2].id}`,
)
await page.goto(`apps/forms/${form.hash}/submit/${submissions[0].id}`)
await expect(submitView.getQuestion('Order')).toBeVisible()

const response = await submitView.submit()
expect(response.status()).toBe(200)
const { answers } = response.request().postDataJSON()
expect(answers[scale.id] ?? []).toEqual([])
expect(answers[ranking.id].map(String)).toEqual([
String(options[0].id),
String(options[1].id),
])
})
27 changes: 26 additions & 1 deletion playwright/support/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { Page, Response } from '@playwright/test'
import type { APIRequestContext, Page, Response } from '@playwright/test'

import { expect } from '@playwright/test'

const FORMS_API_PATH = '/api/v3/forms/'

Expand All @@ -21,3 +23,26 @@ export function waitForApiResponse(page: Page, method: string): Promise<Response
&& response.request().url().includes(FORMS_API_PATH),
)
}

/**
* Call the Forms OCS API as the logged in user
*
* @param request the request context of the page
* @param method the HTTP method
* @param path the path below `api/v3`
* @param data the JSON body
*/
export async function api(
request: APIRequestContext,
method: string,
path: string,
data?: unknown,
) {
const response = await request.fetch(`../ocs/v2.php/apps/forms/api/v3${path}`, {
method,
data,
headers: { 'OCS-APIRequest': 'true', Accept: 'application/json' },
})
expect(response.ok()).toBeTruthy()
return (await response.json()).ocs.data
}
Loading
Loading