Skip to content

Commit

Permalink
refactor(MultipleChoiceStudent): encapsulate answer checking functions (
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokiterashima authored Nov 15, 2024
1 parent ba29819 commit 58d518d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ describe('MultipleChoiceStudentComponent', () => {
spyOn(component, 'studentDataChanged').and.callFake(() => {});
fixture.detectChanges();
});

testMultipleAnswerComponent();
testSingleAnswerSingleCorrectAnswerComponent();
testSingleAnswerMultipleCorrectAnswersComponent();
Expand Down Expand Up @@ -189,7 +188,6 @@ function testSingleAnswerMultipleCorrectAnswersComponent() {
JSON.stringify(singleAnswerMultipleCorrectAnswersComponent)
);
component.component = new MultipleChoiceComponent(component.componentContent, nodeId);

component.ngOnInit();
});
singleAnswerMultipleCorrectAnswersComponentShouldShowCorrect();
Expand All @@ -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);
Expand All @@ -232,26 +225,26 @@ 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();
});
}

function singleAnswerSingleCorrectAnswerComponentShouldShowCorrect() {
it(`should show correct when the correct answer is submitted`, () => {
selectSingleAnswerChoice(choiceId3);
checkAnswer();
component.createComponentStateAndBroadcast('submit');
expect(component.isCorrect).toBeTruthy();
});
}

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();
});
}
Expand All @@ -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);
Expand All @@ -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();
});
}
Expand All @@ -288,15 +280,15 @@ function multipleAnswerComponentShouldShowIncorrectWhenNotJustTheCorrectAnswersA
selectMultipleAnswerChoice(choiceId1);
selectMultipleAnswerChoice(choiceId2);
selectMultipleAnswerChoice(choiceId3);
checkAnswer();
component.createComponentStateAndBroadcast('submit');
expect(component.isCorrect).toBeFalsy();
});
}

function multipleAnswerComponentShouldShowIncorrectWhenNotAllTheCorrectAnswersAreSubmitted() {
it(`should show incorrect when not all the correct answers are submitted`, () => {
selectMultipleAnswerChoice(choiceId2);
checkAnswer();
component.createComponentStateAndBroadcast('submit');
expect(component.isCorrect).toBeFalsy();
});
}
Expand All @@ -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();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,15 @@ 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 {
this.isCorrect = this.choices.every((choice) => this.isStudentChoiceValueCorrect(choice));
}
}

displayFeedback(): void {
private displayFeedback(): void {
this.choices.forEach((choice) => this.displayFeedbackOnChoice(choice));
}

Expand Down Expand Up @@ -291,7 +291,6 @@ export class MultipleChoiceStudent extends ComponentStudent {
const studentData: any = {
studentChoices: this.getStudentChoiceObjects()
};

if (action === 'submit') {
if (this.componentHasCorrectAnswer) {
this.checkAnswer();
Expand Down

0 comments on commit 58d518d

Please sign in to comment.