diff --git a/projects/v3/src/app/components/assessment/assessment.component.spec.ts b/projects/v3/src/app/components/assessment/assessment.component.spec.ts index cc5b1b6c0..20efd664b 100644 --- a/projects/v3/src/app/components/assessment/assessment.component.spec.ts +++ b/projects/v3/src/app/components/assessment/assessment.component.spec.ts @@ -718,4 +718,181 @@ describe('AssessmentComponent', () => { document.body.removeChild(element); })); }); + + describe('_compulsoryQuestionsAnswered', () => { + it('should return empty array when all required questions are answered', () => { + // Set up mock assessment with required questions + component.assessment = { + id: 1, + type: 'default', + isForTeam: false, + groups: [ + { + name: 'Group 1', + questions: [ + { + id: 1, + name: 'Question 1', + type: 'text', + isRequired: true, + audience: ['submitter'] + }, + { + id: 2, + name: 'Question 2', + type: 'multiple', + isRequired: true, + audience: ['submitter'] + } + ] + } + ] + } as any; + + // Set up mock answers + const answers = [ + { questionId: 1, answer: 'Answer to question 1' }, + { questionId: 2, answer: ['Option 1', 'Option 2'] } + ]; + + // Test the function + const missingQuestions = component['_compulsoryQuestionsAnswered'](answers); + + // Expect no missing questions + expect(missingQuestions.length).toBe(0); + }); + + it('should return questions that are required but not answered', () => { + // Set up mock assessment with required questions + component.assessment = { + id: 1, + type: 'default', + isForTeam: false, + groups: [ + { + name: 'Group 1', + questions: [ + { + id: 1, + name: 'Question 1', + type: 'text', + isRequired: true, + audience: ['submitter'] + }, + { + id: 2, + name: 'Question 2', + type: 'text', + isRequired: true, + audience: ['submitter'] + } + ] + } + ] + } as any; + + // Set up mock answers with one missing + const answers = [ + { questionId: 1, answer: 'Answer to question 1' } + // Question 2 is missing + ]; + + // Mock form element + spyOn(component.form.nativeElement, 'querySelector').and.returnValue({ + classList: { + add: jasmine.createSpy('add') + } + }); + + // Test the function + const missingQuestions = component['_compulsoryQuestionsAnswered'](answers); + + // Expect one missing question + expect(missingQuestions.length).toBe(1); + expect(missingQuestions[0].id).toBe(2); + expect(component.form.nativeElement.querySelector).toHaveBeenCalledWith('#q-2'); + }); + + it('should return empty array when either answer or file is provided for required question in review mode', () => { + // Set action to review + component.action = 'review'; + + // Set up mock assessment with required questions for reviewer + component.assessment = { + id: 1, + type: 'default', + isForTeam: false, + groups: [ + { + name: 'Group 1', + questions: [ + { + id: 1, + name: 'Question 1', + type: 'text', + isRequired: true, + audience: ['reviewer'] + } + ] + } + ] + } as any; + + // Mock answers for review (both answer and file are provided) + const answers = [ + { questionId: 1, answer: 'Some answer', file: null } + ]; + + // Test the function + const missingQuestions = component['_compulsoryQuestionsAnswered'](answers); + + // Expect no missing questions + expect(missingQuestions.length).toBe(0); + }); + + it('should handle review action properly', () => { + // Set action to review + component.action = 'review'; + + // Set up mock assessment with required questions for reviewer + component.assessment = { + id: 1, + type: 'default', + isForTeam: false, + groups: [ + { + name: 'Group 1', + questions: [ + { + id: 1, + name: 'Question 1', + type: 'text', + isRequired: true, + audience: ['reviewer'] + } + ] + } + ] + } as any; + + // Mock answers for review (both answer and file are empty) + const answers = [ + { questionId: 1, answer: '', file: null } + ]; + + // Mock form element + spyOn(component.form.nativeElement, 'querySelector').and.returnValue({ + classList: { + add: jasmine.createSpy('add') + } + }); + + // Test the function + const missingQuestions = component['_compulsoryQuestionsAnswered'](answers); + + // Expect one missing question + expect(missingQuestions.length).toBe(1); + expect(missingQuestions[0].id).toBe(1); + }); + }); }); diff --git a/projects/v3/src/app/components/assessment/assessment.component.ts b/projects/v3/src/app/components/assessment/assessment.component.ts index 87c7cc850..778b07b43 100644 --- a/projects/v3/src/app/components/assessment/assessment.component.ts +++ b/projects/v3/src/app/components/assessment/assessment.component.ts @@ -466,7 +466,19 @@ Best regards`; this.assessment.groups.forEach(group => { group.questions.forEach(question => { if (this._isRequired(question)) { - if (this.utils.isEmpty(answered[question.id]) || this.utils.isEmpty(answered[question.id].answer)) { + let isEmpty = false; + const thisQuestion = answered[question.id]; + + // for review: answer & file separated + if (this.action === 'review' && this.utils.isEmpty(thisQuestion.answer) && this.utils.isEmpty(thisQuestion.file)) { + isEmpty = true; + + // for assessment: file is part of the answer + } else if (this.action === 'assessment' && (this.utils.isEmpty(thisQuestion) || this.utils.isEmpty(thisQuestion.answer))) { + isEmpty = true; + } + + if (isEmpty) { missing.push(question); // add highlight effect to the question