Repository navigation
Let Save and delete act on an open answer or distractor chip #6357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AlexVelezLl
merged 2 commits into
learningequality:unstable
from
rtibblesbot:issue-6331-5bef83
Nov 19, 2026
+441
−7
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
296 changes: 296 additions & 0 deletions
296
.../views/QTIEditor/components/EditableChipList/__tests__/EditableChipListWithEditor.spec.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,296 @@ | ||
| import { fireEvent, render, screen, waitFor, within } from '@testing-library/vue'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import Vue from 'vue'; | ||
| import VueRouter from 'vue-router'; | ||
| import EditableChipList from '../index.vue'; | ||
| import { qtiEditorStrings } from '../../../qtiEditorStrings'; | ||
| import { stubProseMirrorLayout } from 'shared/utils/testing'; | ||
| import { getTipTapEditorStrings } from 'shared/views/TipTapEditor/TipTapEditor/TipTapEditorStrings'; | ||
|
|
||
| // jsdom defines `ontouchstart`, which would put the editor in its touch layout. | ||
| jest.mock('shared/utils/browserInfo', () => ({ isTouchDevice: false })); | ||
|
|
||
| const { | ||
| distractorsLabel$, | ||
| addDistractorBtn$, | ||
| editDistractorLabel$, | ||
| deleteDistractorBtn$, | ||
| saveChipBtn$, | ||
| } = qtiEditorStrings; | ||
| const { TipTapEditorLabel$, insertImage$, closeModal$, textFormatOptions$ } = | ||
| getTipTapEditorStrings(); | ||
|
|
||
| const CHIPS = [ | ||
| { id: 'choice_a', content: '<p>Antonio</p>' }, | ||
| { id: 'choice_b', content: '<p>Prospero</p>' }, | ||
| ]; | ||
|
|
||
| const LABELS = { | ||
| addLabel: addDistractorBtn$(), | ||
| listLabel: distractorsLabel$(), | ||
| chipLabel: number => editDistractorLabel$({ number }), | ||
| deleteLabel: number => deleteDistractorBtn$({ number }), | ||
| }; | ||
|
|
||
| const Host = { | ||
| components: { EditableChipList }, | ||
| template: ` | ||
| <div> | ||
| <EditableChipList | ||
| v-if="shown" | ||
| :chips="chips" | ||
| :addMode="addMode" | ||
| v-bind="labels" | ||
| @add-chip="html => chips.push({ id: 'choice_new', content: html })" | ||
| @update-chip="(index, html) => chips.splice(index, 1, { ...chips[index], content: html })" | ||
| @remove-chip="index => chips.splice(index, 1)" | ||
| /> | ||
| <ul v-else aria-label="Saved"> | ||
| <li v-for="chip in chips" :key="chip.id" v-html="chip.content" /> | ||
| </ul> | ||
| <button @click="shown = false">Close question</button> | ||
| </div> | ||
| `, | ||
| props: { | ||
| addMode: { type: String, default: 'button' }, | ||
| initialChips: { type: Array, default: () => CHIPS }, | ||
| }, | ||
| data() { | ||
| return { chips: [...this.initialChips], labels: LABELS, shown: true }; | ||
| }, | ||
| }; | ||
|
|
||
| const list = () => screen.getByRole('list', { name: distractorsLabel$() }); | ||
| const chipTexts = () => | ||
| within(list()) | ||
| .queryAllByRole('listitem') | ||
| .map(item => item.textContent.trim()); | ||
| const button = name => screen.getByRole('button', { name }); | ||
| const editButton = number => button(editDistractorLabel$({ number })); | ||
| const openEditor = () => screen.queryByRole('textbox', { name: TipTapEditorLabel$() }); | ||
|
|
||
| async function writeIn(user, opener) { | ||
| await user.click(opener); | ||
| await waitFor(() => expect(openEditor()).toContainElement(document.activeElement)); | ||
| await user.keyboard('!'); | ||
| } | ||
|
|
||
| // Separate from EditableChipList.spec.js, which mocks TipTapEditor: these run the real one. | ||
| describe('EditableChipList with the real TipTapEditor', () => { | ||
| stubProseMirrorLayout(); | ||
|
|
||
| let consoleError; | ||
|
|
||
| beforeEach(() => { | ||
| consoleError = jest.spyOn(console, 'error'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| try { | ||
| expect(consoleError).not.toHaveBeenCalled(); | ||
| } finally { | ||
| consoleError.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| const renderHost = (addMode, initialChips) => { | ||
| const user = userEvent.setup(); | ||
| render(Host, { props: { addMode, initialChips }, routes: new VueRouter() }); | ||
| return user; | ||
| }; | ||
|
|
||
| describe.each(['button', 'region'])('in %s mode', addMode => { | ||
| it.each([ | ||
| ['a click on', user => user.click(button(saveChipBtn$()))], | ||
| [ | ||
| 'Enter on', | ||
| user => { | ||
| button(saveChipBtn$()).focus(); | ||
| return user.keyboard('{Enter}'); | ||
| }, | ||
| ], | ||
| ])('closes an open chip on %s its save button, keeping the edit', async (_, save) => { | ||
| const user = renderHost(addMode); | ||
| await writeIn(user, editButton(2)); | ||
|
|
||
| await save(user); | ||
|
|
||
| await waitFor(() => expect(editButton(2)).toHaveFocus()); | ||
| expect(openEditor()).toBeNull(); | ||
| expect(chipTexts()).toEqual(['Antonio', 'Prospero!']); | ||
| }); | ||
|
|
||
| it('closes and removes an open chip on a click on its delete button', async () => { | ||
| const user = renderHost(addMode); | ||
| await writeIn(user, editButton(2)); | ||
|
|
||
| await user.click(button(deleteDistractorBtn$({ number: 2 }))); | ||
|
|
||
| expect(openEditor()).toBeNull(); | ||
| expect(chipTexts()).toEqual(['Antonio']); | ||
| }); | ||
|
|
||
| it('keeps an open chip open when its image dialog closes, and saves it after', async () => { | ||
| const user = renderHost(addMode); | ||
| await writeIn(user, editButton(2)); | ||
|
|
||
| await user.click(within(openEditor()).getByRole('button', { name: insertImage$() })); | ||
| // A browser runs Vue's render between a native click's listeners, unmounting the | ||
| // dialog before the click bubbles out of it; jsdom renders only after dispatch. | ||
| const closeButton = await screen.findByRole('button', { name: closeModal$() }); | ||
| closeButton.addEventListener('click', () => closeButton.remove()); | ||
| await user.click(closeButton); | ||
|
|
||
| expect(openEditor()).not.toBeNull(); | ||
| await user.click(button(saveChipBtn$())); | ||
|
|
||
| await waitFor(() => expect(editButton(2)).toHaveFocus()); | ||
| expect(openEditor()).toBeNull(); | ||
| expect(chipTexts()).toEqual(['Antonio', 'Prospero!']); | ||
| }); | ||
|
|
||
| describe.each([ | ||
| ['the gap beside the closed chips', list], | ||
| [ | ||
| "the space beside the open chip's save button", | ||
| () => document.querySelector('li.is-editing .save-row'), | ||
| ], | ||
| ])('on a click on %s', (_, target) => { | ||
| it('closes an open chip, keeping the edit, without opening another', async () => { | ||
| const user = renderHost(addMode); | ||
| await writeIn(user, editButton(2)); | ||
|
|
||
| await user.click(target()); | ||
|
|
||
| await waitFor(() => expect(openEditor()).toBeNull()); | ||
| expect(chipTexts()).toEqual(['Antonio', 'Prospero!']); | ||
| }); | ||
|
|
||
| it('only dismisses an open toolbar menu, keeping the chip open', async () => { | ||
| const user = renderHost(addMode); | ||
| await writeIn(user, editButton(2)); | ||
| const formatButton = within(openEditor()).getByRole('button', { | ||
| name: textFormatOptions$(), | ||
| }); | ||
| await user.click(formatButton); | ||
| await waitFor(() => expect(formatButton).toHaveAttribute('aria-expanded', 'true')); | ||
|
|
||
| await user.click(target()); | ||
|
|
||
| await waitFor(() => expect(formatButton).toHaveAttribute('aria-expanded', 'false')); | ||
| expect(openEditor()).not.toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with a blank chip open', () => { | ||
| const BLANK_SECOND = [ | ||
| { id: 'choice_a', content: '<p>Antonio</p>' }, | ||
| { id: 'choice_b', content: '' }, | ||
| { id: 'choice_c', content: '<p>Caliban</p>' }, | ||
| { id: 'choice_d', content: '<p>Miranda</p>' }, | ||
| ]; | ||
|
|
||
| const openBlank = async () => { | ||
| const user = renderHost(addMode, BLANK_SECOND); | ||
| await user.click(editButton(2)); | ||
| await waitFor(() => expect(openEditor()).toContainElement(document.activeElement)); | ||
| return user; | ||
| }; | ||
|
|
||
| it('removes only it on a click on its delete button', async () => { | ||
| const user = await openBlank(); | ||
|
|
||
| await user.click(button(deleteDistractorBtn$({ number: 2 }))); | ||
|
|
||
| expect(openEditor()).toBeNull(); | ||
| expect(chipTexts()).toEqual(['Antonio', 'Caliban', 'Miranda']); | ||
| }); | ||
|
|
||
| it('removes a later chip whose delete button is clicked', async () => { | ||
| const user = await openBlank(); | ||
|
|
||
| await user.click(button(deleteDistractorBtn$({ number: 3 }))); | ||
|
|
||
| expect(openEditor()).toBeNull(); | ||
| expect(chipTexts()).toEqual(['Antonio', 'Miranda']); | ||
| }); | ||
|
|
||
| it('opens the chip that is clicked', async () => { | ||
| const user = await openBlank(); | ||
|
|
||
| await user.click(editButton(3)); | ||
|
|
||
| await waitFor(() => expect(openEditor()).toHaveTextContent('Caliban')); | ||
| expect(chipTexts()).toHaveLength(3); | ||
| }); | ||
| }); | ||
|
|
||
| it('throws a written draft away on a click on its discard button', async () => { | ||
| const user = renderHost(addMode); | ||
| await writeIn(user, button(addDistractorBtn$())); | ||
|
|
||
| await user.click(button(deleteDistractorBtn$({ number: 3 }))); | ||
|
|
||
| expect(openEditor()).toBeNull(); | ||
| expect(chipTexts()).toEqual(['Antonio', 'Prospero']); | ||
| }); | ||
| }); | ||
|
|
||
| it('adds a region-mode draft on save without opening another', async () => { | ||
| const user = renderHost('region'); | ||
| await writeIn(user, button(addDistractorBtn$())); | ||
|
|
||
| await user.click(button(saveChipBtn$())); | ||
|
|
||
| await waitFor(() => expect(button(addDistractorBtn$())).toHaveFocus()); | ||
| expect(openEditor()).toBeNull(); | ||
| expect(chipTexts()).toEqual(['Antonio', 'Prospero', '!']); | ||
| }); | ||
|
|
||
| describe('in region mode, rendering between listeners as a browser does', () => { | ||
| beforeEach(() => { | ||
| Vue.config.async = false; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| Vue.config.async = true; | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['a chip', () => editButton(2), () => document.querySelector('.pool-content')], | ||
| ['a draft', () => button(addDistractorBtn$()), () => document.querySelector('.pool-content')], | ||
| ['a draft', () => button(addDistractorBtn$()), list], | ||
| ['a chip', () => editButton(2), () => document.querySelector('li.is-editing .save-row')], | ||
| ])('closes %s on a click in the box without opening a draft', async (_, opener, target) => { | ||
| const user = renderHost('region'); | ||
| await writeIn(user, opener()); | ||
|
|
||
| await user.click(target()); | ||
|
|
||
| await waitFor(() => expect(openEditor()).toBeNull()); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['an edited chip', () => editButton(2), ['Antonio', 'Prospero!']], | ||
| ['a written draft', () => button(addDistractorBtn$()), ['Antonio', 'Prospero', '!']], | ||
| ])('keeps %s on a click that unmounts the list', async (_, opener, expected) => { | ||
| const user = renderHost('region'); | ||
| await writeIn(user, opener()); | ||
|
|
||
| await user.click(button('Close question')); | ||
|
|
||
| const saved = within(screen.getByRole('list', { name: 'Saved' })).getAllByRole('listitem'); | ||
| expect(saved.map(item => item.textContent)).toEqual(expected); | ||
| }); | ||
| }); | ||
|
|
||
| it('adds a draft on a save click that moves no focus', async () => { | ||
| const user = renderHost('region'); | ||
| await writeIn(user, button(addDistractorBtn$())); | ||
|
|
||
| await fireEvent.click(button(saveChipBtn$())); | ||
|
|
||
| expect(openEditor()).toBeNull(); | ||
| await waitFor(() => expect(chipTexts()).toEqual(['Antonio', 'Prospero', '!'])); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a line on why this exists: a browser renders between a click's listeners, so a click that closes an editor inside the region turns the region back on before the click bubbles up to it. Without that, the check reads as arbitrary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added above
pressedWhileSuppressed: browsers run microtasks, so Vue renders, between a click's listeners, so closing an editor inside the region un-suppresses it before the click bubbles up. No other suppressed-region check in the branch.Written by rtibblesbot, an LLM-based coding agent.