2019-06-06 09:36:17 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
import { shallowMount } from '@vue/test-utils';
|
|
|
|
import modalCopyButton from '~/vue_shared/components/modal_copy_button.vue';
|
|
|
|
|
|
|
|
describe('modal copy button', () => {
|
|
|
|
const Component = Vue.extend(modalCopyButton);
|
|
|
|
let wrapper;
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = shallowMount(Component, {
|
|
|
|
propsData: {
|
|
|
|
text: 'copy me',
|
2019-10-09 12:06:13 +00:00
|
|
|
title: 'Copy this value',
|
2019-06-06 09:36:17 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('clipboard', () => {
|
|
|
|
it('should fire a `success` event on click', () => {
|
|
|
|
document.execCommand = jest.fn(() => true);
|
|
|
|
window.getSelection = jest.fn(() => ({
|
|
|
|
toString: jest.fn(() => 'test'),
|
|
|
|
removeAllRanges: jest.fn(),
|
|
|
|
}));
|
|
|
|
wrapper.trigger('click');
|
2020-01-01 06:09:01 +00:00
|
|
|
|
|
|
|
return wrapper.vm.$nextTick().then(() => {
|
|
|
|
expect(wrapper.emitted().success).not.toBeEmpty();
|
|
|
|
expect(document.execCommand).toHaveBeenCalledWith('copy');
|
|
|
|
});
|
2019-06-06 09:36:17 +00:00
|
|
|
});
|
|
|
|
it("should propagate the clipboard error event if execCommand doesn't work", () => {
|
|
|
|
document.execCommand = jest.fn(() => false);
|
|
|
|
wrapper.trigger('click');
|
2020-01-01 06:09:01 +00:00
|
|
|
|
|
|
|
return wrapper.vm.$nextTick().then(() => {
|
|
|
|
expect(wrapper.emitted().error).not.toBeEmpty();
|
|
|
|
expect(document.execCommand).toHaveBeenCalledWith('copy');
|
|
|
|
});
|
2019-06-06 09:36:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|