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

153 lines
4.3 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
2021-03-08 18:12:59 +05:30
const createWrapper = (props) => {
2020-01-01 13:55:28 +05:30
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()
2021-03-08 18:12:59 +05:30
.filter((button) => button.props(prop) === value)
2020-01-01 13:55:28 +05:30
.at(0);
beforeEach(() => {
2020-11-24 15:15:51 +05:30
window.gl = {
client: {
isMac: true,
},
};
2020-01-01 13:55:28 +05:30
createWrapper();
});
afterEach(() => {
wrapper.destroy();
wrapper = null;
2017-09-10 17:25:29 +05:30
});
2020-11-24 15:15:51 +05:30
describe('markdown header buttons', () => {
it('renders the buttons with the correct title', () => {
const buttons = [
'Add bold text (⌘B)',
'Add italic text (⌘I)',
'Insert a quote',
'Insert suggestion',
'Insert code',
'Add a link (⌘K)',
'Add a bullet list',
'Add a numbered list',
'Add a task list',
2021-06-08 01:23:25 +05:30
'Add a collapsible section',
2020-11-24 15:15:51 +05:30
'Add a table',
'Go full screen',
];
const elements = findToolbarButtons();
elements.wrappers.forEach((buttonEl, index) => {
expect(buttonEl.props('buttonTitle')).toBe(buttons[index]);
});
});
describe('when the user is on a non-Mac', () => {
beforeEach(() => {
delete window.gl.client.isMac;
createWrapper();
});
it('renders keyboard shortcuts with Ctrl+ instead of ⌘', () => {
const boldButton = findToolbarButtonByProp('icon', 'bold');
expect(boldButton.props('buttonTitle')).toBe('Add bold text (Ctrl+B)');
});
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
});
2021-06-08 01:23:25 +05:30
it('renders collapsible section template', () => {
const detailsBlockButton = findToolbarButtonByProp('icon', 'details-block');
expect(detailsBlockButton.props('tag')).toEqual(
'<details><summary>Click to expand</summary>\n{text}\n</details>',
);
});
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
});