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

247 lines
7.6 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
import { mount } from '@vue/test-utils';
import { TEST_HOST, FIXTURES_PATH } from 'spec/test_constants';
2020-01-01 13:55:28 +05:30
import AxiosMockAdapter from 'axios-mock-adapter';
import $ from 'jquery';
2021-01-03 14:25:43 +05:30
import MarkdownField from '~/vue_shared/components/markdown/field.vue';
2020-01-01 13:55:28 +05:30
import axios from '~/lib/utils/axios_utils';
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';
2020-01-01 13:55:28 +05:30
function assertMarkdownTabs(isWrite, writeLink, previewLink, wrapper) {
2020-05-24 23:13:21 +05:30
expect(writeLink.element.parentNode.classList.contains('active')).toBe(isWrite);
expect(previewLink.element.parentNode.classList.contains('active')).toBe(!isWrite);
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();
subject = null;
2020-01-01 13:55:28 +05:30
axiosMock.restore();
});
2021-01-03 14:25:43 +05:30
function createSubject() {
// 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.
subject = mount(
{
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,
},
},
);
}
const getPreviewLink = () => subject.find('.nav-links .js-preview-link');
const getWriteLink = () => subject.find('.nav-links .js-write-link');
const getMarkdownButton = () => subject.find('.js-md');
const getAllMarkdownButtons = () => subject.findAll('.js-md');
const getVideo = () => subject.find('video');
const getAttachButton = () => subject.find('.button-attach-file');
const clickAttachButton = () => getAttachButton().trigger('click');
const findDropzone = () => subject.find('.div-dropzone');
2020-01-01 13:55:28 +05:30
describe('mounted', () => {
2020-03-13 15:44:24 +05:30
const previewHTML = `
<p>markdown preview</p>
<video src="${FIXTURES_PATH}/static/mock-video.mp4" muted="muted"></video>
`;
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
});
describe('markdown preview', () => {
beforeEach(() => {
axiosMock.onPost(markdownPreviewPath).reply(200, { body: previewHTML });
});
it('sets preview link as active', () => {
2021-01-03 14:25:43 +05:30
previewLink = getPreviewLink();
2020-01-01 13:55:28 +05:30
previewLink.trigger('click');
2021-01-03 14:25:43 +05:30
return subject.vm.$nextTick().then(() => {
2020-01-01 13:55:28 +05:30
expect(previewLink.element.parentNode.classList.contains('active')).toBeTruthy();
});
});
it('shows preview loading text', () => {
2021-01-03 14:25:43 +05:30
previewLink = getPreviewLink();
2020-01-01 13:55:28 +05:30
previewLink.trigger('click');
2021-01-03 14:25:43 +05:30
return subject.vm.$nextTick(() => {
expect(subject.find('.md-preview-holder').element.textContent.trim()).toContain(
2020-01-01 13:55:28 +05:30
'Loading…',
);
});
});
2020-05-24 23:13:21 +05:30
it('renders markdown preview and GFM', () => {
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
previewLink.trigger('click');
2020-03-13 15:44:24 +05:30
return axios.waitFor(markdownPreviewPath).then(() => {
2021-01-03 14:25:43 +05:30
expect(subject.find('.md-preview-holder').element.innerHTML).toContain(previewHTML);
2020-05-24 23:13:21 +05:30
expect(renderGFMSpy).toHaveBeenCalled();
2020-03-13 15:44:24 +05:30
});
});
it('calls video.pause() on comment input when isSubmitting is changed to true', () => {
2021-01-03 14:25:43 +05:30
previewLink = getPreviewLink();
2020-03-13 15:44:24 +05:30
previewLink.trigger('click');
let callPause;
return axios
.waitFor(markdownPreviewPath)
.then(() => {
2021-01-03 14:25:43 +05:30
const video = getVideo();
2020-03-13 15:44:24 +05:30
callPause = jest.spyOn(video.element, 'pause').mockImplementation(() => true);
2021-01-03 14:25:43 +05:30
subject.setProps({ isSubmitting: true });
2020-03-13 15:44:24 +05:30
2021-01-03 14:25:43 +05:30
return subject.vm.$nextTick();
2020-03-13 15:44:24 +05:30
})
.then(() => {
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();
writeLink.trigger('click');
await subject.vm.$nextTick();
2020-01-01 13:55:28 +05:30
2021-01-03 14:25:43 +05:30
assertMarkdownTabs(true, writeLink, previewLink, subject);
2020-01-01 13:55:28 +05:30
writeLink.trigger('click');
2021-01-03 14:25:43 +05:30
await subject.vm.$nextTick();
assertMarkdownTabs(true, writeLink, previewLink, subject);
previewLink.trigger('click');
await subject.vm.$nextTick();
assertMarkdownTabs(false, writeLink, previewLink, subject);
previewLink.trigger('click');
await subject.vm.$nextTick();
assertMarkdownTabs(false, writeLink, previewLink, subject);
2020-01-01 13:55:28 +05:30
});
});
describe('markdown buttons', () => {
it('converts single words', () => {
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');
2021-01-03 14:25:43 +05:30
return subject.vm.$nextTick(() => {
2020-01-01 13:55:28 +05:30
expect(textarea.value).toContain('**testing**');
});
});
it('converts a line', () => {
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);
2021-01-03 14:25:43 +05:30
const markdownButton = getAllMarkdownButtons().wrappers[5];
2020-01-01 13:55:28 +05:30
markdownButton.trigger('click');
2021-01-03 14:25:43 +05:30
return subject.vm.$nextTick(() => {
2020-06-23 00:09:42 +05:30
expect(textarea.value).toContain('- testing');
2020-01-01 13:55:28 +05:30
});
});
it('converts multiple lines', () => {
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);
2021-01-03 14:25:43 +05:30
const markdownButton = getAllMarkdownButtons().wrappers[5];
2020-01-01 13:55:28 +05:30
markdownButton.trigger('click');
2021-01-03 14:25:43 +05:30
return subject.vm.$nextTick(() => {
2020-06-23 00:09:42 +05:30
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' });
await subject.vm.$nextTick();
});
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();
});
});
2020-01-01 13:55:28 +05:30
});
});