Commit 015e76ca authored by Enrique Alcántara's avatar Enrique Alcántara Committed by Natalia Tepluhina

Prepare markdown-header spec for bv upgrade

parent cd98a2b6
import Vue from 'vue'; import { shallowMount } from '@vue/test-utils';
import $ from 'jquery'; import $ from 'jquery';
import headerComponent from '~/vue_shared/components/markdown/header.vue'; import HeaderComponent from '~/vue_shared/components/markdown/header.vue';
import ToolbarButton from '~/vue_shared/components/markdown/toolbar_button.vue';
describe('Markdown field header component', () => { describe('Markdown field header component', () => {
let vm; let wrapper;
beforeEach(() => { const createWrapper = props => {
const Component = Vue.extend(headerComponent); wrapper = shallowMount(HeaderComponent, {
vm = new Component({
propsData: { propsData: {
previewMarkdown: false, previewMarkdown: false,
...props,
}, },
}).$mount(); sync: false,
attachToDocument: true,
});
};
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;
}); });
it('renders markdown header buttons', () => { it('renders markdown header buttons', () => {
...@@ -29,75 +46,72 @@ describe('Markdown field header component', () => { ...@@ -29,75 +46,72 @@ describe('Markdown field header component', () => {
'Add a table', 'Add a table',
'Go full screen', 'Go full screen',
]; ];
const elements = vm.$el.querySelectorAll('.toolbar-btn'); const elements = findToolbarButtons();
elements.forEach((buttonEl, index) => { elements.wrappers.forEach((buttonEl, index) => {
expect(buttonEl.getAttribute('data-original-title')).toBe(buttons[index]); expect(buttonEl.props('buttonTitle')).toBe(buttons[index]);
}); });
}); });
it('renders `write` link as active when previewMarkdown is false', () => { it('renders `write` link as active when previewMarkdown is false', () => {
expect(vm.$el.querySelector('li:nth-child(1)').classList.contains('active')).toBeTruthy(); expect(wrapper.find('li:nth-child(1)').classes()).toContain('active');
}); });
it('renders `preview` link as active when previewMarkdown is true', () => { it('renders `preview` link as active when previewMarkdown is true', () => {
vm.previewMarkdown = true; createWrapper({ previewMarkdown: true });
Vue.nextTick(() => { expect(wrapper.find('li:nth-child(2)').classes()).toContain('active');
expect(vm.$el.querySelector('li:nth-child(2)').classList.contains('active')).toBeTruthy();
});
}); });
it('emits toggle markdown event when clicking preview', () => { it('emits toggle markdown event when clicking preview', () => {
jest.spyOn(vm, '$emit').mockImplementation(); wrapper.find('.js-preview-link').trigger('click');
vm.$el.querySelector('.js-preview-link').click();
expect(vm.$emit).toHaveBeenCalledWith('preview-markdown'); expect(wrapper.emitted('preview-markdown').length).toEqual(1);
vm.$el.querySelector('.js-write-link').click(); wrapper.find('.js-write-link').trigger('click');
expect(vm.$emit).toHaveBeenCalledWith('write-markdown'); expect(wrapper.emitted('write-markdown').length).toEqual(1);
}); });
it('does not emit toggle markdown event when triggered from another form', () => { it('does not emit toggle markdown event when triggered from another form', () => {
jest.spyOn(vm, '$emit').mockImplementation();
$(document).triggerHandler('markdown-preview:show', [ $(document).triggerHandler('markdown-preview:show', [
$( $(
'<form><div class="js-vue-markdown-field"><textarea class="markdown-area"></textarea></div></form>', '<form><div class="js-vue-markdown-field"><textarea class="markdown-area"></textarea></div></form>',
), ),
]); ]);
expect(vm.$emit).not.toHaveBeenCalled(); expect(wrapper.emitted('preview-markdown')).toBeFalsy();
expect(wrapper.emitted('write-markdown')).toBeFalsy();
}); });
it('blurs preview link after click', () => { it('blurs preview link after click', () => {
const link = vm.$el.querySelector('li:nth-child(2) button'); const link = wrapper.find('li:nth-child(2) button');
jest.spyOn(HTMLElement.prototype, 'blur').mockImplementation(); jest.spyOn(HTMLElement.prototype, 'blur').mockImplementation();
link.click(); link.trigger('click');
expect(link.blur).toHaveBeenCalled(); expect(link.element.blur).toHaveBeenCalled();
}); });
it('renders markdown table template', () => { it('renders markdown table template', () => {
expect(vm.mdTable).toEqual( const tableButton = findToolbarButtonByProp('icon', 'table');
expect(tableButton.props('tag')).toEqual(
'| header | header |\n| ------ | ------ |\n| cell | cell |\n| cell | cell |', '| header | header |\n| ------ | ------ |\n| cell | cell |\n| cell | cell |',
); );
}); });
it('renders suggestion template', () => { it('renders suggestion template', () => {
vm.lineContent = 'Some content'; expect(findToolbarButtonByProp('buttonTitle', 'Insert suggestion').props('tag')).toEqual(
'```suggestion:-0+0\n{text}\n```',
expect(vm.mdSuggestion).toEqual('```suggestion:-0+0\n{text}\n```'); );
}); });
it('does not render suggestion button if `canSuggest` is set to false', () => { it('does not render suggestion button if `canSuggest` is set to false', () => {
vm.canSuggest = false; createWrapper({
canSuggest: false,
Vue.nextTick(() => {
expect(vm.$el.querySelector('.js-suggestion-btn')).toBe(null);
}); });
expect(wrapper.find('.js-suggestion-btn').exists()).toBe(false);
}); });
}); });
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