Commit 840d4825 authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch 'ps-fix-attach-a-file' into 'master'

Fix "Attach a file" not working after entering text

See merge request gitlab-org/gitlab!44216
parents 8e468417 bf3b5c7d
...@@ -6,7 +6,14 @@ import { addMarkdownListeners, removeMarkdownListeners } from './lib/utils/text_ ...@@ -6,7 +6,14 @@ import { addMarkdownListeners, removeMarkdownListeners } from './lib/utils/text_
import { disableButtonIfEmptyField } from '~/lib/utils/common_utils'; import { disableButtonIfEmptyField } from '~/lib/utils/common_utils';
export default class GLForm { export default class GLForm {
constructor(form, enableGFM = {}) { /**
* Create a GLForm
*
* @param {jQuery} form Root element of the GLForm
* @param {Object} enableGFM Which autocomplete features should be enabled?
* @param {Boolean} forceNew If true, treat the element as a **new** form even if `gfm-form` class already exists.
*/
constructor(form, enableGFM = {}, forceNew = false) {
this.form = form; this.form = form;
this.textarea = this.form.find('textarea.js-gfm-input'); this.textarea = this.form.find('textarea.js-gfm-input');
this.enableGFM = { ...defaultAutocompleteConfig, ...enableGFM }; this.enableGFM = { ...defaultAutocompleteConfig, ...enableGFM };
...@@ -22,7 +29,7 @@ export default class GLForm { ...@@ -22,7 +29,7 @@ export default class GLForm {
// Before we start, we should clean up any previous data for this form // Before we start, we should clean up any previous data for this form
this.destroy(); this.destroy();
// Set up the form // Set up the form
this.setupForm(); this.setupForm(forceNew);
this.form.data('glForm', this); this.form.data('glForm', this);
} }
...@@ -39,8 +46,8 @@ export default class GLForm { ...@@ -39,8 +46,8 @@ export default class GLForm {
this.form.data('glForm', null); this.form.data('glForm', null);
} }
setupForm() { setupForm(forceNew = false) {
const isNewForm = this.form.is(':not(.gfm-form)'); const isNewForm = this.form.is(':not(.gfm-form)') || forceNew;
this.form.removeClass('js-new-note-form'); this.form.removeClass('js-new-note-form');
if (isNewForm) { if (isNewForm) {
this.form.find('.div-dropzone').remove(); this.form.find('.div-dropzone').remove();
......
...@@ -167,7 +167,9 @@ export default { ...@@ -167,7 +167,9 @@ export default {
}, },
mounted() { mounted() {
// GLForm class handles all the toolbar buttons // GLForm class handles all the toolbar buttons
return new GLForm($(this.$refs['gl-form']), { return new GLForm(
$(this.$refs['gl-form']),
{
emojis: this.enableAutocomplete, emojis: this.enableAutocomplete,
members: this.enableAutocomplete && !this.glFeatures.tributeAutocomplete, members: this.enableAutocomplete && !this.glFeatures.tributeAutocomplete,
issues: this.enableAutocomplete && !this.glFeatures.tributeAutocomplete, issues: this.enableAutocomplete && !this.glFeatures.tributeAutocomplete,
...@@ -177,7 +179,9 @@ export default { ...@@ -177,7 +179,9 @@ export default {
labels: this.enableAutocomplete && !this.glFeatures.tributeAutocomplete, labels: this.enableAutocomplete && !this.glFeatures.tributeAutocomplete,
snippets: this.enableAutocomplete, snippets: this.enableAutocomplete,
vulnerabilities: this.enableAutocomplete, vulnerabilities: this.enableAutocomplete,
}); },
true,
);
}, },
beforeDestroy() { beforeDestroy() {
const glForm = $(this.$refs['gl-form']).data('glForm'); const glForm = $(this.$refs['gl-form']).data('glForm');
...@@ -230,7 +234,7 @@ export default { ...@@ -230,7 +234,7 @@ export default {
<div <div
ref="gl-form" ref="gl-form"
:class="{ 'gl-mt-3 gl-mb-3': addSpacingClasses }" :class="{ 'gl-mt-3 gl-mb-3': addSpacingClasses }"
class="js-vue-markdown-field md-area position-relative" class="js-vue-markdown-field md-area position-relative gfm-form"
> >
<markdown-header <markdown-header
:preview-markdown="previewMarkdown" :preview-markdown="previewMarkdown"
......
---
title: Fix attach file button not working in description fields
merge_request: 44216
author:
type: fixed
...@@ -24,7 +24,7 @@ exports[`Snippet Description Edit component rendering matches the snapshot 1`] = ...@@ -24,7 +24,7 @@ exports[`Snippet Description Edit component rendering matches the snapshot 1`] =
</div> </div>
<div <div
class="js-vue-markdown-field md-area position-relative js-expanded gfm-form" class="js-vue-markdown-field md-area position-relative gfm-form js-expanded"
> >
<markdown-header-stub <markdown-header-stub
linecontent="" linecontent=""
......
...@@ -2,12 +2,13 @@ import { mount } from '@vue/test-utils'; ...@@ -2,12 +2,13 @@ import { mount } from '@vue/test-utils';
import { TEST_HOST, FIXTURES_PATH } from 'spec/test_constants'; import { TEST_HOST, FIXTURES_PATH } from 'spec/test_constants';
import AxiosMockAdapter from 'axios-mock-adapter'; import AxiosMockAdapter from 'axios-mock-adapter';
import $ from 'jquery'; import $ from 'jquery';
import fieldComponent from '~/vue_shared/components/markdown/field.vue'; import MarkdownField from '~/vue_shared/components/markdown/field.vue';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
const markdownPreviewPath = `${TEST_HOST}/preview`; const markdownPreviewPath = `${TEST_HOST}/preview`;
const markdownDocsPath = `${TEST_HOST}/docs`; const markdownDocsPath = `${TEST_HOST}/docs`;
const textareaValue = 'testing\n123'; const textareaValue = 'testing\n123';
const uploadsPath = 'test/uploads';
function assertMarkdownTabs(isWrite, writeLink, previewLink, wrapper) { function assertMarkdownTabs(isWrite, writeLink, previewLink, wrapper) {
expect(writeLink.element.parentNode.classList.contains('active')).toBe(isWrite); expect(writeLink.element.parentNode.classList.contains('active')).toBe(isWrite);
...@@ -15,54 +16,81 @@ function assertMarkdownTabs(isWrite, writeLink, previewLink, wrapper) { ...@@ -15,54 +16,81 @@ function assertMarkdownTabs(isWrite, writeLink, previewLink, wrapper) {
expect(wrapper.find('.md-preview-holder').element.style.display).toBe(isWrite ? 'none' : ''); expect(wrapper.find('.md-preview-holder').element.style.display).toBe(isWrite ? 'none' : '');
} }
function createComponent() {
const wrapper = mount(fieldComponent, {
propsData: {
markdownDocsPath,
markdownPreviewPath,
isSubmitting: false,
textareaValue,
},
slots: {
textarea: `<textarea>${textareaValue}</textarea>`,
},
});
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');
const getVideo = wrapper => wrapper.find('video');
describe('Markdown field component', () => { describe('Markdown field component', () => {
let axiosMock; let axiosMock;
let subject;
beforeEach(() => { beforeEach(() => {
axiosMock = new AxiosMockAdapter(axios); axiosMock = new AxiosMockAdapter(axios);
// window.uploads_path is needed for dropzone to initialize
window.uploads_path = uploadsPath;
}); });
afterEach(() => { afterEach(() => {
subject.destroy();
subject = null;
axiosMock.restore(); axiosMock.restore();
}); });
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');
describe('mounted', () => { describe('mounted', () => {
let wrapper;
const previewHTML = ` const previewHTML = `
<p>markdown preview</p> <p>markdown preview</p>
<video src="${FIXTURES_PATH}/static/mock-video.mp4" muted="muted"></video> <video src="${FIXTURES_PATH}/static/mock-video.mp4" muted="muted"></video>
`; `;
let previewLink; let previewLink;
let writeLink; let writeLink;
let dropzoneSpy;
afterEach(() => { beforeEach(() => {
wrapper.destroy(); dropzoneSpy = jest.fn();
createSubject();
findDropzone().element.addEventListener('click', dropzoneSpy);
}); });
it('renders textarea inside backdrop', () => { it('renders textarea inside backdrop', () => {
wrapper = createComponent(); expect(subject.find('.zen-backdrop textarea').element).not.toBeNull();
expect(wrapper.find('.zen-backdrop textarea').element).not.toBeNull();
}); });
describe('markdown preview', () => { describe('markdown preview', () => {
...@@ -71,44 +99,40 @@ describe('Markdown field component', () => { ...@@ -71,44 +99,40 @@ describe('Markdown field component', () => {
}); });
it('sets preview link as active', () => { it('sets preview link as active', () => {
wrapper = createComponent(); previewLink = getPreviewLink();
previewLink = getPreviewLink(wrapper);
previewLink.trigger('click'); previewLink.trigger('click');
return wrapper.vm.$nextTick().then(() => { return subject.vm.$nextTick().then(() => {
expect(previewLink.element.parentNode.classList.contains('active')).toBeTruthy(); expect(previewLink.element.parentNode.classList.contains('active')).toBeTruthy();
}); });
}); });
it('shows preview loading text', () => { it('shows preview loading text', () => {
wrapper = createComponent(); previewLink = getPreviewLink();
previewLink = getPreviewLink(wrapper);
previewLink.trigger('click'); previewLink.trigger('click');
return wrapper.vm.$nextTick(() => { return subject.vm.$nextTick(() => {
expect(wrapper.find('.md-preview-holder').element.textContent.trim()).toContain( expect(subject.find('.md-preview-holder').element.textContent.trim()).toContain(
'Loading…', 'Loading…',
); );
}); });
}); });
it('renders markdown preview and GFM', () => { it('renders markdown preview and GFM', () => {
wrapper = createComponent();
const renderGFMSpy = jest.spyOn($.fn, 'renderGFM'); const renderGFMSpy = jest.spyOn($.fn, 'renderGFM');
previewLink = getPreviewLink(wrapper); previewLink = getPreviewLink();
previewLink.trigger('click'); previewLink.trigger('click');
return axios.waitFor(markdownPreviewPath).then(() => { return axios.waitFor(markdownPreviewPath).then(() => {
expect(wrapper.find('.md-preview-holder').element.innerHTML).toContain(previewHTML); expect(subject.find('.md-preview-holder').element.innerHTML).toContain(previewHTML);
expect(renderGFMSpy).toHaveBeenCalled(); expect(renderGFMSpy).toHaveBeenCalled();
}); });
}); });
it('calls video.pause() on comment input when isSubmitting is changed to true', () => { it('calls video.pause() on comment input when isSubmitting is changed to true', () => {
wrapper = createComponent(); previewLink = getPreviewLink();
previewLink = getPreviewLink(wrapper);
previewLink.trigger('click'); previewLink.trigger('click');
let callPause; let callPause;
...@@ -116,79 +140,107 @@ describe('Markdown field component', () => { ...@@ -116,79 +140,107 @@ describe('Markdown field component', () => {
return axios return axios
.waitFor(markdownPreviewPath) .waitFor(markdownPreviewPath)
.then(() => { .then(() => {
const video = getVideo(wrapper); const video = getVideo();
callPause = jest.spyOn(video.element, 'pause').mockImplementation(() => true); callPause = jest.spyOn(video.element, 'pause').mockImplementation(() => true);
wrapper.setProps({ subject.setProps({ isSubmitting: true });
isSubmitting: true,
markdownPreviewPath,
markdownDocsPath,
});
return wrapper.vm.$nextTick(); return subject.vm.$nextTick();
}) })
.then(() => { .then(() => {
expect(callPause).toHaveBeenCalled(); expect(callPause).toHaveBeenCalled();
}); });
}); });
it('clicking already active write or preview link does nothing', () => { it('clicking already active write or preview link does nothing', async () => {
wrapper = createComponent(); writeLink = getWriteLink();
writeLink = getWriteLink(wrapper); previewLink = getPreviewLink();
previewLink = getPreviewLink(wrapper);
writeLink.trigger('click'); writeLink.trigger('click');
return wrapper.vm await subject.vm.$nextTick();
.$nextTick()
.then(() => assertMarkdownTabs(true, writeLink, previewLink, wrapper)) assertMarkdownTabs(true, writeLink, previewLink, subject);
.then(() => writeLink.trigger('click')) writeLink.trigger('click');
.then(() => wrapper.vm.$nextTick()) await subject.vm.$nextTick();
.then(() => assertMarkdownTabs(true, writeLink, previewLink, wrapper))
.then(() => previewLink.trigger('click')) assertMarkdownTabs(true, writeLink, previewLink, subject);
.then(() => wrapper.vm.$nextTick()) previewLink.trigger('click');
.then(() => assertMarkdownTabs(false, writeLink, previewLink, wrapper)) await subject.vm.$nextTick();
.then(() => previewLink.trigger('click'))
.then(() => wrapper.vm.$nextTick()) assertMarkdownTabs(false, writeLink, previewLink, subject);
.then(() => assertMarkdownTabs(false, writeLink, previewLink, wrapper)); previewLink.trigger('click');
await subject.vm.$nextTick();
assertMarkdownTabs(false, writeLink, previewLink, subject);
}); });
}); });
describe('markdown buttons', () => { describe('markdown buttons', () => {
it('converts single words', () => { it('converts single words', () => {
wrapper = createComponent(); const textarea = subject.find('textarea').element;
const textarea = wrapper.find('textarea').element;
textarea.setSelectionRange(0, 7); textarea.setSelectionRange(0, 7);
const markdownButton = getMarkdownButton(wrapper); const markdownButton = getMarkdownButton();
markdownButton.trigger('click'); markdownButton.trigger('click');
return wrapper.vm.$nextTick(() => { return subject.vm.$nextTick(() => {
expect(textarea.value).toContain('**testing**'); expect(textarea.value).toContain('**testing**');
}); });
}); });
it('converts a line', () => { it('converts a line', () => {
wrapper = createComponent(); const textarea = subject.find('textarea').element;
const textarea = wrapper.find('textarea').element;
textarea.setSelectionRange(0, 0); textarea.setSelectionRange(0, 0);
const markdownButton = getAllMarkdownButtons(wrapper).wrappers[5]; const markdownButton = getAllMarkdownButtons().wrappers[5];
markdownButton.trigger('click'); markdownButton.trigger('click');
return wrapper.vm.$nextTick(() => { return subject.vm.$nextTick(() => {
expect(textarea.value).toContain('- testing'); expect(textarea.value).toContain('- testing');
}); });
}); });
it('converts multiple lines', () => { it('converts multiple lines', () => {
wrapper = createComponent(); const textarea = subject.find('textarea').element;
const textarea = wrapper.find('textarea').element;
textarea.setSelectionRange(0, 50); textarea.setSelectionRange(0, 50);
const markdownButton = getAllMarkdownButtons(wrapper).wrappers[5]; const markdownButton = getAllMarkdownButtons().wrappers[5];
markdownButton.trigger('click'); markdownButton.trigger('click');
return wrapper.vm.$nextTick(() => { return subject.vm.$nextTick(() => {
expect(textarea.value).toContain('- testing\n- 123'); expect(textarea.value).toContain('- testing\n- 123');
}); });
}); });
}); });
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();
});
});
}); });
}); });
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