Skip to content

Commit d438463

Browse files
rtibblesbotclaude
andcommitted
Emit update:interaction only on a content change
Choice, ordering, text entry and inline choice editors no longer emit on mount, on entering edit mode, or on a rebuild equal by value. Text entry still reports a stored numeric answer it canonicalizes on opening. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 680cd7f commit d438463

8 files changed

Lines changed: 183 additions & 65 deletions

File tree

‎contentcuration/contentcuration/frontend/shared/views/QTIEditor/interactions/choice/Editor.vue‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
import { ValidationError } from '../../constants';
271271
import { generateRandomSlug } from '../../utils/generateRandomSlug';
272272
import { useChoiceInteraction } from '../../composables/useChoiceInteraction';
273+
import { useInteractionUpdates } from '../../composables/useInteractionUpdates';
273274
import ValidationMessage from '../../components/ValidationMessage/index.vue';
274275
import AddListItemButton from '../../components/AddListItemButton/index.vue';
275276
import ClickableRegion from '../../components/ClickableRegion/index.vue';
@@ -395,7 +396,7 @@
395396
bodyXml: bodyXml.value,
396397
responseDeclarations: responseDeclarations.value,
397398
}));
398-
watch(workingInteraction, newVal => emit('update:interaction', newVal), { immediate: true });
399+
useInteractionUpdates(workingInteraction, props, emit);
399400
400401
// Errors are reported the same way, for the card to show that the question needs work.
401402
watch(errors, newVal => emit('update:errors', newVal), { immediate: true });

‎contentcuration/contentcuration/frontend/shared/views/QTIEditor/interactions/choice/__tests__/Editor.spec.js‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
CHOICE_MULTI_DECL_XML as MULTI_DECL,
1414
mockInteractionBlock as block,
1515
mockInteractionBlockWithDecl as blockWithDecl,
16+
updates,
1617
} from '../../../utils/testingFixtures';
1718
import { QuestionType } from '../../../constants';
1819
import { qtiEditorStrings as tr } from '../../../qtiEditorStrings';
@@ -68,6 +69,8 @@ const renderEditor = (props = {}) =>
6869
routes: new VueRouter(),
6970
});
7071

72+
const openTextarea = () => screen.queryAllByRole('textbox').find(el => el.tagName === 'TEXTAREA');
73+
7174
const dragFirstRowToLast = async () => {
7275
const { options, el } = mockSortableInstances[mockSortableInstances.length - 1];
7376
options.onEnd({
@@ -655,25 +658,62 @@ describe('ChoiceInteractionEditor', () => {
655658
});
656659

657660
describe('emits', () => {
658-
it('emits update:interaction on mount with the rebuilt XML and declarations', () => {
661+
it('emits update:interaction for an edit made as the card closes', async () => {
662+
const { emitted, updateProps } = renderEditor({
663+
interaction: block(CHOICE_SINGLE_SELECT_XML),
664+
questionType: QuestionType.SINGLE_SELECT,
665+
});
666+
fireEvent.click(screen.getByRole('button', { name: /add choice/i }));
667+
await updateProps({ mode: 'view' });
668+
expect(updates(emitted)).toHaveLength(1);
669+
});
670+
671+
it('does not emit update:interaction on mount in edit mode', () => {
672+
const { emitted } = renderEditor({
673+
interaction: block(CHOICE_SINGLE_SELECT_XML),
674+
questionType: QuestionType.SINGLE_SELECT,
675+
});
676+
expect(updates(emitted)).toHaveLength(0);
677+
});
678+
679+
it('does not emit update:interaction when the mode changes', async () => {
680+
const { emitted, updateProps } = renderEditor({
681+
interaction: block(CHOICE_SINGLE_SELECT_XML),
682+
questionType: QuestionType.SINGLE_SELECT,
683+
mode: 'view',
684+
});
685+
await updateProps({ mode: 'edit' });
686+
await updateProps({ mode: 'view' });
687+
expect(updates(emitted)).toHaveLength(0);
688+
});
689+
690+
it('does not emit update:interaction when the question is retyped unchanged', async () => {
659691
const { emitted } = renderEditor({
660692
interaction: block(CHOICE_SINGLE_SELECT_XML),
661693
questionType: QuestionType.SINGLE_SELECT,
662694
});
663-
expect(emitted()['update:interaction']).toBeTruthy();
664-
const payload = emitted()['update:interaction'][0][0];
665-
expect(typeof payload.bodyXml).toBe('string');
666-
expect(Array.isArray(payload.responseDeclarations)).toBe(true);
695+
await fireEvent.update(openTextarea(), openTextarea().value);
696+
expect(updates(emitted)).toHaveLength(0);
697+
});
698+
699+
it('emits update:interaction once when switching to multiple select', async () => {
700+
const { emitted, updateProps } = renderEditor({
701+
interaction: block(CHOICE_SINGLE_SELECT_XML),
702+
questionType: QuestionType.SINGLE_SELECT,
703+
});
704+
await updateProps({ questionType: QuestionType.MULTI_SELECT });
705+
expect(updates(emitted)).toHaveLength(1);
706+
expect(updates(emitted)[0][0].responseDeclarations[0]).toContain('cardinality="multiple"');
667707
});
668708

669709
it('emits update:interaction after adding a choice', async () => {
670710
const { emitted } = renderEditor({
671711
interaction: block(CHOICE_SINGLE_SELECT_XML),
672712
questionType: QuestionType.SINGLE_SELECT,
673713
});
674-
const before = emitted()['update:interaction'].length;
675714
await fireEvent.click(screen.getByRole('button', { name: /add choice/i }));
676-
expect(emitted()['update:interaction'].length).toBeGreaterThan(before);
715+
const { bodyXml } = updates(emitted).at(-1)[0];
716+
expect(bodyXml.match(/<qti-simple-choice/g)).toHaveLength(4);
677717
});
678718
});
679719

‎contentcuration/contentcuration/frontend/shared/views/QTIEditor/interactions/inlineChoice/Editor.vue‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
import { qtiEditorStrings } from '../../qtiEditorStrings';
139139
import { ValidationError } from '../../constants';
140140
import { useInlineChoiceInteraction } from '../../composables/useInlineChoiceInteraction';
141+
import { useInteractionUpdates } from '../../composables/useInteractionUpdates';
141142
import ValidationMessage from '../../components/ValidationMessage/index.vue';
142143
import ClickableRegion from '../../components/ClickableRegion/index.vue';
143144
import AnswerSettings from '../choice/components/AnswerSettings/index.vue';
@@ -337,7 +338,7 @@
337338
bodyXml: bodyXml.value,
338339
responseDeclarations: responseDeclarations.value,
339340
}));
340-
watch(workingInteraction, newVal => emit('update:interaction', newVal), { immediate: true });
341+
useInteractionUpdates(workingInteraction, props, emit);
341342
342343
// Errors are reported the same way, for the card to show that the question needs work.
343344
watch(errors, newVal => emit('update:errors', newVal), { immediate: true });

‎contentcuration/contentcuration/frontend/shared/views/QTIEditor/interactions/inlineChoice/__tests__/Editor.spec.js‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { inlineChoiceInteractionDescriptor as descriptor } from '../Descriptor';
77
import { getDropdowns } from '../parse';
88
import { QuestionType } from '../../../constants';
99
import { qtiEditorStrings as tr } from '../../../qtiEditorStrings';
10+
import { updates } from '../../../utils/testingFixtures';
1011
import { CHIP, chip } from './renderPassage';
1112
import { dragSortStrings as dragTr } from 'shared/views/dragSort/dragSortStrings';
1213
import { getTipTapEditorStrings } from 'shared/views/TipTapEditor/TipTapEditor/TipTapEditorStrings';
@@ -97,7 +98,7 @@ const section = name => screen.getByRole('heading', { name }).parentElement;
9798

9899
/** The dropdowns of the last interaction the editor emitted, as saved and read back. */
99100
function savedDropdowns(emitted) {
100-
const [interaction] = emitted['update:interaction'].at(-1);
101+
const [interaction] = updates(emitted).at(-1);
101102
const state = descriptor.parse(interaction.bodyXml, interaction.responseDeclarations);
102103
return getDropdowns(state.passage);
103104
}
@@ -216,7 +217,7 @@ describe('InlineChoiceEditor', () => {
216217

217218
expect(input).toHaveValue('spins');
218219
expect(chips()[1]).toHaveTextContent('spins');
219-
expect(savedDropdowns(emitted())[1].options).toEqual([
220+
expect(savedDropdowns(emitted)[1].options).toEqual([
220221
{ id: 'revolves', text: 'spins' },
221222
{ id: 'circles', text: 'circles' },
222223
]);
@@ -231,7 +232,7 @@ describe('InlineChoiceEditor', () => {
231232
await waitFor(() => expect(optionInputs()[2]).toHaveFocus());
232233
await user.keyboard('rotates');
233234
expect(chips()[1]).toHaveTextContent(/^3/);
234-
expect(savedDropdowns(emitted())[1].options.map(o => o.text)).toEqual([
235+
expect(savedDropdowns(emitted)[1].options.map(o => o.text)).toEqual([
235236
'revolves',
236237
'circles',
237238
'rotates',
@@ -248,7 +249,7 @@ describe('InlineChoiceEditor', () => {
248249
expect(optionInputs().map(input => input.value)).toEqual(['Sun', 'Stars']);
249250
// The chip needs a correct answer, so its count is in its name, beside the error icon.
250251
expect(chips()[0]).toHaveAccessibleName(/2 options/);
251-
expect(savedDropdowns(emitted())[0].options.map(o => o.text)).toEqual(['Sun', 'Stars']);
252+
expect(savedDropdowns(emitted)[0].options.map(o => o.text)).toEqual(['Sun', 'Stars']);
252253
});
253254

254255
it('keeps focus in the panel on the option that takes a removed one’s place', async () => {
@@ -295,7 +296,7 @@ describe('InlineChoiceEditor', () => {
295296
expect(radio(2)).not.toBeChecked();
296297
expect(radio(3)).toBeChecked();
297298
expect(chips()[0]).toHaveTextContent('Stars');
298-
expect(savedDropdowns(emitted())[0].correctId).toBe('stars');
299+
expect(savedDropdowns(emitted)[0].correctId).toBe('stars');
299300
});
300301

301302
it('keeps the correct mark on the same option when it moves', async () => {
@@ -313,7 +314,7 @@ describe('InlineChoiceEditor', () => {
313314
expect(
314315
screen.getByRole('radio', { name: tr.markInlineChoiceOptionCorrect$({ number: 2 }) }),
315316
).toBeChecked();
316-
const [, dropdown] = savedDropdowns(emitted());
317+
const [, dropdown] = savedDropdowns(emitted);
317318
expect(dropdown.options.map(o => o.text)).toEqual(['circles', 'revolves', 'rotates']);
318319
expect(dropdown.correctId).toBe('revolves');
319320
});
@@ -327,7 +328,7 @@ describe('InlineChoiceEditor', () => {
327328
);
328329

329330
expect(chips()[1]).toHaveTextContent(tr.errorNoCorrectAnswer$());
330-
expect(savedDropdowns(emitted())[1].correctId).toBeNull();
331+
expect(savedDropdowns(emitted)[1].correctId).toBeNull();
331332
});
332333

333334
it('restores the list when the edit is undone in the passage', async () => {
@@ -519,7 +520,7 @@ describe('InlineChoiceEditor', () => {
519520
const { emitted, user } = await renderEditor();
520521
await user.click(shuffle());
521522

522-
const [interaction] = emitted()['update:interaction'].at(-1);
523+
const [interaction] = updates(emitted).at(-1);
523524
expect([...interaction.bodyXml.matchAll(/shuffle="(\w+)"/g)].map(m => m[1])).toEqual([
524525
'true',
525526
'true',
@@ -690,7 +691,7 @@ describe('InlineChoiceEditor', () => {
690691
await screen.findByRole('dialog');
691692
await waitFor(() => expect(optionInputs()[0]).toHaveFocus());
692693
await user.keyboard('Venus');
693-
const saved = savedDropdowns(emitted());
694+
const saved = savedDropdowns(emitted);
694695
expect(saved).toHaveLength(3);
695696
expect(saved.map(d => d.options.map(o => o.text))).toContainEqual(['Venus']);
696697
});
@@ -717,16 +718,36 @@ describe('InlineChoiceEditor', () => {
717718
await openChip(user, 1);
718719
await user.type(optionInputs()[0], 's');
719720

720-
const [interaction] = emitted()['update:interaction'].at(-1);
721+
const [interaction] = updates(emitted).at(-1);
721722
expect(interaction.bodyXml).toContain(`src="${image}"`);
722723
expect(interaction.bodyXml).not.toContain('/content/storage/');
723724
});
724725

725-
it('does not save which chip is open', async () => {
726-
const { emitted, user } = await renderEditor();
727-
const before = emitted()['update:interaction'].at(-1)[0].bodyXml;
728-
await openChip(user, 0);
729-
expect(emitted()['update:interaction'].at(-1)[0].bodyXml).toBe(before);
726+
describe('emits', () => {
727+
it('emits update:interaction for an edit made as the card closes', async () => {
728+
const { emitted, updateProps } = await renderEditor();
729+
fireEvent.click(screen.getByRole('checkbox', { name: tr.shuffleAnswersLabel$() }));
730+
await updateProps({ mode: 'view' });
731+
expect(updates(emitted)).toHaveLength(1);
732+
});
733+
734+
it('does not emit update:interaction on mount in edit mode', async () => {
735+
const { emitted } = await renderEditor();
736+
expect(updates(emitted)).toHaveLength(0);
737+
});
738+
739+
it('does not emit update:interaction when the mode changes', async () => {
740+
const { emitted, updateProps } = await renderEditor({ mode: 'view' });
741+
await updateProps({ mode: 'edit' });
742+
await updateProps({ mode: 'view' });
743+
expect(updates(emitted)).toHaveLength(0);
744+
});
745+
746+
it('does not emit update:interaction on opening a chip', async () => {
747+
const { emitted, user } = await renderEditor();
748+
await openChip(user, 0);
749+
expect(updates(emitted)).toHaveLength(0);
750+
});
730751
});
731752

732753
it('restores every chip’s options and correct answer when reopened', async () => {
@@ -735,7 +756,7 @@ describe('InlineChoiceEditor', () => {
735756
await user.click(
736757
screen.getByRole('radio', { name: tr.markInlineChoiceOptionCorrect$({ number: 3 }) }),
737758
);
738-
const [saved] = emitted()['update:interaction'].at(-1);
759+
const [saved] = updates(emitted).at(-1);
739760
unmount();
740761

741762
await renderEditor({ interaction: saved });

‎contentcuration/contentcuration/frontend/shared/views/QTIEditor/interactions/ordering/Editor.vue‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
import { qtiEditorStrings } from '../../qtiEditorStrings';
203203
import { ValidationError } from '../../constants';
204204
import { useOrderingInteraction } from '../../composables/useOrderingInteraction';
205+
import { useInteractionUpdates } from '../../composables/useInteractionUpdates';
205206
import ValidationMessage from '../../components/ValidationMessage/index.vue';
206207
import AddListItemButton from '../../components/AddListItemButton/index.vue';
207208
import ClickableRegion from '../../components/ClickableRegion/index.vue';
@@ -305,7 +306,6 @@
305306
if (newMode === 'edit') {
306307
// Open the question, the first thing in the card, so the card opens at its start.
307308
openPrompt();
308-
emit('update:interaction', workingInteraction.value);
309309
} else {
310310
isPromptOpen.value = false;
311311
openItemId.value = null;
@@ -314,10 +314,7 @@
314314
{ immediate: true },
315315
);
316316
317-
watch(workingInteraction, newVal => {
318-
if (props.mode !== 'edit') return;
319-
emit('update:interaction', newVal);
320-
});
317+
useInteractionUpdates(workingInteraction, props, emit);
321318
322319
const isOnlyItem = computed(() => state.value.items.length <= 1);
323320

‎contentcuration/contentcuration/frontend/shared/views/QTIEditor/interactions/ordering/__tests__/Editor.spec.js‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ORDERING_DECL_XML,
1111
mockInteractionBlock as block,
1212
mockInteractionBlockWithDecl as blockWithDecl,
13+
updates,
1314
} from '../../../utils/testingFixtures';
1415
import { QuestionType } from '../../../constants';
1516
import { qtiEditorStrings as tr } from '../../../qtiEditorStrings';
@@ -294,25 +295,52 @@ describe('OrderingEditor', () => {
294295
});
295296

296297
describe('emits', () => {
297-
it('emits update:interaction on initial mount in edit mode', () => {
298+
it('emits update:interaction for an edit made as the card closes', async () => {
299+
const { emitted, updateProps } = renderEditor({
300+
interaction: blockWithDecl(ORDERING_XML, ORDERING_DECL_XML),
301+
questionType: QuestionType.ORDERING,
302+
});
303+
fireEvent.click(screen.getByRole('button', { name: tr.$tr('addItemBtn') }));
304+
await updateProps({ mode: 'view' });
305+
expect(updates(emitted)).toHaveLength(1);
306+
});
307+
308+
it('does not emit update:interaction on mount in edit mode', () => {
309+
const { emitted } = renderEditor({
310+
interaction: blockWithDecl(ORDERING_XML, ORDERING_DECL_XML),
311+
questionType: QuestionType.ORDERING,
312+
});
313+
expect(updates(emitted)).toHaveLength(0);
314+
});
315+
316+
it('does not emit update:interaction when the mode changes', async () => {
317+
const { emitted, updateProps } = renderEditor({
318+
interaction: blockWithDecl(ORDERING_XML, ORDERING_DECL_XML),
319+
questionType: QuestionType.ORDERING,
320+
mode: 'view',
321+
});
322+
await updateProps({ mode: 'edit' });
323+
await updateProps({ mode: 'view' });
324+
expect(updates(emitted)).toHaveLength(0);
325+
});
326+
327+
it('does not emit update:interaction when the question is retyped unchanged', async () => {
298328
const { emitted } = renderEditor({
299329
interaction: blockWithDecl(ORDERING_XML, ORDERING_DECL_XML),
300330
questionType: QuestionType.ORDERING,
301331
});
302-
expect(emitted()['update:interaction']).toBeTruthy();
303-
const payload = emitted()['update:interaction'][0][0];
304-
expect(typeof payload.bodyXml).toBe('string');
305-
expect(Array.isArray(payload.responseDeclarations)).toBe(true);
332+
await fireEvent.update(openTextarea(), openTextarea().value);
333+
expect(updates(emitted)).toHaveLength(0);
306334
});
307335

308336
it('emits update:interaction after adding an item', async () => {
309337
const { emitted } = renderEditor({
310338
interaction: blockWithDecl(ORDERING_XML, ORDERING_DECL_XML),
311339
questionType: QuestionType.ORDERING,
312340
});
313-
const before = emitted()['update:interaction'].length;
314341
await fireEvent.click(screen.getByRole('button', { name: tr.$tr('addItemBtn') }));
315-
expect(emitted()['update:interaction'].length).toBeGreaterThan(before);
342+
const { bodyXml } = updates(emitted).at(-1)[0];
343+
expect(bodyXml.match(/<qti-simple-choice/g)).toHaveLength(4);
316344
});
317345

318346
it('does not emit update:interaction in view mode', async () => {
@@ -322,8 +350,7 @@ describe('OrderingEditor', () => {
322350
mode: 'view',
323351
showAnswers: true,
324352
});
325-
// Clear mount-time emissions — none should fire in view mode
326-
expect(emitted()['update:interaction']).toBeFalsy();
353+
expect(updates(emitted)).toHaveLength(0);
327354
});
328355
});
329356

0 commit comments

Comments
 (0)