diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-27 00:06:38 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-27 00:06:38 +0300 |
commit | 59a349817c0869e8e0d750884b34256ca4faefca (patch) | |
tree | abb8297dd313e550184f1933a9dc7795a3749c0a /spec/frontend/vue_shared/components | |
parent | 581c10e344d85729e77fce78513819d159289dc5 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/frontend/vue_shared/components')
-rw-r--r-- | spec/frontend/vue_shared/components/markdown/header_spec.js | 86 |
1 files changed, 50 insertions, 36 deletions
diff --git a/spec/frontend/vue_shared/components/markdown/header_spec.js b/spec/frontend/vue_shared/components/markdown/header_spec.js index 1c4247fb5f0..1014fbf0308 100644 --- a/spec/frontend/vue_shared/components/markdown/header_spec.js +++ b/spec/frontend/vue_shared/components/markdown/header_spec.js @@ -1,18 +1,35 @@ -import Vue from 'vue'; +import { shallowMount } from '@vue/test-utils'; 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', () => { - let vm; + let wrapper; - beforeEach(() => { - const Component = Vue.extend(headerComponent); - - vm = new Component({ + const createWrapper = props => { + wrapper = shallowMount(HeaderComponent, { propsData: { 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', () => { @@ -29,75 +46,72 @@ describe('Markdown field header component', () => { 'Add a table', 'Go full screen', ]; - const elements = vm.$el.querySelectorAll('.toolbar-btn'); + const elements = findToolbarButtons(); - elements.forEach((buttonEl, index) => { - expect(buttonEl.getAttribute('data-original-title')).toBe(buttons[index]); + elements.wrappers.forEach((buttonEl, index) => { + expect(buttonEl.props('buttonTitle')).toBe(buttons[index]); }); }); 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', () => { - vm.previewMarkdown = true; + createWrapper({ previewMarkdown: true }); - Vue.nextTick(() => { - expect(vm.$el.querySelector('li:nth-child(2)').classList.contains('active')).toBeTruthy(); - }); + expect(wrapper.find('li:nth-child(2)').classes()).toContain('active'); }); it('emits toggle markdown event when clicking preview', () => { - jest.spyOn(vm, '$emit').mockImplementation(); - - vm.$el.querySelector('.js-preview-link').click(); + wrapper.find('.js-preview-link').trigger('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', () => { - jest.spyOn(vm, '$emit').mockImplementation(); - $(document).triggerHandler('markdown-preview:show', [ $( '<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', () => { - 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(); - link.click(); + link.trigger('click'); - expect(link.blur).toHaveBeenCalled(); + expect(link.element.blur).toHaveBeenCalled(); }); 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 |', ); }); it('renders suggestion template', () => { - vm.lineContent = 'Some content'; - - expect(vm.mdSuggestion).toEqual('```suggestion:-0+0\n{text}\n```'); + expect(findToolbarButtonByProp('buttonTitle', 'Insert suggestion').props('tag')).toEqual( + '```suggestion:-0+0\n{text}\n```', + ); }); it('does not render suggestion button if `canSuggest` is set to false', () => { - vm.canSuggest = false; - - Vue.nextTick(() => { - expect(vm.$el.querySelector('.js-suggestion-btn')).toBe(null); + createWrapper({ + canSuggest: false, }); + + expect(wrapper.find('.js-suggestion-btn').exists()).toBe(false); }); }); |