Skip to content

Commit

Permalink
fix(Constraint): Multiple Choice constraint with multiple choices
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreykwan committed Oct 2, 2023
1 parent d8ac11a commit 10f3da5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/app/services/multipleChoiceService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function isChoicesSelected() {
});
it(`should check if choices are selected when constraint choice ids is a string and the constraint
choice id is selected along with another choice`, () => {
expectIsChoicesSelected([choiceId1, choiceId3], choiceId3, false);
expectIsChoicesSelected([choiceId1, choiceId3], choiceId3, true);
});
it(`should check if choices are selected when constraint choice ids is a string and only the
constraint choice id is selected`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { StudentDataService } from '../../../services/studentDataService';
import { ChoiceChosenConstraintStrategy } from './ChoiceChosenConstraintStrategy';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { StudentTeacherCommonServicesModule } from '../../../../../app/student-teacher-common-services.module';
import { ComponentServiceLookupServiceModule } from '../../../services/componentServiceLookupServiceModule';
import { ComponentServiceLookupService } from '../../../services/componentServiceLookupService';

const choiceId1 = 'choice1';
const choiceId2 = 'choice2';
const choiceId3 = 'choice3';
let componentServiceLookupService: ComponentServiceLookupService;
let dataService: StudentDataService;
let getLatestComponentStateSpy: jasmine.Spy;
let strategy: ChoiceChosenConstraintStrategy;
Expand All @@ -25,9 +28,15 @@ const criteria = {
describe('ChoiceChosenConstraintStrategy', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, StudentTeacherCommonServicesModule]
imports: [
ComponentServiceLookupServiceModule,
HttpClientTestingModule,
StudentTeacherCommonServicesModule
]
});
strategy = new ChoiceChosenConstraintStrategy();
componentServiceLookupService = TestBed.inject(ComponentServiceLookupService);
strategy.componentServiceLookupService = componentServiceLookupService;
dataService = TestBed.inject(StudentDataService);
strategy.dataService = dataService;
getLatestComponentStateSpy = spyOn(
Expand Down Expand Up @@ -70,7 +79,7 @@ function oneChoiceChosen(): void {
}

function multipleChoicesChosen(): void {
describe('multipe choices are chosen', () => {
describe('multiple choices are chosen', () => {
describe('the expected choice is not chosen', () => {
it('returns false', () => {
setStudentChoicesEvaluateAndExpect([choice2, choice3], false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ import { AbstractConstraintStrategy } from './AbstractConstraintStrategy';

export class ChoiceChosenConstraintStrategy extends AbstractConstraintStrategy {
evaluate(criteria: any): boolean {
const service = this.componentServiceLookupService.getService('MultipleChoice');
const latestComponentState = this.dataService.getLatestComponentStateByNodeIdAndComponentId(
criteria.params.nodeId,
criteria.params.componentId
);
return latestComponentState != null && this.isChoiceChosen(criteria, latestComponentState);
}

private isChoiceChosen(criteria: any, componentState: any): boolean {
const studentChoiceIds = componentState.studentData.studentChoices.map(
(choice: any) => choice.id
);
return studentChoiceIds.includes(criteria.params.choiceIds);
return latestComponentState != null && service.choiceChosen(criteria, latestComponentState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ export class MultipleChoiceService extends ComponentService {
studentChoiceIds: string[],
constraintChoiceIds: string | string[]
): boolean {
return typeof constraintChoiceIds === 'string'
? studentChoiceIds.length === 1 && studentChoiceIds[0] === constraintChoiceIds
: arraysContainSameValues(studentChoiceIds, constraintChoiceIds);
if (constraintChoiceIds instanceof Array) {
return arraysContainSameValues(studentChoiceIds, constraintChoiceIds);
} else {
return studentChoiceIds.includes(constraintChoiceIds);
}
}

isCompleted(component: any, componentStates: any[], nodeEvents: any[], node: any) {
Expand Down

0 comments on commit 10f3da5

Please sign in to comment.