2020-03-13 15:44:24 +05:30
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import fieldComponent from '~/vue_shared/components/markdown/field.vue';
|
|
|
|
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';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
|
|
|
|
const markdownPreviewPath = `${TEST_HOST}/preview`;
|
|
|
|
const markdownDocsPath = `${TEST_HOST}/docs`;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function createComponent() {
|
|
|
|
const wrapper = mount(fieldComponent, {
|
|
|
|
propsData: {
|
|
|
|
markdownDocsPath,
|
|
|
|
markdownPreviewPath,
|
2020-03-13 15:44:24 +05:30
|
|
|
isSubmitting: false,
|
2020-01-01 13:55:28 +05:30
|
|
|
},
|
|
|
|
slots: {
|
|
|
|
textarea: '<textarea>testing\n123</textarea>',
|
|
|
|
},
|
|
|
|
template: `
|
|
|
|
<field-component
|
|
|
|
markdown-preview-path="${markdownPreviewPath}"
|
|
|
|
markdown-docs-path="${markdownDocsPath}"
|
2020-03-13 15:44:24 +05:30
|
|
|
:isSubmitting="false"
|
2020-01-01 13:55:28 +05:30
|
|
|
>
|
|
|
|
<textarea
|
|
|
|
slot="textarea"
|
|
|
|
v-model="text">
|
|
|
|
<slot>this is a test</slot>
|
|
|
|
</textarea>
|
|
|
|
</field-component>
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
const getPreviewLink = wrapper => wrapper.find('.nav-links .js-preview-link');
|
|
|
|
const getWriteLink = wrapper => wrapper.find('.nav-links .js-write-link');
|
|
|
|
const getMarkdownButton = wrapper => wrapper.find('.js-md');
|
|
|
|
const getAllMarkdownButtons = wrapper => wrapper.findAll('.js-md');
|
2020-03-13 15:44:24 +05:30
|
|
|
const getVideo = wrapper => wrapper.find('video');
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
describe('Markdown field component', () => {
|
|
|
|
let axiosMock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
axiosMock = new AxiosMockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
axiosMock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('mounted', () => {
|
|
|
|
let wrapper;
|
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;
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
it('renders textarea inside backdrop', () => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
expect(wrapper.find('.zen-backdrop textarea').element).not.toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('markdown preview', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
axiosMock.onPost(markdownPreviewPath).reply(200, { body: previewHTML });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets preview link as active', () => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
previewLink = getPreviewLink(wrapper);
|
|
|
|
previewLink.trigger('click');
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
return wrapper.vm.$nextTick().then(() => {
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(previewLink.element.parentNode.classList.contains('active')).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows preview loading text', () => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
previewLink = getPreviewLink(wrapper);
|
|
|
|
previewLink.trigger('click');
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
return wrapper.vm.$nextTick(() => {
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(wrapper.find('.md-preview-holder').element.textContent.trim()).toContain(
|
|
|
|
'Loading…',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it('renders markdown preview and GFM', () => {
|
2020-01-01 13:55:28 +05:30
|
|
|
wrapper = createComponent();
|
2020-05-24 23:13:21 +05:30
|
|
|
const renderGFMSpy = jest.spyOn($.fn, 'renderGFM');
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
previewLink = getPreviewLink(wrapper);
|
|
|
|
|
|
|
|
previewLink.trigger('click');
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
return axios.waitFor(markdownPreviewPath).then(() => {
|
|
|
|
expect(wrapper.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', () => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
previewLink = getPreviewLink(wrapper);
|
|
|
|
previewLink.trigger('click');
|
|
|
|
|
|
|
|
let callPause;
|
|
|
|
|
|
|
|
return axios
|
|
|
|
.waitFor(markdownPreviewPath)
|
|
|
|
.then(() => {
|
|
|
|
const video = getVideo(wrapper);
|
|
|
|
callPause = jest.spyOn(video.element, 'pause').mockImplementation(() => true);
|
|
|
|
|
|
|
|
wrapper.setProps({
|
|
|
|
isSubmitting: true,
|
|
|
|
markdownPreviewPath,
|
|
|
|
markdownDocsPath,
|
|
|
|
});
|
|
|
|
|
|
|
|
return wrapper.vm.$nextTick();
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
expect(callPause).toHaveBeenCalled();
|
|
|
|
});
|
2020-01-01 13:55:28 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('clicking already active write or preview link does nothing', () => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
writeLink = getWriteLink(wrapper);
|
|
|
|
previewLink = getPreviewLink(wrapper);
|
|
|
|
|
|
|
|
writeLink.trigger('click');
|
2020-03-13 15:44:24 +05:30
|
|
|
return wrapper.vm
|
|
|
|
.$nextTick()
|
2020-01-01 13:55:28 +05:30
|
|
|
.then(() => assertMarkdownTabs(true, writeLink, previewLink, wrapper))
|
|
|
|
.then(() => writeLink.trigger('click'))
|
2020-03-13 15:44:24 +05:30
|
|
|
.then(() => wrapper.vm.$nextTick())
|
2020-01-01 13:55:28 +05:30
|
|
|
.then(() => assertMarkdownTabs(true, writeLink, previewLink, wrapper))
|
|
|
|
.then(() => previewLink.trigger('click'))
|
2020-03-13 15:44:24 +05:30
|
|
|
.then(() => wrapper.vm.$nextTick())
|
2020-01-01 13:55:28 +05:30
|
|
|
.then(() => assertMarkdownTabs(false, writeLink, previewLink, wrapper))
|
|
|
|
.then(() => previewLink.trigger('click'))
|
2020-03-13 15:44:24 +05:30
|
|
|
.then(() => wrapper.vm.$nextTick())
|
2020-01-01 13:55:28 +05:30
|
|
|
.then(() => assertMarkdownTabs(false, writeLink, previewLink, wrapper));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('markdown buttons', () => {
|
|
|
|
it('converts single words', () => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
const textarea = wrapper.find('textarea').element;
|
|
|
|
textarea.setSelectionRange(0, 7);
|
|
|
|
const markdownButton = getMarkdownButton(wrapper);
|
|
|
|
markdownButton.trigger('click');
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
return wrapper.vm.$nextTick(() => {
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(textarea.value).toContain('**testing**');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('converts a line', () => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
const textarea = wrapper.find('textarea').element;
|
|
|
|
textarea.setSelectionRange(0, 0);
|
|
|
|
const markdownButton = getAllMarkdownButtons(wrapper).wrappers[5];
|
|
|
|
markdownButton.trigger('click');
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
return wrapper.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', () => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
const textarea = wrapper.find('textarea').element;
|
|
|
|
textarea.setSelectionRange(0, 50);
|
|
|
|
const markdownButton = getAllMarkdownButtons(wrapper).wrappers[5];
|
|
|
|
markdownButton.trigger('click');
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
return wrapper.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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|