|
| 1 | +import { fireEvent, render, screen, waitFor, within } from '@testing-library/vue'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import Vue from 'vue'; |
| 4 | +import VueRouter from 'vue-router'; |
| 5 | +import EditableChipList from '../index.vue'; |
| 6 | +import { qtiEditorStrings } from '../../../qtiEditorStrings'; |
| 7 | +import { stubProseMirrorLayout } from 'shared/utils/testing'; |
| 8 | +import { getTipTapEditorStrings } from 'shared/views/TipTapEditor/TipTapEditor/TipTapEditorStrings'; |
| 9 | + |
| 10 | +// jsdom defines `ontouchstart`, which would put the editor in its touch layout. |
| 11 | +jest.mock('shared/utils/browserInfo', () => ({ isTouchDevice: false })); |
| 12 | + |
| 13 | +const { |
| 14 | + distractorsLabel$, |
| 15 | + addDistractorBtn$, |
| 16 | + editDistractorLabel$, |
| 17 | + deleteDistractorBtn$, |
| 18 | + saveChipBtn$, |
| 19 | +} = qtiEditorStrings; |
| 20 | +const { TipTapEditorLabel$, insertImage$, closeModal$, textFormatOptions$ } = |
| 21 | + getTipTapEditorStrings(); |
| 22 | + |
| 23 | +const CHIPS = [ |
| 24 | + { id: 'choice_a', content: '<p>Antonio</p>' }, |
| 25 | + { id: 'choice_b', content: '<p>Prospero</p>' }, |
| 26 | +]; |
| 27 | + |
| 28 | +const LABELS = { |
| 29 | + addLabel: addDistractorBtn$(), |
| 30 | + listLabel: distractorsLabel$(), |
| 31 | + chipLabel: number => editDistractorLabel$({ number }), |
| 32 | + deleteLabel: number => deleteDistractorBtn$({ number }), |
| 33 | +}; |
| 34 | + |
| 35 | +const Host = { |
| 36 | + components: { EditableChipList }, |
| 37 | + template: ` |
| 38 | + <EditableChipList |
| 39 | + :chips="chips" |
| 40 | + :addMode="addMode" |
| 41 | + v-bind="labels" |
| 42 | + @add-chip="html => chips.push({ id: 'choice_new', content: html })" |
| 43 | + @update-chip="(index, html) => chips.splice(index, 1, { ...chips[index], content: html })" |
| 44 | + @remove-chip="index => chips.splice(index, 1)" |
| 45 | + /> |
| 46 | + `, |
| 47 | + props: { |
| 48 | + addMode: { type: String, default: 'button' }, |
| 49 | + initialChips: { type: Array, default: () => CHIPS }, |
| 50 | + }, |
| 51 | + data() { |
| 52 | + return { chips: [...this.initialChips], labels: LABELS }; |
| 53 | + }, |
| 54 | +}; |
| 55 | + |
| 56 | +const list = () => screen.getByRole('list', { name: distractorsLabel$() }); |
| 57 | +const chipTexts = () => |
| 58 | + within(list()) |
| 59 | + .queryAllByRole('listitem') |
| 60 | + .map(item => item.textContent.trim()); |
| 61 | +const button = name => screen.getByRole('button', { name }); |
| 62 | +const editButton = number => button(editDistractorLabel$({ number })); |
| 63 | +const openEditor = () => screen.queryByRole('textbox', { name: TipTapEditorLabel$() }); |
| 64 | + |
| 65 | +async function writeIn(user, opener) { |
| 66 | + await user.click(opener); |
| 67 | + await waitFor(() => expect(openEditor()).toContainElement(document.activeElement)); |
| 68 | + await user.keyboard('!'); |
| 69 | +} |
| 70 | + |
| 71 | +describe('EditableChipList save', () => { |
| 72 | + stubProseMirrorLayout(); |
| 73 | + |
| 74 | + let consoleError; |
| 75 | + |
| 76 | + beforeEach(() => { |
| 77 | + consoleError = jest.spyOn(console, 'error'); |
| 78 | + }); |
| 79 | + |
| 80 | + afterEach(() => { |
| 81 | + try { |
| 82 | + expect(consoleError).not.toHaveBeenCalled(); |
| 83 | + } finally { |
| 84 | + consoleError.mockRestore(); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + const renderHost = (addMode, initialChips) => { |
| 89 | + const user = userEvent.setup(); |
| 90 | + render(Host, { props: { addMode, initialChips }, routes: new VueRouter() }); |
| 91 | + return user; |
| 92 | + }; |
| 93 | + |
| 94 | + describe.each(['button', 'region'])('in %s mode', addMode => { |
| 95 | + it('closes an open chip on a click on its save button, keeping the edit', async () => { |
| 96 | + const user = renderHost(addMode); |
| 97 | + await writeIn(user, editButton(2)); |
| 98 | + |
| 99 | + await user.click(button(saveChipBtn$())); |
| 100 | + |
| 101 | + await waitFor(() => expect(editButton(2)).toHaveFocus()); |
| 102 | + expect(openEditor()).toBeNull(); |
| 103 | + expect(chipTexts()).toEqual(['Antonio', 'Prospero!']); |
| 104 | + }); |
| 105 | + |
| 106 | + it('closes an open chip on Enter on its save button, keeping the edit', async () => { |
| 107 | + const user = renderHost(addMode); |
| 108 | + await writeIn(user, editButton(2)); |
| 109 | + |
| 110 | + button(saveChipBtn$()).focus(); |
| 111 | + await user.keyboard('{Enter}'); |
| 112 | + |
| 113 | + await waitFor(() => expect(editButton(2)).toHaveFocus()); |
| 114 | + expect(openEditor()).toBeNull(); |
| 115 | + expect(chipTexts()).toEqual(['Antonio', 'Prospero!']); |
| 116 | + }); |
| 117 | + |
| 118 | + it('closes and removes an open chip on a click on its delete button', async () => { |
| 119 | + const user = renderHost(addMode); |
| 120 | + await writeIn(user, editButton(2)); |
| 121 | + |
| 122 | + await user.click(button(deleteDistractorBtn$({ number: 2 }))); |
| 123 | + |
| 124 | + expect(openEditor()).toBeNull(); |
| 125 | + expect(chipTexts()).toEqual(['Antonio']); |
| 126 | + }); |
| 127 | + |
| 128 | + it('keeps an open chip open when its image dialog closes, and saves it after', async () => { |
| 129 | + const user = renderHost(addMode); |
| 130 | + await writeIn(user, editButton(2)); |
| 131 | + |
| 132 | + await user.click(within(openEditor()).getByRole('button', { name: insertImage$() })); |
| 133 | + // A browser runs Vue's render between a native click's listeners, unmounting the |
| 134 | + // dialog before the click bubbles out of it; jsdom renders only after dispatch. |
| 135 | + const closeButton = await screen.findByRole('button', { name: closeModal$() }); |
| 136 | + closeButton.addEventListener('click', () => closeButton.remove()); |
| 137 | + await user.click(closeButton); |
| 138 | + |
| 139 | + expect(openEditor()).not.toBeNull(); |
| 140 | + await user.click(button(saveChipBtn$())); |
| 141 | + |
| 142 | + await waitFor(() => expect(editButton(2)).toHaveFocus()); |
| 143 | + expect(openEditor()).toBeNull(); |
| 144 | + expect(chipTexts()).toEqual(['Antonio', 'Prospero!']); |
| 145 | + }); |
| 146 | + |
| 147 | + describe.each([ |
| 148 | + ['the gap beside the closed chips', list], |
| 149 | + ["the open chip's message line", () => document.querySelector('li.is-editing .is-reserved')], |
| 150 | + ])('on a click on %s', (_, target) => { |
| 151 | + it('closes an open chip, keeping the edit, without opening another', async () => { |
| 152 | + const user = renderHost(addMode); |
| 153 | + await writeIn(user, editButton(2)); |
| 154 | + |
| 155 | + await user.click(target()); |
| 156 | + |
| 157 | + await waitFor(() => expect(openEditor()).toBeNull()); |
| 158 | + expect(chipTexts()).toEqual(['Antonio', 'Prospero!']); |
| 159 | + }); |
| 160 | + |
| 161 | + it('only dismisses an open toolbar menu, keeping the chip open', async () => { |
| 162 | + const user = renderHost(addMode); |
| 163 | + await writeIn(user, editButton(2)); |
| 164 | + const formatButton = within(openEditor()).getByRole('button', { |
| 165 | + name: textFormatOptions$(), |
| 166 | + }); |
| 167 | + await user.click(formatButton); |
| 168 | + await waitFor(() => expect(formatButton).toHaveAttribute('aria-expanded', 'true')); |
| 169 | + |
| 170 | + await user.click(target()); |
| 171 | + |
| 172 | + await waitFor(() => expect(formatButton).toHaveAttribute('aria-expanded', 'false')); |
| 173 | + expect(openEditor()).not.toBeNull(); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + describe('with a blank chip open', () => { |
| 178 | + const BLANK_SECOND = [ |
| 179 | + { id: 'choice_a', content: '<p>Antonio</p>' }, |
| 180 | + { id: 'choice_b', content: '' }, |
| 181 | + { id: 'choice_c', content: '<p>Caliban</p>' }, |
| 182 | + { id: 'choice_d', content: '<p>Miranda</p>' }, |
| 183 | + ]; |
| 184 | + |
| 185 | + const openBlank = async addMode => { |
| 186 | + const user = renderHost(addMode, BLANK_SECOND); |
| 187 | + await user.click(editButton(2)); |
| 188 | + await waitFor(() => expect(openEditor()).toContainElement(document.activeElement)); |
| 189 | + return user; |
| 190 | + }; |
| 191 | + |
| 192 | + it('removes only it on a click on its delete button', async () => { |
| 193 | + const user = await openBlank(addMode); |
| 194 | + |
| 195 | + await user.click(button(deleteDistractorBtn$({ number: 2 }))); |
| 196 | + |
| 197 | + expect(openEditor()).toBeNull(); |
| 198 | + expect(chipTexts()).toEqual(['Antonio', 'Caliban', 'Miranda']); |
| 199 | + }); |
| 200 | + |
| 201 | + it('removes a later chip whose delete button is clicked', async () => { |
| 202 | + const user = await openBlank(addMode); |
| 203 | + |
| 204 | + await user.click(button(deleteDistractorBtn$({ number: 3 }))); |
| 205 | + |
| 206 | + expect(openEditor()).toBeNull(); |
| 207 | + expect(chipTexts()).toEqual(['Antonio', 'Miranda']); |
| 208 | + }); |
| 209 | + |
| 210 | + it('opens the chip that is clicked', async () => { |
| 211 | + const user = await openBlank(addMode); |
| 212 | + |
| 213 | + await user.click(editButton(3)); |
| 214 | + |
| 215 | + await waitFor(() => expect(openEditor()).toHaveTextContent('Caliban')); |
| 216 | + expect(chipTexts()).toHaveLength(3); |
| 217 | + }); |
| 218 | + }); |
| 219 | + |
| 220 | + it('throws a written draft away on a click on its discard button', async () => { |
| 221 | + const user = renderHost(addMode); |
| 222 | + await writeIn(user, button(addDistractorBtn$())); |
| 223 | + |
| 224 | + await user.click(button(deleteDistractorBtn$({ number: 3 }))); |
| 225 | + |
| 226 | + expect(openEditor()).toBeNull(); |
| 227 | + expect(chipTexts()).toEqual(['Antonio', 'Prospero']); |
| 228 | + }); |
| 229 | + }); |
| 230 | + |
| 231 | + it('adds a region-mode draft on save without opening another', async () => { |
| 232 | + const user = renderHost('region'); |
| 233 | + await writeIn(user, button(addDistractorBtn$())); |
| 234 | + |
| 235 | + await user.click(button(saveChipBtn$())); |
| 236 | + |
| 237 | + await waitFor(() => expect(button(addDistractorBtn$())).toHaveFocus()); |
| 238 | + expect(openEditor()).toBeNull(); |
| 239 | + expect(chipTexts()).toEqual(['Antonio', 'Prospero', '!']); |
| 240 | + }); |
| 241 | + |
| 242 | + describe('in region mode, rendering between listeners as a browser does', () => { |
| 243 | + beforeEach(() => { |
| 244 | + Vue.config.async = false; |
| 245 | + }); |
| 246 | + |
| 247 | + afterEach(() => { |
| 248 | + Vue.config.async = true; |
| 249 | + }); |
| 250 | + |
| 251 | + it.each([ |
| 252 | + ['a chip', () => editButton(2), () => document.querySelector('.pool-content')], |
| 253 | + ['a draft', () => button(addDistractorBtn$()), () => document.querySelector('.pool-content')], |
| 254 | + ['a draft', () => button(addDistractorBtn$()), list], |
| 255 | + ])('closes %s on a click in the box without opening a draft', async (_, opener, target) => { |
| 256 | + const user = renderHost('region'); |
| 257 | + await writeIn(user, opener()); |
| 258 | + |
| 259 | + await user.click(target()); |
| 260 | + |
| 261 | + expect(openEditor()).toBeNull(); |
| 262 | + }); |
| 263 | + }); |
| 264 | + |
| 265 | + it('adds a draft on a save click that moves no focus', async () => { |
| 266 | + const user = renderHost('region'); |
| 267 | + await writeIn(user, button(addDistractorBtn$())); |
| 268 | + |
| 269 | + await fireEvent.click(button(saveChipBtn$())); |
| 270 | + |
| 271 | + expect(openEditor()).toBeNull(); |
| 272 | + await waitFor(() => expect(chipTexts()).toEqual(['Antonio', 'Prospero', '!'])); |
| 273 | + }); |
| 274 | +}); |
0 commit comments