|
| 1 | +import { ref } from 'vue'; |
| 2 | +import { useChoiceInteraction } from '../useChoiceInteraction'; |
| 3 | +import { QuestionType } from '../../constants'; |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// Helpers |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | + |
| 9 | +function makeAnswer(overrides = {}) { |
| 10 | + return { id: 'choice_a', content: 'A', correct: false, fixed: false, ...overrides }; |
| 11 | +} |
| 12 | + |
| 13 | +function makeBlock(choices, questionType = QuestionType.SINGLE_SELECT) { |
| 14 | + const maxChoices = questionType === QuestionType.SINGLE_SELECT ? 1 : 2; |
| 15 | + const correctIds = choices.filter(a => a.correct).map(a => a.id); |
| 16 | + |
| 17 | + const bodyXml = `<qti-choice-interaction response-identifier="RESPONSE" max-choices="${maxChoices}"> |
| 18 | + ${choices.map(a => `<qti-simple-choice identifier="${a.id}">${a.content}</qti-simple-choice>`).join('\n ')} |
| 19 | + </qti-choice-interaction>`; |
| 20 | + |
| 21 | + const declaration = `<qti-response-declaration identifier="RESPONSE" |
| 22 | + cardinality="${questionType === QuestionType.SINGLE_SELECT ? 'single' : 'multiple'}" |
| 23 | + base-type="identifier"> |
| 24 | + <qti-correct-response> |
| 25 | + ${correctIds.map(id => `<qti-value>${id}</qti-value>`).join('')} |
| 26 | + </qti-correct-response> |
| 27 | + </qti-response-declaration>`; |
| 28 | + |
| 29 | + return { bodyXml, responseDeclarations: [declaration] }; |
| 30 | +} |
| 31 | + |
| 32 | +function setup(choices, questionType = QuestionType.SINGLE_SELECT) { |
| 33 | + const qt = ref(questionType); |
| 34 | + const block = makeBlock(choices, questionType); |
| 35 | + return { qt, ...useChoiceInteraction(block, qt) }; |
| 36 | +} |
| 37 | + |
| 38 | +// --------------------------------------------------------------------------- |
| 39 | +// Tests |
| 40 | +// --------------------------------------------------------------------------- |
| 41 | + |
| 42 | +describe('useChoiceInteraction', () => { |
| 43 | + describe('addChoice()', () => { |
| 44 | + it('appends a new choice to the list', () => { |
| 45 | + const { state, addChoice } = setup([ |
| 46 | + makeAnswer({ id: 'a', content: 'A' }), |
| 47 | + makeAnswer({ id: 'b', content: 'B' }), |
| 48 | + ]); |
| 49 | + addChoice(); |
| 50 | + expect(state.value.choices).toHaveLength(3); |
| 51 | + }); |
| 52 | + |
| 53 | + it('new choice has a generated "choice_" identifier', () => { |
| 54 | + const { state, addChoice } = setup([makeAnswer({ id: 'a' }), makeAnswer({ id: 'b' })]); |
| 55 | + addChoice(); |
| 56 | + const newAnswer = state.value.choices[2]; |
| 57 | + expect(newAnswer.id).toMatch(/^choice_[a-z0-9]{8}$/); |
| 58 | + }); |
| 59 | + |
| 60 | + it('new choice has empty content and correct: false', () => { |
| 61 | + const { state, addChoice } = setup([makeAnswer({ id: 'a' }), makeAnswer({ id: 'b' })]); |
| 62 | + addChoice(); |
| 63 | + const newAnswer = state.value.choices[2]; |
| 64 | + expect(newAnswer.content).toBe(''); |
| 65 | + expect(newAnswer.correct).toBe(false); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('removeChoice()', () => { |
| 70 | + it('removes the choice with the given id', () => { |
| 71 | + const { state, removeChoice } = setup([makeAnswer({ id: 'a' }), makeAnswer({ id: 'b' })]); |
| 72 | + removeChoice('a'); |
| 73 | + expect(state.value.choices.find(a => a.id === 'a')).toBeUndefined(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('is a no-op when only one choice remains', () => { |
| 77 | + const { state, removeChoice } = setup([makeAnswer({ id: 'a' })]); |
| 78 | + removeChoice('a'); |
| 79 | + expect(state.value.choices).toHaveLength(1); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('moveChoiceUp()', () => { |
| 84 | + it('swaps choice with the previous one', () => { |
| 85 | + const { state, moveChoiceUp } = setup([ |
| 86 | + makeAnswer({ id: 'a' }), |
| 87 | + makeAnswer({ id: 'b' }), |
| 88 | + makeAnswer({ id: 'c' }), |
| 89 | + ]); |
| 90 | + moveChoiceUp('b'); |
| 91 | + expect(state.value.choices.map(a => a.id)).toEqual(['b', 'a', 'c']); |
| 92 | + }); |
| 93 | + |
| 94 | + it('is a no-op when the choice is first', () => { |
| 95 | + const { state, moveChoiceUp } = setup([makeAnswer({ id: 'a' }), makeAnswer({ id: 'b' })]); |
| 96 | + moveChoiceUp('a'); |
| 97 | + expect(state.value.choices.map(a => a.id)).toEqual(['a', 'b']); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('moveChoiceDown()', () => { |
| 102 | + it('swaps choice with the next one', () => { |
| 103 | + const { state, moveChoiceDown } = setup([ |
| 104 | + makeAnswer({ id: 'a' }), |
| 105 | + makeAnswer({ id: 'b' }), |
| 106 | + makeAnswer({ id: 'c' }), |
| 107 | + ]); |
| 108 | + moveChoiceDown('b'); |
| 109 | + expect(state.value.choices.map(a => a.id)).toEqual(['a', 'c', 'b']); |
| 110 | + }); |
| 111 | + |
| 112 | + it('is a no-op when the choice is last', () => { |
| 113 | + const { state, moveChoiceDown } = setup([makeAnswer({ id: 'a' }), makeAnswer({ id: 'b' })]); |
| 114 | + moveChoiceDown('b'); |
| 115 | + expect(state.value.choices.map(a => a.id)).toEqual(['a', 'b']); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe('toggleCorrectChoice()', () => { |
| 120 | + it('singleSelect: sets only the target as correct and clears others', () => { |
| 121 | + const { state, toggleCorrectChoice, qt } = setup([ |
| 122 | + makeAnswer({ id: 'a', correct: true }), |
| 123 | + makeAnswer({ id: 'b', correct: false }), |
| 124 | + ]); |
| 125 | + qt.value = QuestionType.SINGLE_SELECT; |
| 126 | + toggleCorrectChoice('b'); |
| 127 | + expect(state.value.choices.find(a => a.id === 'b').correct).toBe(true); |
| 128 | + expect(state.value.choices.find(a => a.id === 'a').correct).toBe(false); |
| 129 | + }); |
| 130 | + |
| 131 | + it('multiSelect: toggles only the target, leaves others unchanged', () => { |
| 132 | + const { state, toggleCorrectChoice, qt } = setup( |
| 133 | + [makeAnswer({ id: 'a', correct: true }), makeAnswer({ id: 'b', correct: false })], |
| 134 | + QuestionType.MULTI_SELECT, |
| 135 | + ); |
| 136 | + qt.value = QuestionType.MULTI_SELECT; |
| 137 | + toggleCorrectChoice('b'); |
| 138 | + expect(state.value.choices.find(a => a.id === 'b').correct).toBe(true); |
| 139 | + expect(state.value.choices.find(a => a.id === 'a').correct).toBe(true); |
| 140 | + }); |
| 141 | + |
| 142 | + it('multiSelect: toggles correct off when already correct', () => { |
| 143 | + const { state, toggleCorrectChoice, qt } = setup( |
| 144 | + [makeAnswer({ id: 'a', correct: true }), makeAnswer({ id: 'b', correct: true })], |
| 145 | + QuestionType.MULTI_SELECT, |
| 146 | + ); |
| 147 | + qt.value = QuestionType.MULTI_SELECT; |
| 148 | + toggleCorrectChoice('a'); |
| 149 | + expect(state.value.choices.find(a => a.id === 'a').correct).toBe(false); |
| 150 | + expect(state.value.choices.find(a => a.id === 'b').correct).toBe(true); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + describe('setPrompt()', () => { |
| 155 | + it('updates the prompt field', () => { |
| 156 | + const { state, setPrompt } = setup([makeAnswer({ id: 'a' }), makeAnswer({ id: 'b' })]); |
| 157 | + setPrompt('<p>New prompt</p>'); |
| 158 | + expect(state.value.prompt).toBe('<p>New prompt</p>'); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + describe('setChoiceContent()', () => { |
| 163 | + it('updates content for the target choice only', () => { |
| 164 | + const { state, setChoiceContent } = setup([ |
| 165 | + makeAnswer({ id: 'a', content: 'Old' }), |
| 166 | + makeAnswer({ id: 'b', content: 'Unchanged' }), |
| 167 | + ]); |
| 168 | + setChoiceContent('a', 'New content'); |
| 169 | + expect(state.value.choices.find(a => a.id === 'a').content).toBe('New content'); |
| 170 | + expect(state.value.choices.find(a => a.id === 'b').content).toBe('Unchanged'); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('setShuffle()', () => { |
| 175 | + it('updates the shuffle flag', () => { |
| 176 | + const { state, setShuffle } = setup([makeAnswer({ id: 'a' }), makeAnswer({ id: 'b' })]); |
| 177 | + setShuffle(true); |
| 178 | + expect(state.value.shuffle).toBe(true); |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments