|
| 1 | +import { mount } from '@vue/test-utils' |
| 2 | +import NcRichContenteditable from '../../../../src/components/NcRichContenteditable/NcRichContenteditable.vue' |
| 3 | +import Tribute from 'tributejs/dist/tribute.esm.js' |
| 4 | + |
| 5 | +// FIXME: find a way to use Tribute in JSDOM or test with e2e |
| 6 | +jest.mock('tributejs/dist/tribute.esm.js') |
| 7 | +Tribute.mockImplementation(() => ({ |
| 8 | + attach: jest.fn(), |
| 9 | + detach: jest.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +/** |
| 13 | + * Mount NcRichContentEditable |
| 14 | + * |
| 15 | + * @param {object} options mount options |
| 16 | + * @param {object} options.propsData mount options.propsData |
| 17 | + * @param {object} options.listeners mount options.listeners |
| 18 | + * @param {object} options.attrs mount options.attrs |
| 19 | + * @return {object} |
| 20 | + */ |
| 21 | +function mountNcRichContenteditable({ propsData, listeners, attrs } = {}) { |
| 22 | + let currentValue = propsData?.value |
| 23 | + |
| 24 | + const wrapper = mount(NcRichContenteditable, { |
| 25 | + propsData: { |
| 26 | + value: currentValue, |
| 27 | + ...propsData, |
| 28 | + }, |
| 29 | + listeners: { |
| 30 | + 'update:value': ($event) => { |
| 31 | + currentValue = $event |
| 32 | + wrapper.setProps({ value: $event }) |
| 33 | + }, |
| 34 | + ...listeners, |
| 35 | + }, |
| 36 | + attrs: { |
| 37 | + ...attrs, |
| 38 | + }, |
| 39 | + attachTo: document.body, |
| 40 | + }) |
| 41 | + |
| 42 | + const getCurrentValue = () => currentValue |
| 43 | + |
| 44 | + const inputValue = async (newValue) => { |
| 45 | + wrapper.element.innerHTML += newValue |
| 46 | + await wrapper.trigger('input') |
| 47 | + } |
| 48 | + |
| 49 | + return { |
| 50 | + wrapper, |
| 51 | + getCurrentValue, |
| 52 | + inputValue, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +describe('NcRichContenteditable', () => { |
| 57 | + it('should update value during input', async () => { |
| 58 | + const { wrapper, inputValue } = mountNcRichContenteditable() |
| 59 | + const TEST_TEXT = 'Test Text' |
| 60 | + await inputValue('Test Text') |
| 61 | + expect(wrapper.emitted('update:value')).toBeDefined() |
| 62 | + expect(wrapper.emitted('update:value').at(-1)[0]).toBe(TEST_TEXT) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should not emit "submit" during input', async () => { |
| 66 | + const { wrapper, inputValue } = mountNcRichContenteditable() |
| 67 | + await inputValue('Test Text') |
| 68 | + expect(wrapper.emitted('submit')).not.toBeDefined() |
| 69 | + }) |
| 70 | + |
| 71 | + it('should emit "paste" on past', async () => { |
| 72 | + const { wrapper } = mountNcRichContenteditable() |
| 73 | + await wrapper.trigger('paste', { clipboardData: { getData: () => 'PASTED_TEXT', files: [], items: {} } }) |
| 74 | + expect(wrapper.emitted('paste')).toBeDefined() |
| 75 | + expect(wrapper.emitted('paste')).toHaveLength(1) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should emit "submit" on Enter', async () => { |
| 79 | + const { wrapper, inputValue } = mountNcRichContenteditable() |
| 80 | + |
| 81 | + await inputValue('Test Text') |
| 82 | + |
| 83 | + await wrapper.trigger('keydown', { keyCode: 13 }) // Enter |
| 84 | + |
| 85 | + expect(wrapper.emitted('submit')).toBeDefined() |
| 86 | + expect(wrapper.emitted('submit')).toHaveLength(1) |
| 87 | + }) |
| 88 | + |
| 89 | + it('should not emit "submit" on Enter during composition session', async () => { |
| 90 | + const { wrapper, inputValue } = mountNcRichContenteditable() |
| 91 | + |
| 92 | + await wrapper.trigger('compositionstart') |
| 93 | + await inputValue('猫') |
| 94 | + await wrapper.trigger('keydown', { keyCode: 13 }) // Enter |
| 95 | + await wrapper.trigger('compositionend') |
| 96 | + await inputValue(' - means "Cat"') |
| 97 | + await wrapper.trigger('keydown', { keyCode: 13 }) // Enter |
| 98 | + |
| 99 | + expect(wrapper.emitted('submit')).toBeDefined() |
| 100 | + expect(wrapper.emitted('submit')).toHaveLength(1) |
| 101 | + }) |
| 102 | + |
| 103 | + it('should proxy component events listeners to native event handlers', async () => { |
| 104 | + const handlers = { |
| 105 | + focus: jest.fn(), |
| 106 | + paste: jest.fn(), |
| 107 | + blur: jest.fn(), |
| 108 | + } |
| 109 | + const { wrapper } = mountNcRichContenteditable({ |
| 110 | + listeners: handlers, |
| 111 | + }) |
| 112 | + |
| 113 | + await wrapper.trigger('focus') |
| 114 | + await wrapper.trigger('paste', { clipboardData: { getData: () => 'PASTED_TEXT', files: [], items: {} } }) |
| 115 | + await wrapper.trigger('blur') |
| 116 | + |
| 117 | + expect(handlers.focus).toHaveBeenCalledTimes(1) |
| 118 | + expect(handlers.paste).toHaveBeenCalledTimes(1) |
| 119 | + expect(handlers.blur).toHaveBeenCalledTimes(1) |
| 120 | + }) |
| 121 | +}) |
0 commit comments