|
| 1 | +import { render, screen, waitFor, within } from '@testing-library/vue'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import VueRouter from 'vue-router'; |
| 4 | +import EditableChipList from '../index.vue'; |
| 5 | +import { qtiEditorStrings } from '../../../qtiEditorStrings'; |
| 6 | +import { stubProseMirrorLayout } from 'shared/utils/testing'; |
| 7 | +import { getTipTapEditorStrings } from 'shared/views/TipTapEditor/TipTapEditor/TipTapEditorStrings'; |
| 8 | + |
| 9 | +// jsdom defines `ontouchstart`, which would put the editor in its touch layout. |
| 10 | +jest.mock('shared/utils/browserInfo', () => ({ isTouchDevice: false })); |
| 11 | + |
| 12 | +const { |
| 13 | + distractorsLabel$, |
| 14 | + addDistractorBtn$, |
| 15 | + editDistractorLabel$, |
| 16 | + deleteDistractorBtn$, |
| 17 | + saveChipBtn$, |
| 18 | +} = qtiEditorStrings; |
| 19 | +const { TipTapEditorLabel$ } = getTipTapEditorStrings(); |
| 20 | + |
| 21 | +const CHIPS = [ |
| 22 | + { id: 'choice_a', content: '<p>Antonio</p>' }, |
| 23 | + { id: 'choice_b', content: '<p>Prospero</p>' }, |
| 24 | +]; |
| 25 | + |
| 26 | +const LABELS = { |
| 27 | + addLabel: addDistractorBtn$(), |
| 28 | + listLabel: distractorsLabel$(), |
| 29 | + chipLabel: number => editDistractorLabel$({ number }), |
| 30 | + deleteLabel: number => deleteDistractorBtn$({ number }), |
| 31 | +}; |
| 32 | + |
| 33 | +const Host = { |
| 34 | + components: { EditableChipList }, |
| 35 | + template: ` |
| 36 | + <EditableChipList |
| 37 | + :chips="chips" |
| 38 | + :addMode="addMode" |
| 39 | + v-bind="labels" |
| 40 | + @add-chip="html => chips.push({ id: 'choice_new', content: html })" |
| 41 | + @update-chip="(index, html) => chips.splice(index, 1, { ...chips[index], content: html })" |
| 42 | + @remove-chip="index => chips.splice(index, 1)" |
| 43 | + /> |
| 44 | + `, |
| 45 | + props: { |
| 46 | + addMode: { type: String, default: 'button' }, |
| 47 | + initialChips: { type: Array, default: () => CHIPS }, |
| 48 | + }, |
| 49 | + data() { |
| 50 | + return { chips: [...this.initialChips], labels: LABELS }; |
| 51 | + }, |
| 52 | +}; |
| 53 | + |
| 54 | +const list = () => screen.getByRole('list', { name: distractorsLabel$() }); |
| 55 | +const chipTexts = () => |
| 56 | + within(list()) |
| 57 | + .queryAllByRole('listitem') |
| 58 | + .map(item => item.textContent.trim()); |
| 59 | +const button = name => screen.getByRole('button', { name }); |
| 60 | +const editButton = number => button(editDistractorLabel$({ number })); |
| 61 | +const openEditor = () => screen.queryByRole('textbox', { name: TipTapEditorLabel$() }); |
| 62 | + |
| 63 | +async function writeIn(user, opener) { |
| 64 | + await user.click(opener); |
| 65 | + await waitFor(() => expect(openEditor()).toContainElement(document.activeElement)); |
| 66 | + await user.keyboard('!'); |
| 67 | +} |
| 68 | + |
| 69 | +describe('EditableChipList save', () => { |
| 70 | + stubProseMirrorLayout(); |
| 71 | + |
| 72 | + let consoleError; |
| 73 | + |
| 74 | + beforeEach(() => { |
| 75 | + consoleError = jest.spyOn(console, 'error'); |
| 76 | + }); |
| 77 | + |
| 78 | + afterEach(() => { |
| 79 | + try { |
| 80 | + expect(consoleError).not.toHaveBeenCalled(); |
| 81 | + } finally { |
| 82 | + consoleError.mockRestore(); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + const renderHost = (addMode, initialChips) => { |
| 87 | + const user = userEvent.setup(); |
| 88 | + render(Host, { props: { addMode, initialChips }, routes: new VueRouter() }); |
| 89 | + return user; |
| 90 | + }; |
| 91 | + |
| 92 | + describe.each(['button', 'region'])('in %s mode', addMode => { |
| 93 | + it('closes an open chip on a click on its save button, keeping the edit', async () => { |
| 94 | + const user = renderHost(addMode); |
| 95 | + await writeIn(user, editButton(2)); |
| 96 | + |
| 97 | + await user.click(button(saveChipBtn$())); |
| 98 | + |
| 99 | + await waitFor(() => expect(editButton(2)).toHaveFocus()); |
| 100 | + expect(openEditor()).toBeNull(); |
| 101 | + expect(chipTexts()).toEqual(['Antonio', 'Prospero!']); |
| 102 | + }); |
| 103 | + |
| 104 | + it('closes an open chip on Enter on its save button, keeping the edit', async () => { |
| 105 | + const user = renderHost(addMode); |
| 106 | + await writeIn(user, editButton(2)); |
| 107 | + |
| 108 | + button(saveChipBtn$()).focus(); |
| 109 | + await user.keyboard('{Enter}'); |
| 110 | + |
| 111 | + await waitFor(() => expect(editButton(2)).toHaveFocus()); |
| 112 | + expect(openEditor()).toBeNull(); |
| 113 | + expect(chipTexts()).toEqual(['Antonio', 'Prospero!']); |
| 114 | + }); |
| 115 | + |
| 116 | + it('closes and removes an open chip on a click on its delete button', async () => { |
| 117 | + const user = renderHost(addMode); |
| 118 | + await writeIn(user, editButton(2)); |
| 119 | + |
| 120 | + await user.click(button(deleteDistractorBtn$({ number: 2 }))); |
| 121 | + |
| 122 | + expect(openEditor()).toBeNull(); |
| 123 | + expect(chipTexts()).toEqual(['Antonio']); |
| 124 | + }); |
| 125 | + |
| 126 | + describe.each([ |
| 127 | + ['the gap beside the closed chips', () => document.querySelector('ul.chip-list')], |
| 128 | + ["the open chip's message line", () => document.querySelector('li.is-editing .is-reserved')], |
| 129 | + ])('on a click on %s', (_, target) => { |
| 130 | + it('closes an open chip, keeping the edit, without opening another', async () => { |
| 131 | + const user = renderHost(addMode); |
| 132 | + await writeIn(user, editButton(2)); |
| 133 | + |
| 134 | + await user.click(target()); |
| 135 | + |
| 136 | + await waitFor(() => expect(openEditor()).toBeNull()); |
| 137 | + expect(chipTexts()).toEqual(['Antonio', 'Prospero!']); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe('with a blank chip open', () => { |
| 142 | + const BLANK_SECOND = [ |
| 143 | + { id: 'choice_a', content: '<p>Antonio</p>' }, |
| 144 | + { id: 'choice_b', content: '' }, |
| 145 | + { id: 'choice_c', content: '<p>Caliban</p>' }, |
| 146 | + { id: 'choice_d', content: '<p>Miranda</p>' }, |
| 147 | + ]; |
| 148 | + |
| 149 | + const openBlank = async addMode => { |
| 150 | + const user = renderHost(addMode, BLANK_SECOND); |
| 151 | + await user.click(editButton(2)); |
| 152 | + await waitFor(() => expect(openEditor()).toContainElement(document.activeElement)); |
| 153 | + return user; |
| 154 | + }; |
| 155 | + |
| 156 | + it('removes only it on a click on its delete button', async () => { |
| 157 | + const user = await openBlank(addMode); |
| 158 | + |
| 159 | + await user.click(button(deleteDistractorBtn$({ number: 2 }))); |
| 160 | + |
| 161 | + expect(openEditor()).toBeNull(); |
| 162 | + expect(chipTexts()).toEqual(['Antonio', 'Caliban', 'Miranda']); |
| 163 | + }); |
| 164 | + |
| 165 | + it('removes a later chip whose delete button is clicked', async () => { |
| 166 | + const user = await openBlank(addMode); |
| 167 | + |
| 168 | + await user.click(button(deleteDistractorBtn$({ number: 3 }))); |
| 169 | + |
| 170 | + expect(openEditor()).toBeNull(); |
| 171 | + expect(chipTexts()).toEqual(['Antonio', 'Miranda']); |
| 172 | + }); |
| 173 | + |
| 174 | + it('opens the chip that is clicked', async () => { |
| 175 | + const user = await openBlank(addMode); |
| 176 | + |
| 177 | + await user.click(editButton(3)); |
| 178 | + |
| 179 | + await waitFor(() => expect(openEditor()).toHaveTextContent('Caliban')); |
| 180 | + expect(chipTexts()).toHaveLength(3); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + it('throws a written draft away on a click on its discard button', async () => { |
| 185 | + const user = renderHost(addMode); |
| 186 | + await writeIn(user, button(addDistractorBtn$())); |
| 187 | + |
| 188 | + await user.click(button(deleteDistractorBtn$({ number: 3 }))); |
| 189 | + |
| 190 | + expect(openEditor()).toBeNull(); |
| 191 | + expect(chipTexts()).toEqual(['Antonio', 'Prospero']); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + it('adds a region-mode draft on save without opening another', async () => { |
| 196 | + const user = renderHost('region'); |
| 197 | + await writeIn(user, button(addDistractorBtn$())); |
| 198 | + |
| 199 | + await user.click(button(saveChipBtn$())); |
| 200 | + |
| 201 | + await waitFor(() => expect(button(addDistractorBtn$())).toHaveFocus()); |
| 202 | + expect(openEditor()).toBeNull(); |
| 203 | + expect(chipTexts()).toEqual(['Antonio', 'Prospero', '!']); |
| 204 | + }); |
| 205 | +}); |
0 commit comments