From f2761fefb7fd75c7a2cbfe4b369fd3f749e6e0be Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Fri, 30 Jul 2021 11:59:42 +0300 Subject: [PATCH] Fix: onCompleting event doesn't work correctly in preview mode. #3157 --- src/survey.ts | 12 +++++++----- tests/surveyShowPreviewTests.ts | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/survey.ts b/src/survey.ts index cb50ab8585..37368985b0 100644 --- a/src/survey.ts +++ b/src/survey.ts @@ -3340,10 +3340,9 @@ export class SurveyModel extends Base if (this.doServerValidation(doComplete)) return false; if (doComplete) { this.currentPage.passed = true; - this.doComplete(); - } else { - this.doNextPage(); + return this.doComplete(); } + this.doNextPage(); return true; } /** @@ -3558,9 +3557,11 @@ export class SurveyModel extends Base * - calls `sendResult` function. * * Calling the `doComplete` function does not perform any validation, unlike the `completeLastPage` function. + * The function can return false, if you set options.allowComplete to false in onCompleting event. Otherwise it returns true. * It calls `navigateToUrl` after calling `onComplete` event. * In case calling `options.showDataSaving` callback in the `onComplete` event, `navigateToUrl` is used on calling `options.showDataSavingSuccess` callback. * @see completeLastPage + * @see onCompleting * @see cookieName * @see state * @see onComplete @@ -3569,13 +3570,13 @@ export class SurveyModel extends Base * @see navigateToUrl * @see navigateToUrlOnCondition */ - public doComplete(isCompleteOnTrigger: boolean = false) { + public doComplete(isCompleteOnTrigger: boolean = false): boolean { var onCompletingOptions = { allowComplete: true, isCompleteOnTrigger: isCompleteOnTrigger, }; this.onCompleting.fire(this, onCompletingOptions); - if (!onCompletingOptions.allowComplete) return; + if (!onCompletingOptions.allowComplete) return false; let previousCookie = this.hasCookie; this.stopTimer(); this.setCompleted(); @@ -3607,6 +3608,7 @@ export class SurveyModel extends Base if (!savingDataStarted) { this.navigateTo(); } + return true; } /** * Starts the survey. Changes the survey mode from "starting" to "running". Call this function if your survey has a start page, otherwise this function does nothing. diff --git a/tests/surveyShowPreviewTests.ts b/tests/surveyShowPreviewTests.ts index 7b428c76ff..12dd34d49f 100644 --- a/tests/surveyShowPreviewTests.ts +++ b/tests/surveyShowPreviewTests.ts @@ -173,6 +173,30 @@ QUnit.test( assert.equal(survey.currentPageNo, 1, "Current page is the last one"); } ); +QUnit.test( + "showPreviewBeforeComplete = 'showAllQuestions', and do noting onCompleting, options.allowComplete = false", + function(assert) { + var survey = new SurveyModel({ + pages: [ + { elements: [{ type: "text", name: "q1" }] }, + { elements: [{ type: "text", name: "q2" }] }, + ], + }); + survey.showPreviewBeforeComplete = "showAllQuestions"; + var allowComplete = false; + survey.onCompleting.add((sender, options) => { + options.allowComplete = allowComplete; + }); + survey.currentPageNo = 1; + survey.showPreview(); + assert.equal(survey.state, "preview"); + survey.completeLastPage(); + assert.equal(survey.state, "preview", "Keep showing preview"); + allowComplete = true; + survey.completeLastPage(); + assert.equal(survey.state, "completed"); + } +); QUnit.test( "showPreviewBeforeComplete = 'showAllQuestions', questionsOnPageMode = 'singlePage'",