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.

170 lines
5 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';
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');
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();
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)',
2022-05-07 20:08:51 +05:30
'Add strikethrough text (⌘⇧X)',
2020-11-24 15:15:51 +05:30
'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
});
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
]);
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', () => {
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(
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
});
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);
});
2017-09-10 17:25:29 +05:30
});