Commit 463f51d6 authored by Himanshu Kapoor's avatar Himanshu Kapoor

Add tests for the insert image toolbar button

parent 359961ca
......@@ -36,25 +36,40 @@ export default {
};
},
methods: {
cleanFields() {
resetFields() {
this.imgSrc = '';
this.$refs.fileUpload.value = '';
this.$refs.fileSelector.value = '';
},
insertImage() {
const alt = getImageAlt(this.imgSrc);
this.tiptapEditor.chain().focus().setImage({ src: this.imgSrc, alt }).run();
this.cleanFields();
this.tiptapEditor
.chain()
.focus()
.setImage({
src: this.imgSrc,
canonicalSrc: this.imgSrc,
alt: getImageAlt(this.imgSrc),
})
.run();
this.resetFields();
this.emitExecute();
},
emitExecute(source = 'url') {
this.$emit('execute', { contentType: 'image', value: source });
},
openFileUpload() {
this.$refs.fileUpload.click();
this.$refs.fileSelector.click();
},
onUploadChange(e) {
this.tiptapEditor.chain().focus().uploadImage({ file: e.target.files[0] }).run();
this.cleanFields();
onFileSelect(e) {
this.tiptapEditor
.chain()
.focus()
.uploadImage({
file: e.target.files[0],
})
.run();
this.resetFields();
this.emitExecute('upload');
},
},
......@@ -69,7 +84,7 @@ export default {
size="small"
category="tertiary"
icon="media"
@hidden="cleanFields()"
@hidden="resetFields()"
>
<gl-dropdown-form class="gl-px-3!">
<gl-form-input-group v-model="imgSrc" :placeholder="__('Image URL')">
......@@ -84,12 +99,12 @@ export default {
</gl-dropdown-item>
<input
ref="fileUpload"
ref="fileSelector"
type="file"
name="content_editor_image"
:accept="$options.acceptedMimes"
class="gl-display-none"
@change="onUploadChange"
@change="onFileSelect"
/>
</gl-dropdown>
</template>
import { GlButton, GlFormInputGroup } from '@gitlab/ui';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import ToolbarImageButton from '~/content_editor/components/toolbar_image_button.vue';
import { configure as configureImageExtension } from '~/content_editor/extensions/image';
import { createTestEditor, mockChainedCommands } from '../test_utils';
describe('content_editor/components/toolbar_image_button', () => {
let wrapper;
let editor;
const buildWrapper = () => {
wrapper = mountExtended(ToolbarImageButton, {
propsData: {
tiptapEditor: editor,
},
});
};
const findImageURLInput = () =>
wrapper.findComponent(GlFormInputGroup).find('input[type="text"]');
const findApplyImageButton = () => wrapper.findComponent(GlButton);
const selectFile = async (file) => {
const input = wrapper.find({ ref: 'fileSelector' });
// override the property definition because `input.files` isn't directly modifyable
Object.defineProperty(input.element, 'files', { value: [file], writable: true });
await input.trigger('change');
};
beforeEach(() => {
const { tiptapExtension: Image } = configureImageExtension({
renderMarkdown: jest.fn(),
uploadsPath: '/uploads/',
});
editor = createTestEditor({
extensions: [Image],
});
buildWrapper();
});
afterEach(() => {
editor.destroy();
wrapper.destroy();
});
it('sets the image to the value in the URL input when "Insert" button is clicked', async () => {
const commands = mockChainedCommands(editor, ['focus', 'setImage', 'run']);
await findImageURLInput().setValue('https://example.com/img.jpg');
await findApplyImageButton().trigger('click');
expect(commands.focus).toHaveBeenCalled();
expect(commands.setImage).toHaveBeenCalledWith({
alt: 'img',
src: 'https://example.com/img.jpg',
canonicalSrc: 'https://example.com/img.jpg',
});
expect(commands.run).toHaveBeenCalled();
expect(wrapper.emitted().execute[0]).toEqual([{ contentType: 'image', value: 'url' }]);
});
it('uploads the selected image when file input changes', async () => {
const commands = mockChainedCommands(editor, ['focus', 'uploadImage', 'run']);
const file = new File(['foo'], 'foo.png', { type: 'image/png' });
await selectFile(file);
expect(commands.focus).toHaveBeenCalled();
expect(commands.uploadImage).toHaveBeenCalledWith({ file });
expect(commands.run).toHaveBeenCalled();
expect(wrapper.emitted().execute[0]).toEqual([{ contentType: 'image', value: 'upload' }]);
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment