From 3c7e4f8df8a36a274c7d9bd19ee67160340824e1 Mon Sep 17 00:00:00 2001 From: tsv2013 Date: Wed, 27 Dec 2023 17:45:05 +0300 Subject: [PATCH] Fixed #7507 - Timer doesn't stop at 0 when a survey has validation errors --- src/surveyTimerModel.ts | 19 +++++++++++-------- tests/surveytimertests.ts | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/surveyTimerModel.ts b/src/surveyTimerModel.ts index 5d5596f8ae..7fc7fb2024 100644 --- a/src/surveyTimerModel.ts +++ b/src/surveyTimerModel.ts @@ -30,10 +30,10 @@ export class SurveyTimerModel extends Base { @property() clockMinorText: string; @property({ defaultValue: 0 }) spent: number; public get survey(): ISurveyTimerText { return this.surveyValue; } - public onCreating(): void {} + public onCreating(): void { } private timerFunc: any = null; public start(): void { - if(!this.survey) return; + if (!this.survey) return; if (this.isRunning || this.isDesignMode) return; this.survey.onCurrentPageChanged.add(() => { this.update(); @@ -65,23 +65,26 @@ export class SurveyTimerModel extends Base { } this.spent = this.spent + 1; this.update(); - if(this.onTimer) { + if (this.onTimer) { this.onTimer(page); } } private updateProgress() { let { spent, limit } = this.survey.timerInfo; - if(!limit) { + if (!limit) { this.progress = undefined; } else { - if(spent == 0) { + if (spent == 0) { this.progress = 0; setTimeout(() => { - this.progress = Math.floor((spent + 1)/limit * 100) / 100; + this.progress = Math.floor((spent + 1) / limit * 100) / 100; }, 0); } - else if(spent !== limit) { - this.progress = Math.floor((spent + 1)/limit * 100) / 100; + else if (spent <= limit) { + this.progress = Math.floor((spent + 1) / limit * 100) / 100; + } + if (this.progress > 1) { + this.progress = undefined; } } } diff --git a/tests/surveytimertests.ts b/tests/surveytimertests.ts index 316d08fe59..7994c0b7e1 100644 --- a/tests/surveytimertests.ts +++ b/tests/surveytimertests.ts @@ -433,4 +433,37 @@ QUnit.test("Check timer when limits are not specified", function(assert) { assert.strictEqual(timerModel.clockMajorText, "0:01"); assert.strictEqual(timerModel.clockMinorText, undefined); survey.stopTimer(); -}); \ No newline at end of file +}); + +QUnit.test("Progress shouldn't be more than 1", function (assert) { + const createSurvey = (maxTimeToFinish: number, maxTimeToFinishPage: number): SurveyModel => { + var survey = new SurveyModel(); + survey.maxTimeToFinish = maxTimeToFinish; + survey.maxTimeToFinishPage = maxTimeToFinishPage; + survey.addNewPage("p1"); + survey.pages[0].addNewQuestion("text"); + return survey; + }; + var survey = createSurvey(3, 3); + const timerModel = survey.timerModel; + survey.startTimer(); + assert.equal(survey.timerInfo.limit, 3); + assert.equal(survey.timerInfo.spent, 0, "initial spent"); + assert.equal(timerModel.progress, 0, "initial progress"); + doTimer(1); + assert.equal(survey.timerInfo.limit, 3); + assert.equal(survey.timerInfo.spent, 1, "spent 1"); + assert.equal(timerModel.progress, 0.66, "progress 1"); + doTimer(1); + assert.equal(survey.timerInfo.limit, 3); + assert.equal(survey.timerInfo.spent, 2, "spent 2"); + assert.equal(timerModel.progress, 1, "progress 2"); + doTimer(1); + assert.equal(survey.timerInfo.limit, 3); + assert.equal(survey.timerInfo.spent, 3, "spent 3"); + assert.equal(timerModel.progress, undefined, "progress 3"); + doTimer(1); + assert.equal(survey.timerInfo.limit, 3); + assert.equal(survey.timerInfo.spent, 3, "spent 4"); + assert.equal(timerModel.progress, undefined, "progress 4"); +});