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

122 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-01-01 13:55:28 +05:30
import { shallowMount } from '@vue/test-utils';
2018-10-15 14:42:47 +05:30
import $ from 'jquery';
2020-01-01 13:55:28 +05:30
import HeaderComponent from '~/vue_shared/components/markdown/header.vue';
import ToolbarButton from '~/vue_shared/components/markdown/toolbar_button.vue';
2017-09-10 17:25:29 +05:30
describe('Markdown field header component', () => {
2020-01-01 13:55:28 +05:30
let wrapper;
2017-09-10 17:25:29 +05:30
2020-01-01 13:55:28 +05:30
const createWrapper = props => {
wrapper = shallowMount(HeaderComponent, {
2017-09-10 17:25:29 +05:30
propsData: {
previewMarkdown: false,
2020-01-01 13:55:28 +05:30
...props,
2017-09-10 17:25:29 +05:30
},
2020-01-01 13:55:28 +05:30
});
};
2017-09-10 17:25:29 +05:30
2020-01-01 13:55:28 +05:30
const findToolbarButtons = () => wrapper.findAll(ToolbarButton);
const findToolbarButtonByProp = (prop, value) =>
findToolbarButtons()
.filter(button => button.props(prop) === value)
.at(0);
beforeEach(() => {
createWrapper();
});
afterEach(() => {
wrapper.destroy();
wrapper = null;
2017-09-10 17:25:29 +05:30
});
2018-12-05 23:21:45 +05:30
it('renders markdown header buttons', () => {
2018-12-13 13:39:08 +05:30
const buttons = [
'Add bold text',
'Add italic text',
'Insert a quote',
2019-09-04 21:01:54 +05:30
'Insert suggestion',
2018-12-13 13:39:08 +05:30
'Insert code',
'Add a link',
'Add a bullet list',
'Add a numbered list',
'Add a task list',
'Add a table',
'Go full screen',
];
2020-01-01 13:55:28 +05:30
const elements = findToolbarButtons();
2018-12-05 23:21:45 +05:30
2020-01-01 13:55:28 +05:30
elements.wrappers.forEach((buttonEl, index) => {
expect(buttonEl.props('buttonTitle')).toBe(buttons[index]);
2018-12-05 23:21:45 +05:30
});
2017-09-10 17:25:29 +05:30
});
it('renders `write` link as active when previewMarkdown is false', () => {
2020-01-01 13:55:28 +05:30
expect(wrapper.find('li:nth-child(1)').classes()).toContain('active');
2017-09-10 17:25:29 +05:30
});
2020-01-01 13:55:28 +05:30
it('renders `preview` link as active when previewMarkdown is true', () => {
createWrapper({ previewMarkdown: true });
2017-09-10 17:25:29 +05:30
2020-01-01 13:55:28 +05:30
expect(wrapper.find('li:nth-child(2)').classes()).toContain('active');
2017-09-10 17:25:29 +05:30
});
it('emits toggle markdown event when clicking preview', () => {
2020-01-01 13:55:28 +05:30
wrapper.find('.js-preview-link').trigger('click');
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
return wrapper.vm
.$nextTick()
.then(() => {
expect(wrapper.emitted('preview-markdown').length).toEqual(1);
wrapper.find('.js-write-link').trigger('click');
return wrapper.vm.$nextTick();
})
.then(() => {
expect(wrapper.emitted('write-markdown').length).toEqual(1);
});
2017-09-10 17:25:29 +05:30
});
2018-10-15 14:42:47 +05:30
it('does not emit toggle markdown event when triggered from another form', () => {
$(document).triggerHandler('markdown-preview:show', [
2018-12-13 13:39:08 +05:30
$(
'<form><div class="js-vue-markdown-field"><textarea class="markdown-area"></textarea></div></form>',
),
2018-10-15 14:42:47 +05:30
]);
2020-01-01 13:55:28 +05:30
expect(wrapper.emitted('preview-markdown')).toBeFalsy();
expect(wrapper.emitted('write-markdown')).toBeFalsy();
2018-10-15 14:42:47 +05:30
});
2019-09-30 21:07:59 +05:30
it('blurs preview link after click', () => {
2020-01-01 13:55:28 +05:30
const link = wrapper.find('li:nth-child(2) button');
2019-09-30 21:07:59 +05:30
jest.spyOn(HTMLElement.prototype, 'blur').mockImplementation();
2017-09-10 17:25:29 +05:30
2020-01-01 13:55:28 +05:30
link.trigger('click');
2017-09-10 17:25:29 +05:30
2020-01-01 13:55:28 +05:30
expect(link.element.blur).toHaveBeenCalled();
2017-09-10 17:25:29 +05:30
});
2018-12-05 23:21:45 +05:30
it('renders markdown table template', () => {
2020-01-01 13:55:28 +05:30
const tableButton = findToolbarButtonByProp('icon', 'table');
expect(tableButton.props('tag')).toEqual(
2018-12-13 13:39:08 +05:30
'| header | header |\n| ------ | ------ |\n| cell | cell |\n| cell | cell |',
);
2018-12-05 23:21:45 +05:30
});
2019-02-15 15:39:39 +05:30
it('renders suggestion template', () => {
2020-01-01 13:55:28 +05:30
expect(findToolbarButtonByProp('buttonTitle', 'Insert suggestion').props('tag')).toEqual(
'```suggestion:-0+0\n{text}\n```',
);
2019-02-15 15:39:39 +05:30
});
it('does not render suggestion button if `canSuggest` is set to false', () => {
2020-01-01 13:55:28 +05:30
createWrapper({
canSuggest: false,
2019-02-15 15:39:39 +05:30
});
2020-01-01 13:55:28 +05:30
expect(wrapper.find('.js-suggestion-btn').exists()).toBe(false);
2019-02-15 15:39:39 +05:30
});
2017-09-10 17:25:29 +05:30
});