|
| 1 | +// CkEditorField.spec.js |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from 'vitest' |
| 4 | +import { mount } from '@vue/test-utils' |
| 5 | +import CkEditorField from './CkEditorField.vue' |
| 6 | + |
| 7 | +vi.mock('ckeditor5', () => ({ |
| 8 | + ClassicEditor: {}, |
| 9 | + Essentials: {}, |
| 10 | + Paragraph: {}, |
| 11 | + Bold: {}, |
| 12 | + Italic: {}, |
| 13 | + Heading: {}, |
| 14 | + Link: {}, |
| 15 | + List: {}, |
| 16 | + BlockQuote: {}, |
| 17 | + Table: {}, |
| 18 | + TableToolbar: {}, |
| 19 | + HorizontalLine: {}, |
| 20 | + Image: {}, |
| 21 | + ImageToolbar: {}, |
| 22 | + ImageCaption: {}, |
| 23 | + ImageStyle: {}, |
| 24 | + ImageResize: {}, |
| 25 | + AutoImage: {}, |
| 26 | + PictureEditing: {}, |
| 27 | +})) |
| 28 | + |
| 29 | +const CkeditorStub = { |
| 30 | + name: 'ckeditor', |
| 31 | + props: [ |
| 32 | + 'modelValue', |
| 33 | + 'editor', |
| 34 | + 'config', |
| 35 | + 'id', |
| 36 | + ], |
| 37 | + emits: ['update:modelValue'], |
| 38 | + template: ` |
| 39 | + <div class="ckeditor-stub"> |
| 40 | + <button |
| 41 | + class="change-value" |
| 42 | + @click="$emit('update:modelValue', '<p>Updated</p>')" |
| 43 | + > |
| 44 | + Update |
| 45 | + </button> |
| 46 | + </div> |
| 47 | + `, |
| 48 | +} |
| 49 | + |
| 50 | +describe('CkEditorField', () => { |
| 51 | + const createWrapper = (props = {}) => |
| 52 | + mount(CkEditorField, { |
| 53 | + props: { |
| 54 | + ...props, |
| 55 | + }, |
| 56 | + global: { |
| 57 | + stubs: { |
| 58 | + ckeditor: CkeditorStub, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }) |
| 62 | + |
| 63 | + it('renders label when provided', () => { |
| 64 | + const wrapper = createWrapper({ |
| 65 | + label: 'Content', |
| 66 | + }) |
| 67 | + |
| 68 | + expect(wrapper.text()).toContain('Content') |
| 69 | + }) |
| 70 | + |
| 71 | + it('does not render label when not provided', () => { |
| 72 | + const wrapper = createWrapper() |
| 73 | + |
| 74 | + expect(wrapper.find('label').exists()) |
| 75 | + .toBe(false) |
| 76 | + }) |
| 77 | + |
| 78 | + it('passes modelValue to ckeditor', () => { |
| 79 | + const wrapper = createWrapper({ |
| 80 | + modelValue: '<p>Hello</p>', |
| 81 | + }) |
| 82 | + |
| 83 | + const editor = |
| 84 | + wrapper.findComponent(CkeditorStub) |
| 85 | + |
| 86 | + expect(editor.props('modelValue')) |
| 87 | + .toBe('<p>Hello</p>') |
| 88 | + }) |
| 89 | + |
| 90 | + it('emits update:modelValue', async () => { |
| 91 | + const wrapper = createWrapper() |
| 92 | + |
| 93 | + await wrapper |
| 94 | + .find('.change-value') |
| 95 | + .trigger('click') |
| 96 | + |
| 97 | + expect( |
| 98 | + wrapper.emitted('update:modelValue') |
| 99 | + ).toEqual([ |
| 100 | + ['<p>Updated</p>'], |
| 101 | + ]) |
| 102 | + }) |
| 103 | + |
| 104 | + it('uses provided id', () => { |
| 105 | + const wrapper = createWrapper({ |
| 106 | + id: 'content-editor', |
| 107 | + label: 'Content', |
| 108 | + }) |
| 109 | + |
| 110 | + const label = wrapper.find('label') |
| 111 | + |
| 112 | + expect(label.attributes('for')) |
| 113 | + .toBe('content-editor') |
| 114 | + }) |
| 115 | + |
| 116 | + it('generates an id when one is not provided', () => { |
| 117 | + const wrapper = createWrapper({ |
| 118 | + label: 'Content', |
| 119 | + }) |
| 120 | + |
| 121 | + const label = wrapper.find('label') |
| 122 | + |
| 123 | + expect( |
| 124 | + label.attributes('for') |
| 125 | + ).toMatch(/^ckeditor-/) |
| 126 | + }) |
| 127 | + |
| 128 | + it('passes id to ckeditor', () => { |
| 129 | + const wrapper = createWrapper({ |
| 130 | + id: 'editor-1', |
| 131 | + }) |
| 132 | + |
| 133 | + const editor = |
| 134 | + wrapper.findComponent(CkeditorStub) |
| 135 | + |
| 136 | + expect(editor.props('id')) |
| 137 | + .toBe('editor-1') |
| 138 | + }) |
| 139 | + |
| 140 | + it('passes editor instance to ckeditor', () => { |
| 141 | + const wrapper = createWrapper() |
| 142 | + |
| 143 | + const editor = |
| 144 | + wrapper.findComponent(CkeditorStub) |
| 145 | + |
| 146 | + expect(editor.props('editor')) |
| 147 | + .toBeDefined() |
| 148 | + }) |
| 149 | + |
| 150 | + it('passes editor config to ckeditor', () => { |
| 151 | + const wrapper = createWrapper() |
| 152 | + |
| 153 | + const editor = |
| 154 | + wrapper.findComponent(CkeditorStub) |
| 155 | + |
| 156 | + const config = editor.props('config') |
| 157 | + |
| 158 | + expect(config.licenseKey) |
| 159 | + .toBe('GPL') |
| 160 | + |
| 161 | + expect(config.toolbar) |
| 162 | + .toContain('bold') |
| 163 | + |
| 164 | + expect(config.toolbar) |
| 165 | + .toContain('italic') |
| 166 | + |
| 167 | + expect(config.toolbar) |
| 168 | + .toContain('insertTable') |
| 169 | + }) |
| 170 | + |
| 171 | + it('contains image toolbar configuration', () => { |
| 172 | + const wrapper = createWrapper() |
| 173 | + |
| 174 | + const config = |
| 175 | + wrapper.findComponent(CkeditorStub) |
| 176 | + .props('config') |
| 177 | + |
| 178 | + expect(config.image.toolbar) |
| 179 | + .toContain('toggleImageCaption') |
| 180 | + |
| 181 | + expect(config.image.toolbar) |
| 182 | + .toContain('imageTextAlternative') |
| 183 | + }) |
| 184 | +}) |
0 commit comments