Skip to content

Commit b9d60fc

Browse files
committed
Add some tests to multipleChoiceService.
1 parent 00489b0 commit b9d60fc

2 files changed

Lines changed: 67 additions & 7 deletions

File tree

src/main/webapp/site/src/app/services/multipleChoiceService.spec.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ let choiceId2: string = 'bbbbbbbbbb';
1818
let choiceId3: string = 'cccccccccc';
1919
let choiceText1: string = 'Apple';
2020
let choiceText2: string = 'Banana';
21+
let choiceText3: string = 'Cherry';
2122
let choice1: any;
2223
let choice2: any;
24+
let choice3: any;
2325
let nodeId1: string = 'node1';
2426
let componentId1: string = 'abcdefghij';
2527

@@ -43,6 +45,7 @@ describe('MultipleChoiceService', () => {
4345
studentDataService = TestBed.get(StudentDataService);
4446
choice1 = createChoice(choiceId1, choiceText1, '', false);
4547
choice2 = createChoice(choiceId2, choiceText2, '', false);
48+
choice3 = createChoice(choiceId3, choiceText3, '', false);
4649
});
4750
createComponent();
4851
choiceChosen();
@@ -52,6 +55,9 @@ describe('MultipleChoiceService', () => {
5255
getStudentDataString();
5356
componentStateHasStudentWork();
5457
componentHasCorrectAnswer();
58+
isRadioCorrect();
59+
isCheckboxCorrect();
60+
isChoiceIdsSame();
5561
});
5662

5763
function createComponent() {
@@ -83,7 +89,7 @@ function createComponentState(studentChoices: any[], isSubmit: boolean = false)
8389
};
8490
}
8591

86-
function createMultipleChoiceComponent(choices: any[]) {
92+
function createComponentContent(choices: any[]) {
8793
return {
8894
choices: choices
8995
};
@@ -251,7 +257,7 @@ function componentStateHasStudentWork() {
251257

252258
function componentHasCorrectAnswer() {
253259
function expectComponentHasCorrectAnswer(expectedResult: boolean) {
254-
const component = createMultipleChoiceComponent([
260+
const component = createComponentContent([
255261
createChoice(choiceId1, choiceText1, '', expectedResult)
256262
]);
257263
expect(service.componentHasCorrectAnswer(component)).toEqual(expectedResult);
@@ -263,3 +269,61 @@ function componentHasCorrectAnswer() {
263269
expectComponentHasCorrectAnswer(true);
264270
});
265271
}
272+
273+
function isRadioCorrect() {
274+
let componentContent: any;
275+
beforeEach(() => {
276+
componentContent = createComponentContent([createChoice(choiceId1, choiceText1, '', true)]);
277+
});
278+
function expectIsRadioCorrect(chosenChoices: any[], expectedResult: boolean): void {
279+
const componentState = createComponentState(chosenChoices, true);
280+
expect(service.isRadioCorrect(componentContent, componentState)).toBe(expectedResult);
281+
}
282+
it('should check if a radio component state is correct when it is not correct', () => {
283+
expectIsRadioCorrect([choice2], false);
284+
});
285+
it('should check if a radio component state is correct when it is correct', () => {
286+
expectIsRadioCorrect([choice1], true);
287+
});
288+
}
289+
290+
function isCheckboxCorrect() {
291+
let componentContent: any;
292+
beforeEach(() => {
293+
componentContent = createComponentContent([
294+
createChoice(choiceId1, choiceText1, '', true),
295+
createChoice(choiceId2, choiceText2, '', true)
296+
]);
297+
});
298+
function expectIsCheckboxCorrect(chosenChoices: any[], expectedResult: boolean): void {
299+
const componentState = createComponentState(chosenChoices, true);
300+
expect(service.isCheckboxCorrect(componentContent, componentState)).toBe(expectedResult);
301+
}
302+
it('should check if a checkbox component state is correct when it is not correct', () => {
303+
expectIsCheckboxCorrect([choice1, choice2, choice3], false);
304+
});
305+
it('should check if a checkbox component state is correct when it is correct', () => {
306+
expectIsCheckboxCorrect([choice1, choice2], true);
307+
});
308+
}
309+
310+
function isChoiceIdsSame() {
311+
function expectChoiceIdsSame(
312+
choiceIds1: string[],
313+
choiceIds2: string[],
314+
expectedResult: boolean
315+
): void {
316+
expect(service.isChoiceIdsSame(choiceIds1, choiceIds2)).toEqual(expectedResult);
317+
}
318+
it('should check if choice ids are the same when they are not the same', () => {
319+
expectChoiceIdsSame([choiceId1], [choiceId2], false);
320+
});
321+
it(`should check if choice ids are the same when there are different numbers of choice
322+
ids`, () => {
323+
expectChoiceIdsSame([choiceId1, choiceId2], [choiceId1], false);
324+
});
325+
it(`should check if choice ids are the same when they are the same but in different
326+
order`, () => {
327+
expectChoiceIdsSame([choiceId1, choiceId2], [choiceId2, choiceId1], true);
328+
});
329+
}

src/main/webapp/wise5/components/multipleChoice/multipleChoiceService.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,7 @@ export class MultipleChoiceService extends ComponentService {
209209
}
210210

211211
getChoicesIdsStudentChose(studentChoices: any[]): string[] {
212-
const studentChoiceIds: string[] = [];
213-
for (const studentChoice of studentChoices) {
214-
studentChoiceIds.push(studentChoice.id);
215-
}
216-
return studentChoiceIds;
212+
return studentChoices.map((studentChoice) => studentChoice.id);
217213
}
218214

219215
isChoiceIdsSame(choiceIds1: string[], choiceIds2: string[]): boolean {

0 commit comments

Comments
 (0)