From e63ca4690b6953fb8072dc11fad7687e4731cc3c Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 7 Mar 2024 14:08:22 +0200 Subject: [PATCH] defaultValue may work incorrectly for one question per page mode fix #7932 (#7933) --- .vscode/settings.json | 1 + src/survey.ts | 22 ++++++++--------- tests/surveytests.ts | 56 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8ade14f58e..a9159f218e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -24,4 +24,5 @@ "revealWithFocus": "test-results", "clearOnRun": "none" }, + "testing.openTesting": "neverOpen", } \ No newline at end of file diff --git a/src/survey.ts b/src/survey.ts index b68e2e0fc3..2df51efcdd 100644 --- a/src/survey.ts +++ b/src/survey.ts @@ -1058,7 +1058,7 @@ export class SurveyModel extends SurveyElementCore public getType(): string { return "survey"; } - protected onPropertyValueChanged(name: string, oldValue: any, newValue: any) { + protected onPropertyValueChanged(name: string, oldValue: any, newValue: any): void { if (name === "questionsOnPageMode") { this.onQuestionsOnPageModeChanged(oldValue); } @@ -3589,7 +3589,7 @@ export class SurveyModel extends SurveyElementCore return this._isDesignMode; } private _isDesignMode: boolean = false; - public setDesignMode(value: boolean) { + public setDesignMode(value: boolean): void { if (!!this._isDesignMode != !!value) { this._isDesignMode = !!value; this.onQuestionsOnPageModeChanged("standard"); @@ -4174,7 +4174,7 @@ export class SurveyModel extends SurveyElementCore if (this.isDesignMode) return; if (this.isShowingPreview) { this.runningPages = this.pages.slice(0, this.pages.length); - this.setupPagesForPageModes(true); + this.setupPagesForPageModes(true, false); } else { if (this.runningPages) { this.restoreOriginalPages(this.runningPages); @@ -4201,7 +4201,7 @@ export class SurveyModel extends SurveyElementCore } private changeCurrentPageFromPreview: boolean; private originalPages: any; - protected onQuestionsOnPageModeChanged(oldValue: string) { + protected onQuestionsOnPageModeChanged(oldValue: string, isFirstLoad: boolean = false): void { if (this.isShowingPreview) return; if (this.questionsOnPageMode == "standard" || this.isDesignMode) { if (this.originalPages) { @@ -4212,7 +4212,7 @@ export class SurveyModel extends SurveyElementCore if (!oldValue || oldValue == "standard") { this.originalPages = this.pages.slice(0, this.pages.length); } - this.setupPagesForPageModes(this.isSinglePage); + this.setupPagesForPageModes(this.isSinglePage, isFirstLoad); } this.runConditions(); this.updateVisibleIndexes(); @@ -4229,10 +4229,10 @@ export class SurveyModel extends SurveyElementCore private getPageStartIndex(): number { return this.firstPageIsStarted && this.pages.length > 0 ? 1 : 0; } - private isCreatingPagesForPreview: boolean; - private setupPagesForPageModes(isSinglePage: boolean) { + private isLockingUpdateOnPageModes: boolean; + private setupPagesForPageModes(isSinglePage: boolean, isFirstLoad: boolean) { this.questionHashesClear(); - this.isCreatingPagesForPreview = true; + this.isLockingUpdateOnPageModes = !isFirstLoad; var startIndex = this.getPageStartIndex(); super.startLoadingFromJson(); var newPages = this.createPagesForQuestionOnPageMode( @@ -4250,7 +4250,7 @@ export class SurveyModel extends SurveyElementCore } this.doElementsOnLoad(); this.updateCurrentPage(); - this.isCreatingPagesForPreview = false; + this.isLockingUpdateOnPageModes = false; } private createPagesForQuestionOnPageMode( isSinglePage: boolean, @@ -6116,7 +6116,7 @@ export class SurveyModel extends SurveyElementCore endLoadingFromJson() { this.isEndLoadingFromJson = "processing"; this.onFirstPageIsStartedChanged(); - this.onQuestionsOnPageModeChanged("standard"); + this.onQuestionsOnPageModeChanged("standard", true); super.endLoadingFromJson(); if (this.hasCookie) { this.isCompletedBefore = true; @@ -6443,7 +6443,7 @@ export class SurveyModel extends SurveyElementCore allowNotifyValueChanged: boolean = true, questionName?: string ): void { - if (this.isCreatingPagesForPreview) return; + if (this.isLockingUpdateOnPageModes) return; var newValue = newQuestionValue; if (allowNotifyValueChanged) { newValue = this.questionOnValueChanging(name, newQuestionValue); diff --git a/tests/surveytests.ts b/tests/surveytests.ts index deae65ba1c..5201bb71b1 100644 --- a/tests/surveytests.ts +++ b/tests/surveytests.ts @@ -18579,6 +18579,62 @@ QUnit.test("survey.toJSON() doesn't work correctly if questionsOnPageMode=questi assert.deepEqual(surveyJson, prepareJSON); }); +QUnit.test("defaultValue & visibleIf issues if questionsOnPageMode=questionPerPage is used #7932", function (assert) { + const surveyJson = { + elements: [ + { + type: "panel", + name: "panel1", + elements: [ + { + type: "radiogroup", + name: "q1_1", + choices: ["A", "B"], + defaultValue: "A", + }, + { + type: "text", + name: "q1_2", + visibleIf: + "{q1_1} equals 'A'", + }, + ], + }, + { + type: "panel", + name: "panel2", + elements: [ + { + type: "radiogroup", + name: "q2_1", + choices: ["A", "B"], + defaultValue: "A", + }, + { + type: "text", + name: "q2_2", + visibleIf: + "{q2_1} equals 'A'", + }, + ], + } + ], + questionsOnPageMode: "questionPerPage", + }; + + const survey = new SurveyModel(surveyJson); + assert.deepEqual(survey.data, { q1_1: "A", q2_1: "A" }, "survey.data"); + survey.setValue("q3", "B"); + const q1_1 = survey.getQuestionByName("q1_1"); + const q2_1 = survey.getQuestionByName("q2_1"); + const q1_2 = survey.getQuestionByName("q1_2"); + const q2_2 = survey.getQuestionByName("q2_2"); + + assert.equal(q1_1.value, "A", "q1_1.value"); + assert.equal(q2_1.value, "A", "q2_1.value"); + assert.equal(q1_2.visible, true, "q1_2.visible"); + assert.equal(q2_2.visible, true, "q2_2.visible"); +}); QUnit.test("Bug on loading json with collapsed panel. It was fixed in v1.9.117, #7355", function (assert) { const survey = new SurveyModel({