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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

219 lines
6.4 KiB
JavaScript
Raw Normal View History

2018-10-15 14:42:47 +05:30
import $ from 'jquery';
2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
import { GlTabs } from '@gitlab/ui';
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';
2023-05-27 22:25:52 +05:30
import DrawioToolbarButton from '~/vue_shared/components/markdown/drawio_toolbar_button.vue';
2022-04-04 11:22:00 +05:30
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
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) => {
2022-04-04 11:22:00 +05:30
wrapper = shallowMountExtended(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
},
2022-04-04 11:22:00 +05:30
stubs: { GlTabs },
2020-01-01 13:55:28 +05:30
});
};
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
const findWriteTab = () => wrapper.findByTestId('write-tab');
const findPreviewTab = () => wrapper.findByTestId('preview-tab');
const findToolbar = () => wrapper.findByTestId('md-header-toolbar');
2022-08-27 11:52:29 +05:30
const findToolbarButtons = () => wrapper.findAllComponents(ToolbarButton);
2020-01-01 13:55:28 +05:30
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);
2023-05-27 22:25:52 +05:30
const findDrawioToolbarButton = () => wrapper.findComponent(DrawioToolbarButton);
2020-01-01 13:55:28 +05:30
beforeEach(() => {
2020-11-24 15:15:51 +05:30
window.gl = {
client: {
isMac: true,
},
};
2020-01-01 13:55:28 +05:30
createWrapper();
});
2020-11-24 15:15:51 +05:30
describe('markdown header buttons', () => {
it('renders the buttons with the correct title', () => {
const buttons = [
2022-08-27 11:52:29 +05:30
'Insert suggestion',
2020-11-24 15:15:51 +05:30
'Add bold text (⌘B)',
'Add italic text (⌘I)',
2022-05-07 20:08:51 +05:30
'Add strikethrough text (⌘⇧X)',
2020-11-24 15:15:51 +05:30
'Insert a quote',
'Insert code',
'Add a link (⌘K)',
'Add a bullet list',
'Add a numbered list',
2022-08-27 11:52:29 +05:30
'Add a checklist',
2022-11-25 23:54:43 +05:30
'Indent line (⌘])',
'Outdent line (⌘[)',
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]);
});
});
2022-08-27 11:52:29 +05:30
it('renders "Attach a file or image" button using gl-button', () => {
const button = wrapper.findByTestId('button-attach-file');
expect(button.element.tagName).toBe('GL-BUTTON-STUB');
expect(button.attributes('title')).toBe('Attach a file or image');
});
2020-11-24 15:15:51 +05:30
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
});
2022-04-04 11:22:00 +05:30
it('activates `write` tab when previewMarkdown is false', () => {
expect(findWriteTab().attributes('active')).toBe('true');
expect(findPreviewTab().attributes('active')).toBeUndefined();
2017-09-10 17:25:29 +05:30
});
2022-04-04 11:22:00 +05:30
it('activates `preview` tab when previewMarkdown is true', () => {
2020-01-01 13:55:28 +05:30
createWrapper({ previewMarkdown: true });
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
expect(findWriteTab().attributes('active')).toBeUndefined();
expect(findPreviewTab().attributes('active')).toBe('true');
2017-09-10 17:25:29 +05:30
});
2022-04-04 11:22:00 +05:30
it('hides toolbar in preview mode', () => {
createWrapper({ previewMarkdown: true });
2022-06-21 17:19:12 +05:30
expect(findToolbar().classes().includes('gl-display-none!')).toBe(true);
2022-04-04 11:22:00 +05:30
});
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
it('emits toggle markdown event when clicking preview tab', async () => {
const eventData = { target: {} };
findPreviewTab().vm.$emit('click', eventData);
2020-03-13 15:44:24 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
expect(wrapper.emitted('preview-markdown').length).toEqual(1);
findWriteTab().vm.$emit('click', eventData);
await nextTick();
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
]);
2022-08-27 11:52:29 +05:30
expect(wrapper.emitted('preview-markdown')).toBeUndefined();
expect(wrapper.emitted('write-markdown')).toBeUndefined();
2018-10-15 14:42:47 +05:30
});
2019-09-30 21:07:59 +05:30
it('blurs preview link after click', () => {
2022-04-04 11:22:00 +05:30
const target = { blur: jest.fn() };
findPreviewTab().vm.$emit('click', { target });
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
expect(target.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(
2022-11-25 23:54:43 +05:30
'| header | header |\n| ------ | ------ |\n| | |\n| | |',
2018-12-13 13:39:08 +05:30
);
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
});
2022-05-07 20:08:51 +05:30
it('hides preview tab when previewMarkdown property is false', () => {
createWrapper({
enablePreview: false,
});
expect(wrapper.findByTestId('preview-tab').exists()).toBe(false);
});
2022-07-16 23:28:13 +05:30
describe('restricted tool bar items', () => {
let defaultCount;
beforeEach(() => {
defaultCount = findToolbarButtons().length;
});
it('restricts items as per input', () => {
createWrapper({
restrictedToolBarItems: ['quote'],
});
expect(findToolbarButtons().length).toBe(defaultCount - 1);
});
it('shows all items by default', () => {
createWrapper();
expect(findToolbarButtons().length).toBe(defaultCount);
});
});
2023-05-27 22:25:52 +05:30
describe('when drawIOEnabled is true', () => {
const uploadsPath = '/uploads';
const markdownPreviewPath = '/preview';
beforeEach(() => {
createWrapper({
drawioEnabled: true,
uploadsPath,
markdownPreviewPath,
});
});
it('renders drawio toolbar button', () => {
expect(findDrawioToolbarButton().props()).toEqual({
uploadsPath,
markdownPreviewPath,
});
});
});
2017-09-10 17:25:29 +05:30
});