From 58d518d1b58d247da62deff5e3c5863ff0c8c3e3 Mon Sep 17 00:00:00 2001 From: Hiroki Terashima Date: Fri, 15 Nov 2024 09:53:53 -0800 Subject: [PATCH] refactor(MultipleChoiceStudent): encapsulate answer checking functions (#1993) --- .../multiple-choice-student.component.spec.ts | 28 +++++++------------ .../multiple-choice-student.component.ts | 5 ++-- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/assets/wise5/components/multipleChoice/multiple-choice-student/multiple-choice-student.component.spec.ts b/src/assets/wise5/components/multipleChoice/multiple-choice-student/multiple-choice-student.component.spec.ts index fbb859d5879..6139bd87d87 100644 --- a/src/assets/wise5/components/multipleChoice/multiple-choice-student/multiple-choice-student.component.spec.ts +++ b/src/assets/wise5/components/multipleChoice/multiple-choice-student/multiple-choice-student.component.spec.ts @@ -130,7 +130,6 @@ describe('MultipleChoiceStudentComponent', () => { spyOn(component, 'studentDataChanged').and.callFake(() => {}); fixture.detectChanges(); }); - testMultipleAnswerComponent(); testSingleAnswerSingleCorrectAnswerComponent(); testSingleAnswerMultipleCorrectAnswersComponent(); @@ -189,7 +188,6 @@ function testSingleAnswerMultipleCorrectAnswersComponent() { JSON.stringify(singleAnswerMultipleCorrectAnswersComponent) ); component.component = new MultipleChoiceComponent(component.componentContent, nodeId); - component.ngOnInit(); }); singleAnswerMultipleCorrectAnswersComponentShouldShowCorrect(); @@ -204,15 +202,10 @@ function selectMultipleAnswerChoice(choiceId) { component.addOrRemoveFromStudentChoices(choiceId); } -function checkAnswer() { - component.checkAnswer(); -} - function singleAnswerSingleCorrectAnswerComponentShouldShowTheFeedbackOnTheSubmittedChoice() { it('should show the feedback on the submitted choice', () => { selectSingleAnswerChoice(choiceId1); - checkAnswer(); - component.displayFeedback(); + component.createComponentStateAndBroadcast('submit'); const choice1 = getChoiceById(choiceId1); const choice2 = getChoiceById(choiceId2); const choice3 = getChoiceById(choiceId3); @@ -232,7 +225,7 @@ function getChoiceById(id: string): any { function singleAnswerSingleCorrectAnswerComponentShouldShowIncorrect() { it(`should show incorrect when the incorrect answer is submitted`, () => { selectSingleAnswerChoice(choiceId1); - checkAnswer(); + component.createComponentStateAndBroadcast('submit'); expect(component.isCorrect).toBeFalsy(); }); } @@ -240,7 +233,7 @@ function singleAnswerSingleCorrectAnswerComponentShouldShowIncorrect() { function singleAnswerSingleCorrectAnswerComponentShouldShowCorrect() { it(`should show correct when the correct answer is submitted`, () => { selectSingleAnswerChoice(choiceId3); - checkAnswer(); + component.createComponentStateAndBroadcast('submit'); expect(component.isCorrect).toBeTruthy(); }); } @@ -248,10 +241,10 @@ function singleAnswerSingleCorrectAnswerComponentShouldShowCorrect() { function singleAnswerMultipleCorrectAnswersComponentShouldShowCorrect() { it(`should show correct when one of the multiple correct answers is submitted`, () => { selectSingleAnswerChoice(choiceId2); - checkAnswer(); + component.createComponentStateAndBroadcast('submit'); expect(component.isCorrect).toBeTruthy(); selectSingleAnswerChoice(choiceId3); - checkAnswer(); + component.createComponentStateAndBroadcast('submit'); expect(component.isCorrect).toBeTruthy(); }); } @@ -261,8 +254,7 @@ function multipleAnswerComponentShouldShowTheFeedbackOnTheSubmittedChoices() { selectMultipleAnswerChoice(choiceId1); selectMultipleAnswerChoice(choiceId2); selectMultipleAnswerChoice(choiceId3); - checkAnswer(); - component.displayFeedback(); + component.createComponentStateAndBroadcast('submit'); const choice1 = getChoiceById(choiceId1); const choice2 = getChoiceById(choiceId2); const choice3 = getChoiceById(choiceId3); @@ -278,7 +270,7 @@ function multipleAnswerComponentShouldShowTheFeedbackOnTheSubmittedChoices() { function multipleAnswerComponentShouldShowIncorrectWhenTheIncorrectAnswerIsSubmitted() { it(`should show incorrect when the incorrect answer is submitted`, () => { selectMultipleAnswerChoice(choiceId1); - checkAnswer(); + component.createComponentStateAndBroadcast('submit'); expect(component.isCorrect).toBeFalsy(); }); } @@ -288,7 +280,7 @@ function multipleAnswerComponentShouldShowIncorrectWhenNotJustTheCorrectAnswersA selectMultipleAnswerChoice(choiceId1); selectMultipleAnswerChoice(choiceId2); selectMultipleAnswerChoice(choiceId3); - checkAnswer(); + component.createComponentStateAndBroadcast('submit'); expect(component.isCorrect).toBeFalsy(); }); } @@ -296,7 +288,7 @@ function multipleAnswerComponentShouldShowIncorrectWhenNotJustTheCorrectAnswersA function multipleAnswerComponentShouldShowIncorrectWhenNotAllTheCorrectAnswersAreSubmitted() { it(`should show incorrect when not all the correct answers are submitted`, () => { selectMultipleAnswerChoice(choiceId2); - checkAnswer(); + component.createComponentStateAndBroadcast('submit'); expect(component.isCorrect).toBeFalsy(); }); } @@ -305,7 +297,7 @@ function multipleAnswerComponentShouldShowCorrectWhenOnlyTheCorrectAnswersAreSub it(`should show correct when only the correct answers are submitted`, () => { selectMultipleAnswerChoice(choiceId2); selectMultipleAnswerChoice(choiceId3); - checkAnswer(); + component.createComponentStateAndBroadcast('submit'); expect(component.isCorrect).toBeTruthy(); }); } diff --git a/src/assets/wise5/components/multipleChoice/multiple-choice-student/multiple-choice-student.component.ts b/src/assets/wise5/components/multipleChoice/multiple-choice-student/multiple-choice-student.component.ts index 9904bf56974..9022b89875d 100644 --- a/src/assets/wise5/components/multipleChoice/multiple-choice-student/multiple-choice-student.component.ts +++ b/src/assets/wise5/components/multipleChoice/multiple-choice-student/multiple-choice-student.component.ts @@ -248,7 +248,7 @@ export class MultipleChoiceStudent extends ComponentStudent { this.choices.forEach((choice) => (choice.showFeedback = false)); } - checkAnswer(): void { + private checkAnswer(): void { if (this.component.isRadio()) { this.isCorrect = this.choices.some((choice) => choice.isCorrect && this.isChecked(choice.id)); } else { @@ -256,7 +256,7 @@ export class MultipleChoiceStudent extends ComponentStudent { } } - displayFeedback(): void { + private displayFeedback(): void { this.choices.forEach((choice) => this.displayFeedbackOnChoice(choice)); } @@ -291,7 +291,6 @@ export class MultipleChoiceStudent extends ComponentStudent { const studentData: any = { studentChoices: this.getStudentChoiceObjects() }; - if (action === 'submit') { if (this.componentHasCorrectAnswer) { this.checkAnswer();