Skip to content

Commit b3342cb

Browse files
authored
Fix oppia#19975: Ensure misconception feedback option state is saved correctly (oppia#22032)
* resolve bug with misconception feedback checkbox not saving state * refactor question misconception editor to manage feedback and update test * refactor question misconception editor to manage feedback and update test * refactor question misconception editor to manage feedback and update test * update test * update test * refactor question misconception editor to manage feedback and update test * refactor question misconception editor to manage feedback and update test
1 parent 3c375ae commit b3342cb

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

core/templates/components/question-directives/question-misconception-editor/question-misconception-editor.component.spec.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,57 @@ describe('Question Misconception Editor Component', () => {
120120
);
121121
});
122122

123-
it('should use feedback by default', () => {
123+
it('should initialize feedbackIsUsed to true if no previous state exists', () => {
124+
component.previousFeedbackIsUsed = null;
125+
component.ngOnInit();
126+
127+
expect(component.feedbackIsUsed).toBeTrue();
128+
});
129+
130+
it('should initialize feedbackIsUsed to true if previous state was true', () => {
131+
component.previousFeedbackIsUsed = true;
132+
component.ngOnInit();
133+
124134
expect(component.feedbackIsUsed).toBeTrue();
125-
expect(component.misconceptionsBySkill).toEqual(mockMisconceptionObject);
135+
});
136+
137+
it('should initialize feedbackIsUsed to false if previous state was false', () => {
138+
component.previousFeedbackIsUsed = false;
139+
component.ngOnInit();
140+
141+
expect(component.feedbackIsUsed).toBeFalse();
142+
});
143+
144+
it('should update feedbackIsUsed and store the previous state', () => {
145+
component.feedbackIsUsed = true;
146+
147+
const updatedValues = {
148+
misconception: mockMisconceptionObject.abc[1],
149+
skillId: 'id',
150+
feedbackIsUsed: false,
151+
};
152+
component.updateValues(updatedValues);
153+
154+
expect(component.feedbackIsUsed).toBeFalse();
155+
expect(component.previousFeedbackIsUsed).toBeTrue();
156+
});
157+
158+
it('should clear outcome feedback if feedbackIsUsed is false and feedback matches misconception feedback', () => {
159+
component.selectedMisconception = mockMisconceptionObject.abc[0];
160+
component.selectedMisconceptionSkillId = 'abc';
161+
component.feedbackIsUsed = false;
162+
component.outcome.feedback.html =
163+
mockMisconceptionObject.abc[0].getFeedback();
164+
165+
const saveAnswerGroupFeedbackSpy = spyOn(
166+
component.saveAnswerGroupFeedback,
167+
'emit'
168+
);
169+
170+
component.updateMisconception();
171+
172+
const emittedOutcome = saveAnswerGroupFeedbackSpy.calls.first().args[0];
173+
expect(emittedOutcome.feedback.html).toEqual('');
126174
});
127175

128176
it('should enable edit mode correctly', () => {

core/templates/components/question-directives/question-misconception-editor/question-misconception-editor.component.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class QuestionMisconceptionEditorComponent implements OnInit {
6565
selectedMisconceptionSkillId!: string;
6666
feedbackIsUsed: boolean = false;
6767
misconceptionEditorIsOpen: boolean = false;
68+
previousFeedbackIsUsed: boolean | null = null;
6869
directiveSubscriptions = new Subscription();
6970

7071
constructor(
@@ -95,8 +96,12 @@ export class QuestionMisconceptionEditorComponent implements OnInit {
9596
this.initValues();
9697
})
9798
);
99+
if (this.previousFeedbackIsUsed !== null) {
100+
this.feedbackIsUsed = this.previousFeedbackIsUsed;
101+
} else {
102+
this.feedbackIsUsed = true;
103+
}
98104
}
99-
this.feedbackIsUsed = true;
100105
}
101106

102107
initValues(): void {
@@ -115,6 +120,11 @@ export class QuestionMisconceptionEditorComponent implements OnInit {
115120
this.misconceptionName = misconceptions[i].getName();
116121
this.selectedMisconception = misconceptions[i];
117122
this.selectedMisconceptionSkillId = skillId;
123+
if (this.previousFeedbackIsUsed === null) {
124+
this.feedbackIsUsed =
125+
this.outcome.feedback.html.trim() ===
126+
misconceptions[i].getFeedback().trim();
127+
}
118128
}
119129
}
120130
} else {
@@ -149,9 +159,12 @@ export class QuestionMisconceptionEditorComponent implements OnInit {
149159
}
150160

151161
updateValues(newValues: MisconceptionUpdatedValues): void {
162+
if (this.feedbackIsUsed !== newValues.feedbackIsUsed) {
163+
this.previousFeedbackIsUsed = this.feedbackIsUsed;
164+
this.feedbackIsUsed = newValues.feedbackIsUsed;
165+
}
152166
this.selectedMisconception = newValues.misconception;
153167
this.selectedMisconceptionSkillId = newValues.skillId;
154-
this.feedbackIsUsed = newValues.feedbackIsUsed;
155168
}
156169

157170
tagAnswerGroupWithMisconception(): void {
@@ -190,9 +203,14 @@ export class QuestionMisconceptionEditorComponent implements OnInit {
190203
let outcome = cloneDeep(this.outcome);
191204
if (this.feedbackIsUsed) {
192205
outcome.feedback.html = this.selectedMisconception.getFeedback();
193-
this.saveAnswerGroupFeedback.emit(outcome);
194-
this.externalSaveService.onExternalSave.emit();
206+
} else {
207+
if (outcome.feedback.html === this.selectedMisconception.getFeedback()) {
208+
outcome.feedback.html = '';
209+
}
195210
}
211+
212+
this.saveAnswerGroupFeedback.emit(outcome);
213+
this.externalSaveService.onExternalSave.emit();
196214
this.misconceptionEditorIsOpen = false;
197215
}
198216

core/templates/components/question-directives/question-misconception-selector/question-misconception-selector.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export class QuestionMisconceptionSelectorComponent implements OnInit {
5050
constructor(private stateEditorService: StateEditorService) {}
5151

5252
ngOnInit(): void {
53-
this.misconceptionFeedbackIsUsed = true;
53+
if (this.misconceptionFeedbackIsUsed !== false) {
54+
this.misconceptionFeedbackIsUsed = true;
55+
}
5456
this.misconceptionsBySkill =
5557
this.stateEditorService.getMisconceptionsBySkill();
5658
}

0 commit comments

Comments
 (0)