Commit f54e79e8 authored by Simon Knox's avatar Simon Knox

Merge branch 'cngo-update-tests-to-remove-keyboardDownEvent' into 'master'

Update tests to remove `keyboardDownEvent` helper

See merge request gitlab-org/gitlab!53848
parents f34aebd7 5b6747bf
import Vue from 'vue'; import { shallowMount } from '@vue/test-utils';
import DescriptionField from '~/issue_show/components/fields/description.vue';
import MarkdownField from '~/vue_shared/components/markdown/field.vue';
import eventHub from '~/issue_show/event_hub'; import eventHub from '~/issue_show/event_hub';
import Store from '~/issue_show/stores';
import descriptionField from '~/issue_show/components/fields/description.vue';
import { keyboardDownEvent } from '../../helpers';
describe('Description field component', () => { describe('Description field component', () => {
let vm; let wrapper;
let store;
beforeEach((done) => {
const Component = Vue.extend(descriptionField);
const el = document.createElement('div');
store = new Store({
titleHtml: '',
descriptionHtml: '',
issuableRef: '',
});
store.formState.description = 'test';
document.body.appendChild(el);
jest.spyOn(eventHub, '$emit').mockImplementation(() => {}); const findTextarea = () => wrapper.find({ ref: 'textarea' });
vm = new Component({ const mountComponent = (description = 'test') =>
el, shallowMount(DescriptionField, {
attachTo: document.body,
propsData: { propsData: {
markdownPreviewPath: '/', markdownPreviewPath: '/',
markdownDocsPath: '/', markdownDocsPath: '/',
formState: store.formState, formState: {
description,
},
},
stubs: {
MarkdownField,
}, },
}).$mount(); });
beforeEach(() => {
jest.spyOn(eventHub, '$emit');
});
Vue.nextTick(done); afterEach(() => {
wrapper.destroy();
wrapper = null;
}); });
it('renders markdown field with description', () => { it('renders markdown field with description', () => {
expect(vm.$el.querySelector('.md-area textarea').value).toBe('test'); wrapper = mountComponent();
expect(findTextarea().element.value).toBe('test');
}); });
it('renders markdown field with a markdown description', (done) => { it('renders markdown field with a markdown description', () => {
store.formState.description = '**test**'; const markdown = '**test**';
Vue.nextTick(() => { wrapper = mountComponent(markdown);
expect(vm.$el.querySelector('.md-area textarea').value).toBe('**test**');
done(); expect(findTextarea().element.value).toBe(markdown);
});
}); });
it('focuses field when mounted', () => { it('focuses field when mounted', () => {
expect(document.activeElement).toBe(vm.$refs.textarea); wrapper = mountComponent();
expect(document.activeElement).toBe(findTextarea().element);
}); });
it('triggers update with meta+enter', () => { it('triggers update with meta+enter', () => {
vm.$el.querySelector('.md-area textarea').dispatchEvent(keyboardDownEvent(13, true)); wrapper = mountComponent();
expect(eventHub.$emit).toHaveBeenCalled(); findTextarea().trigger('keydown.enter', { metaKey: true });
expect(eventHub.$emit).toHaveBeenCalledWith('update.issuable');
}); });
it('triggers update with ctrl+enter', () => { it('triggers update with ctrl+enter', () => {
vm.$el.querySelector('.md-area textarea').dispatchEvent(keyboardDownEvent(13, false, true)); wrapper = mountComponent();
expect(eventHub.$emit).toHaveBeenCalled(); findTextarea().trigger('keydown.enter', { ctrlKey: true });
});
it('has a ref named `textarea`', () => { expect(eventHub.$emit).toHaveBeenCalledWith('update.issuable');
expect(vm.$refs.textarea).not.toBeNull();
}); });
}); });
import Vue from 'vue'; import { shallowMount } from '@vue/test-utils';
import TitleField from '~/issue_show/components/fields/title.vue';
import eventHub from '~/issue_show/event_hub'; import eventHub from '~/issue_show/event_hub';
import Store from '~/issue_show/stores';
import titleField from '~/issue_show/components/fields/title.vue';
import { keyboardDownEvent } from '../../helpers';
describe('Title field component', () => { describe('Title field component', () => {
let vm; let wrapper;
let store;
beforeEach(() => { const findInput = () => wrapper.find({ ref: 'input' });
const Component = Vue.extend(titleField);
store = new Store({
titleHtml: '',
descriptionHtml: '',
issuableRef: '',
});
store.formState.title = 'test';
jest.spyOn(eventHub, '$emit').mockImplementation(() => {}); beforeEach(() => {
jest.spyOn(eventHub, '$emit');
vm = new Component({ wrapper = shallowMount(TitleField, {
propsData: { propsData: {
formState: store.formState, formState: {
title: 'test',
},
}, },
}).$mount(); });
});
afterEach(() => {
wrapper.destroy();
wrapper = null;
}); });
it('renders form control with formState title', () => { it('renders form control with formState title', () => {
expect(vm.$el.querySelector('.form-control').value).toBe('test'); expect(findInput().element.value).toBe('test');
}); });
it('triggers update with meta+enter', () => { it('triggers update with meta+enter', () => {
vm.$el.querySelector('.form-control').dispatchEvent(keyboardDownEvent(13, true)); findInput().trigger('keydown.enter', { metaKey: true });
expect(eventHub.$emit).toHaveBeenCalled(); expect(eventHub.$emit).toHaveBeenCalledWith('update.issuable');
}); });
it('triggers update with ctrl+enter', () => { it('triggers update with ctrl+enter', () => {
vm.$el.querySelector('.form-control').dispatchEvent(keyboardDownEvent(13, false, true)); findInput().trigger('keydown.enter', { ctrlKey: true });
expect(eventHub.$emit).toHaveBeenCalled();
});
it('has a ref named `input`', () => { expect(eventHub.$emit).toHaveBeenCalledWith('update.issuable');
expect(vm.$refs.input).not.toBeNull();
}); });
}); });
export const keyboardDownEvent = (code, metaKey = false, ctrlKey = false) => {
const e = new CustomEvent('keydown');
e.keyCode = code;
e.metaKey = metaKey;
e.ctrlKey = ctrlKey;
return e;
};
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