2022-08-29 09:10:06 +00:00
|
|
|
import { GlButton, GlFormInputGroup, GlDropdown } from '@gitlab/ui';
|
2021-07-14 15:09:57 +00:00
|
|
|
import { mountExtended } from 'helpers/vue_test_utils_helper';
|
|
|
|
import ToolbarImageButton from '~/content_editor/components/toolbar_image_button.vue';
|
2021-08-10 15:10:03 +00:00
|
|
|
import Attachment from '~/content_editor/extensions/attachment';
|
2021-07-27 21:09:42 +00:00
|
|
|
import Image from '~/content_editor/extensions/image';
|
2021-07-14 15:09:57 +00:00
|
|
|
import { createTestEditor, mockChainedCommands } from '../test_utils';
|
|
|
|
|
|
|
|
describe('content_editor/components/toolbar_image_button', () => {
|
|
|
|
let wrapper;
|
|
|
|
let editor;
|
|
|
|
|
|
|
|
const buildWrapper = () => {
|
|
|
|
wrapper = mountExtended(ToolbarImageButton, {
|
2021-07-29 09:08:46 +00:00
|
|
|
provide: {
|
2021-07-14 15:09:57 +00:00
|
|
|
tiptapEditor: editor,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const findImageURLInput = () =>
|
|
|
|
wrapper.findComponent(GlFormInputGroup).find('input[type="text"]');
|
|
|
|
const findApplyImageButton = () => wrapper.findComponent(GlButton);
|
2022-08-29 09:10:06 +00:00
|
|
|
const findDropdown = () => wrapper.findComponent(GlDropdown);
|
2021-07-14 15:09:57 +00:00
|
|
|
|
|
|
|
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(() => {
|
|
|
|
editor = createTestEditor({
|
2021-07-27 21:09:42 +00:00
|
|
|
extensions: [
|
2021-08-10 15:10:03 +00:00
|
|
|
Image,
|
|
|
|
Attachment.configure({
|
2021-07-27 21:09:42 +00:00
|
|
|
renderMarkdown: jest.fn(),
|
|
|
|
uploadsPath: '/uploads/',
|
|
|
|
}),
|
|
|
|
],
|
2021-07-14 15:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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 () => {
|
2021-08-10 15:10:03 +00:00
|
|
|
const commands = mockChainedCommands(editor, ['focus', 'uploadAttachment', 'run']);
|
2021-07-14 15:09:57 +00:00
|
|
|
const file = new File(['foo'], 'foo.png', { type: 'image/png' });
|
|
|
|
|
|
|
|
await selectFile(file);
|
|
|
|
|
|
|
|
expect(commands.focus).toHaveBeenCalled();
|
2021-08-10 15:10:03 +00:00
|
|
|
expect(commands.uploadAttachment).toHaveBeenCalledWith({ file });
|
2021-07-14 15:09:57 +00:00
|
|
|
expect(commands.run).toHaveBeenCalled();
|
|
|
|
|
|
|
|
expect(wrapper.emitted().execute[0]).toEqual([{ contentType: 'image', value: 'upload' }]);
|
|
|
|
});
|
2022-08-29 09:10:06 +00:00
|
|
|
|
|
|
|
describe('a11y tests', () => {
|
|
|
|
it('sets text, title, and text-sr-only properties to the table button dropdown', () => {
|
|
|
|
buildWrapper();
|
|
|
|
|
|
|
|
expect(findDropdown().props()).toMatchObject({
|
|
|
|
text: 'Insert image',
|
|
|
|
textSrOnly: true,
|
|
|
|
});
|
|
|
|
expect(findDropdown().attributes('title')).toBe('Insert image');
|
|
|
|
});
|
|
|
|
});
|
2021-07-14 15:09:57 +00:00
|
|
|
});
|