diff --git a/playwright/e2e/edit-submission.spec.ts b/playwright/e2e/edit-submission.spec.ts index 2fa21ad1e..2c478f6fb 100644 --- a/playwright/e2e/edit-submission.spec.ts +++ b/playwright/e2e/edit-submission.spec.ts @@ -86,4 +86,41 @@ test.describe('Edit submission', () => { await submitView.submit() await expect(submitView.successMessage).toBeVisible() }) + + test('The draft of a form opened from the navigation while editing a submission is restored', async ({ + page, + appNavigation, + form, + topBar, + resultsView, + submitView, + }) => { + const resultsUrl = page.url().replace(/\/submit.*$/, '/results') + await appNavigation.clickNewForm() + await expect(form.titleField).toHaveValue('') + await form.fillTitle('Draft form') + await form.addQuestion(QuestionType.ShortAnswer) + const questions = await form.getQuestions() + await questions[0].fillTitle('Pet name') + await topBar.toggleView(FormsView.View) + await submitView.fillText('Pet name', 'Rex') + + page.once('dialog', (dialog) => dialog.accept()) + await page.goto(resultsUrl) + await resultsView.switchToResponses() + await resultsView.editSubmission() + await expect( + await submitView.getGridCellInput('Skills matrix', 'Beginner', 'Python'), + ).toBeChecked() + await appNavigation.getOwnForm('Draft form').click() + await page + .getByRole('dialog', { name: 'Leave form' }) + .getByRole('button', { name: 'Leave' }) + .click() + + await expect( + submitView.getQuestion('Pet name').getByRole('textbox'), + ).toHaveValue('Rex') + await expect(page.getByRole('dialog', { name: 'Clear form' })).toHaveCount(0) + }) }) diff --git a/playwright/e2e/submit-form.spec.ts b/playwright/e2e/submit-form.spec.ts index aa0b666a9..1104ccb9e 100644 --- a/playwright/e2e/submit-form.spec.ts +++ b/playwright/e2e/submit-form.spec.ts @@ -67,6 +67,34 @@ test.describe('Form submission', () => { await expect(submitView.successMessage).toBeVisible() }) + test('The draft of a form opened from the navigation is restored', async ({ + page, + appNavigation, + form, + topBar, + submitView, + }) => { + const submitUrl = page.url().replace(/\/edit$/, '/submit') + await appNavigation.clickNewForm() + await expect(form.titleField).toHaveValue('') + await form.fillTitle('Draft form') + await form.addQuestion(QuestionType.ShortAnswer) + const questions = await form.getQuestions() + await questions[0].fillTitle('Pet name') + await topBar.toggleView(FormsView.View) + await submitView.fillText('Pet name', 'Rex') + + page.once('dialog', (dialog) => dialog.accept()) + await page.goto(submitUrl) + await expect(submitView.getQuestion('Your name')).toBeVisible() + await appNavigation.getOwnForm('Draft form').click() + + await expect( + submitView.getQuestion('Pet name').getByRole('textbox'), + ).toHaveValue('Rex') + await expect(page.getByRole('dialog', { name: 'Clear form' })).toHaveCount(0) + }) + test('Partial submission succeeds when no fields are required', async ({ topBar, submitView, diff --git a/src/views/Submit.vue b/src/views/Submit.vue index 4dc324669..cf3b9af9b 100644 --- a/src/views/Submit.vue +++ b/src/views/Submit.vue @@ -636,7 +636,7 @@ export default defineComponent({ */ function initFromLocalStorage(): void { const savedState = getFormValuesFromLocalStorage() - if (!savedState) { + if (!savedState || !props.form.questions) { return } @@ -1141,7 +1141,7 @@ export default defineComponent({ watch( () => props.hash, - (): void => { + async (hash: string): Promise => { // If public view, abort. Should normally not occur. if (props.publicView) { logger.error('Hash changed on public view. Aborting.') @@ -1149,7 +1149,10 @@ export default defineComponent({ } resetData() // Fetch full form on change - void viewForm.fetchFullForm(props.form.id) + await viewForm.fetchFullForm(props.form.id) + if (hash !== props.hash) { + return + } initFromLocalStorage() SetWindowTitle(viewForm.formTitle.value) },