debian-mirror-gitlab/spec/frontend/vue_shared/components/markdown/field_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

349 lines
11 KiB
JavaScript
Raw Normal View History

2022-03-02 08:16:31 +05:30
import { nextTick } from 'vue';
2020-01-01 13:55:28 +05:30
import AxiosMockAdapter from 'axios-mock-adapter';
import $ from 'jquery';
2021-03-11 19:13:27 +05:30
import { TEST_HOST, FIXTURES_PATH } from 'spec/test_constants';
2020-01-01 13:55:28 +05:30
import axios from '~/lib/utils/axios_utils';
2021-03-11 19:13:27 +05:30
import MarkdownField from '~/vue_shared/components/markdown/field.vue';
2022-05-07 20:08:51 +05:30
import MarkdownFieldHeader from '~/vue_shared/components/markdown/header.vue';
2022-07-16 23:28:13 +05:30
import MarkdownToolbar from '~/vue_shared/components/markdown/toolbar.vue';
2022-04-04 11:22:00 +05:30
import { mountExtended } from 'helpers/vue_test_utils_helper';
2020-01-01 13:55:28 +05:30
const markdownPreviewPath = `${TEST_HOST}/preview`;
const markdownDocsPath = `${TEST_HOST}/docs`;
2021-01-03 14:25:43 +05:30
const textareaValue = 'testing\n123';
const uploadsPath = 'test/uploads';
2022-07-16 23:28:13 +05:30
const restrictedToolBarItems = ['quote'];
2020-01-01 13:55:28 +05:30
function assertMarkdownTabs(isWrite, writeLink, previewLink, wrapper) {
2022-04-04 11:22:00 +05:30
expect(writeLink.element.children[0].classList.contains('active')).toBe(isWrite);
expect(previewLink.element.children[0].classList.contains('active')).toBe(!isWrite);
2020-05-24 23:13:21 +05:30
expect(wrapper.find('.md-preview-holder').element.style.display).toBe(isWrite ? 'none' : '');
2020-01-01 13:55:28 +05:30
}
describe('Markdown field component', () => {
let axiosMock;
2021-01-03 14:25:43 +05:30
let subject;
2020-01-01 13:55:28 +05:30
beforeEach(() => {
axiosMock = new AxiosMockAdapter(axios);
2021-01-03 14:25:43 +05:30
// window.uploads_path is needed for dropzone to initialize
window.uploads_path = uploadsPath;
2020-01-01 13:55:28 +05:30
});
afterEach(() => {
2021-01-03 14:25:43 +05:30
subject.destroy();
2020-01-01 13:55:28 +05:30
axiosMock.restore();
});
2022-05-07 20:08:51 +05:30
function createSubject({ lines = [], enablePreview = true } = {}) {
2021-01-03 14:25:43 +05:30
// We actually mount a wrapper component so that we can force Vue to rerender classes in order to test a regression
// caused by mixing Vanilla JS and Vue.
2022-04-04 11:22:00 +05:30
subject = mountExtended(
2021-01-03 14:25:43 +05:30
{
components: {
MarkdownField,
},
props: {
wrapperClasses: {
type: String,
required: false,
default: '',
},
},
template: `
<markdown-field :class="wrapperClasses" v-bind="$attrs">
<template #textarea>
<textarea class="js-gfm-input" :value="$attrs.textareaValue"></textarea>
</template>
</markdown-field>`,
},
{
propsData: {
markdownDocsPath,
markdownPreviewPath,
isSubmitting: false,
textareaValue,
2021-11-11 11:23:49 +05:30
lines,
2022-05-07 20:08:51 +05:30
enablePreview,
2022-07-16 23:28:13 +05:30
restrictedToolBarItems,
2021-01-03 14:25:43 +05:30
},
},
);
}
2022-04-04 11:22:00 +05:30
const getPreviewLink = () => subject.findByTestId('preview-tab');
const getWriteLink = () => subject.findByTestId('write-tab');
2021-01-03 14:25:43 +05:30
const getMarkdownButton = () => subject.find('.js-md');
2022-05-07 20:08:51 +05:30
const getListBulletedButton = () => subject.findAll('.js-md[title="Add a bullet list"]');
2021-01-03 14:25:43 +05:30
const getVideo = () => subject.find('video');
const getAttachButton = () => subject.find('.button-attach-file');
const clickAttachButton = () => getAttachButton().trigger('click');
const findDropzone = () => subject.find('.div-dropzone');
2022-07-16 23:28:13 +05:30
const findMarkdownHeader = () => subject.findComponent(MarkdownFieldHeader);
const findMarkdownToolbar = () => subject.findComponent(MarkdownToolbar);
2021-01-03 14:25:43 +05:30
2020-01-01 13:55:28 +05:30
describe('mounted', () => {
2020-03-13 15:44:24 +05:30
const previewHTML = `
<p>markdown preview</p>
2022-06-21 17:19:12 +05:30
<video src="${FIXTURES_PATH}/static/mock-video.mp4"></video>
2020-03-13 15:44:24 +05:30
`;
2020-01-01 13:55:28 +05:30
let previewLink;
let writeLink;
2021-01-03 14:25:43 +05:30
let dropzoneSpy;
2020-01-01 13:55:28 +05:30
2021-01-03 14:25:43 +05:30
beforeEach(() => {
dropzoneSpy = jest.fn();
createSubject();
findDropzone().element.addEventListener('click', dropzoneSpy);
2020-05-24 23:13:21 +05:30
});
2020-01-01 13:55:28 +05:30
it('renders textarea inside backdrop', () => {
2021-01-03 14:25:43 +05:30
expect(subject.find('.zen-backdrop textarea').element).not.toBeNull();
2020-01-01 13:55:28 +05:30
});
2022-06-21 17:19:12 +05:30
it('renders referenced commands on markdown preview', async () => {
axiosMock
.onPost(markdownPreviewPath)
.reply(200, { references: { users: [], commands: 'test command' } });
previewLink = getPreviewLink();
previewLink.vm.$emit('click', { target: {} });
await axios.waitFor(markdownPreviewPath);
const referencedCommands = subject.find('[data-testid="referenced-commands"]');
expect(referencedCommands.exists()).toBe(true);
expect(referencedCommands.text()).toContain('test command');
});
2020-01-01 13:55:28 +05:30
describe('markdown preview', () => {
beforeEach(() => {
axiosMock.onPost(markdownPreviewPath).reply(200, { body: previewHTML });
});
2022-04-04 11:22:00 +05:30
it('sets preview link as active', async () => {
2021-01-03 14:25:43 +05:30
previewLink = getPreviewLink();
2022-04-04 11:22:00 +05:30
previewLink.vm.$emit('click', { target: {} });
2020-01-01 13:55:28 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
expect(previewLink.element.children[0].classList.contains('active')).toBe(true);
2020-01-01 13:55:28 +05:30
});
2022-04-04 11:22:00 +05:30
it('shows preview loading text', async () => {
2021-01-03 14:25:43 +05:30
previewLink = getPreviewLink();
2022-04-04 11:22:00 +05:30
previewLink.vm.$emit('click', { target: {} });
2020-01-01 13:55:28 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
expect(subject.find('.md-preview-holder').element.textContent.trim()).toContain('Loading…');
2020-01-01 13:55:28 +05:30
});
2022-04-04 11:22:00 +05:30
it('renders markdown preview and GFM', async () => {
2020-05-24 23:13:21 +05:30
const renderGFMSpy = jest.spyOn($.fn, 'renderGFM');
2020-01-01 13:55:28 +05:30
2021-01-03 14:25:43 +05:30
previewLink = getPreviewLink();
2020-01-01 13:55:28 +05:30
2022-04-04 11:22:00 +05:30
previewLink.vm.$emit('click', { target: {} });
2020-01-01 13:55:28 +05:30
2022-04-04 11:22:00 +05:30
await axios.waitFor(markdownPreviewPath);
expect(subject.find('.md-preview-holder').element.innerHTML).toContain(previewHTML);
expect(renderGFMSpy).toHaveBeenCalled();
2020-03-13 15:44:24 +05:30
});
2022-04-04 11:22:00 +05:30
it('calls video.pause() on comment input when isSubmitting is changed to true', async () => {
2021-01-03 14:25:43 +05:30
previewLink = getPreviewLink();
2022-04-04 11:22:00 +05:30
previewLink.vm.$emit('click', { target: {} });
2020-03-13 15:44:24 +05:30
2022-04-04 11:22:00 +05:30
await axios.waitFor(markdownPreviewPath);
const video = getVideo();
const callPause = jest.spyOn(video.element, 'pause').mockImplementation(() => true);
2020-03-13 15:44:24 +05:30
2022-04-04 11:22:00 +05:30
subject.setProps({ isSubmitting: true });
2020-03-13 15:44:24 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
expect(callPause).toHaveBeenCalled();
2020-01-01 13:55:28 +05:30
});
2021-01-03 14:25:43 +05:30
it('clicking already active write or preview link does nothing', async () => {
writeLink = getWriteLink();
previewLink = getPreviewLink();
2022-04-04 11:22:00 +05:30
writeLink.vm.$emit('click', { target: {} });
await nextTick();
2020-01-01 13:55:28 +05:30
2021-01-03 14:25:43 +05:30
assertMarkdownTabs(true, writeLink, previewLink, subject);
2022-04-04 11:22:00 +05:30
writeLink.vm.$emit('click', { target: {} });
await nextTick();
2021-01-03 14:25:43 +05:30
assertMarkdownTabs(true, writeLink, previewLink, subject);
2022-04-04 11:22:00 +05:30
previewLink.vm.$emit('click', { target: {} });
await nextTick();
2021-01-03 14:25:43 +05:30
assertMarkdownTabs(false, writeLink, previewLink, subject);
2022-04-04 11:22:00 +05:30
previewLink.vm.$emit('click', { target: {} });
await nextTick();
2021-01-03 14:25:43 +05:30
assertMarkdownTabs(false, writeLink, previewLink, subject);
2020-01-01 13:55:28 +05:30
});
2022-07-16 23:28:13 +05:30
it('passes correct props to MarkdownToolbar', () => {
expect(findMarkdownToolbar().props()).toEqual({
canAttachFile: true,
markdownDocsPath,
quickActionsDocsPath: '',
showCommentToolBar: true,
});
});
2020-01-01 13:55:28 +05:30
});
describe('markdown buttons', () => {
2022-07-16 23:28:13 +05:30
beforeEach(() => {
// needed for the underlying insertText to work
document.execCommand = jest.fn(() => false);
});
2022-04-04 11:22:00 +05:30
it('converts single words', async () => {
2021-01-03 14:25:43 +05:30
const textarea = subject.find('textarea').element;
2020-01-01 13:55:28 +05:30
textarea.setSelectionRange(0, 7);
2021-01-03 14:25:43 +05:30
const markdownButton = getMarkdownButton();
2020-01-01 13:55:28 +05:30
markdownButton.trigger('click');
2022-04-04 11:22:00 +05:30
await nextTick();
expect(textarea.value).toContain('**testing**');
2020-01-01 13:55:28 +05:30
});
2022-04-04 11:22:00 +05:30
it('converts a line', async () => {
2021-01-03 14:25:43 +05:30
const textarea = subject.find('textarea').element;
2020-01-01 13:55:28 +05:30
textarea.setSelectionRange(0, 0);
2022-05-07 20:08:51 +05:30
const markdownButton = getListBulletedButton();
2020-01-01 13:55:28 +05:30
markdownButton.trigger('click');
2022-04-04 11:22:00 +05:30
await nextTick();
expect(textarea.value).toContain('- testing');
2020-01-01 13:55:28 +05:30
});
2022-04-04 11:22:00 +05:30
it('converts multiple lines', async () => {
2021-01-03 14:25:43 +05:30
const textarea = subject.find('textarea').element;
2020-01-01 13:55:28 +05:30
textarea.setSelectionRange(0, 50);
2022-05-07 20:08:51 +05:30
const markdownButton = getListBulletedButton();
2020-01-01 13:55:28 +05:30
markdownButton.trigger('click');
2022-04-04 11:22:00 +05:30
await nextTick();
expect(textarea.value).toContain('- testing\n- 123');
2020-01-01 13:55:28 +05:30
});
});
2021-01-03 14:25:43 +05:30
it('should render attach a file button', () => {
expect(getAttachButton().text()).toBe('Attach a file');
});
it('should trigger dropzone when attach button is clicked', () => {
expect(dropzoneSpy).not.toHaveBeenCalled();
clickAttachButton();
expect(dropzoneSpy).toHaveBeenCalled();
});
describe('when textarea has changed', () => {
beforeEach(async () => {
// Do something to trigger rerendering the class
subject.setProps({ wrapperClasses: 'foo' });
2022-04-04 11:22:00 +05:30
await nextTick();
2021-01-03 14:25:43 +05:30
});
it('should have rerendered classes and kept gfm-form', () => {
expect(subject.classes()).toEqual(expect.arrayContaining(['gfm-form', 'foo']));
});
it('should trigger dropzone when attach button is clicked', () => {
expect(dropzoneSpy).not.toHaveBeenCalled();
clickAttachButton();
expect(dropzoneSpy).toHaveBeenCalled();
});
2022-03-02 08:16:31 +05:30
describe('mentioning all users', () => {
const users = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map((i) => `user_${i}`);
it('shows warning on mention of all users', async () => {
axiosMock.onPost(markdownPreviewPath).reply(200, { references: { users } });
subject.setProps({ textareaValue: 'hello @all' });
await axios.waitFor(markdownPreviewPath).then(() => {
expect(subject.text()).toContain(
'You are about to add 11 people to the discussion. They will all receive a notification.',
);
});
});
it('removes warning when all mention is removed', async () => {
axiosMock.onPost(markdownPreviewPath).reply(200, { references: { users } });
subject.setProps({ textareaValue: 'hello @all' });
await axios.waitFor(markdownPreviewPath);
jest.spyOn(axios, 'post');
subject.setProps({ textareaValue: 'hello @allan' });
await nextTick();
expect(axios.post).not.toHaveBeenCalled();
expect(subject.text()).not.toContain(
'You are about to add 11 people to the discussion. They will all receive a notification.',
);
});
2022-05-07 20:08:51 +05:30
it('removes warning when all mention is removed while endpoint is loading', async () => {
axiosMock.onPost(markdownPreviewPath).reply(200, { references: { users } });
jest.spyOn(axios, 'post');
subject.setProps({ textareaValue: 'hello @all' });
await nextTick();
subject.setProps({ textareaValue: 'hello @allan' });
await axios.waitFor(markdownPreviewPath);
expect(axios.post).toHaveBeenCalled();
expect(subject.text()).not.toContain(
'You are about to add 11 people to the discussion. They will all receive a notification.',
);
});
2022-03-02 08:16:31 +05:30
});
2021-01-03 14:25:43 +05:30
});
2020-01-01 13:55:28 +05:30
});
2021-11-11 11:23:49 +05:30
describe('suggestions', () => {
it('escapes new line characters', () => {
2022-05-07 20:08:51 +05:30
createSubject({ lines: [{ rich_text: 'hello world\\n' }] });
2021-11-11 11:23:49 +05:30
2022-07-16 23:28:13 +05:30
expect(findMarkdownHeader().props('lineContent')).toBe('hello world%br');
2021-11-11 11:23:49 +05:30
});
});
2022-05-07 20:08:51 +05:30
it('allows enabling and disabling Markdown Preview', () => {
createSubject({ enablePreview: false });
expect(subject.findComponent(MarkdownFieldHeader).props('enablePreview')).toBe(false);
subject.destroy();
createSubject({ enablePreview: true });
expect(subject.findComponent(MarkdownFieldHeader).props('enablePreview')).toBe(true);
});
2022-07-16 23:28:13 +05:30
it('passess restricted tool bar items', () => {
createSubject();
expect(subject.findComponent(MarkdownFieldHeader).props('restrictedToolBarItems')).toBe(
restrictedToolBarItems,
);
});
2020-01-01 13:55:28 +05:30
});