2021-01-03 14:25:43 +05:30
|
|
|
import { GlButton } from '@gitlab/ui';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { mount } from '@vue/test-utils';
|
2021-03-08 18:12:59 +05:30
|
|
|
import initCopyToClipboard from '~/behaviors/copy_to_clipboard';
|
2021-03-11 19:13:27 +05:30
|
|
|
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
describe('clipboard button', () => {
|
|
|
|
let wrapper;
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
const createWrapper = (propsData, options = {}) => {
|
|
|
|
wrapper = mount(ClipboardButton, {
|
2020-01-01 13:55:28 +05:30
|
|
|
propsData,
|
2021-02-22 17:27:13 +05:30
|
|
|
...options,
|
2020-01-01 13:55:28 +05:30
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
const findButton = () => wrapper.find(GlButton);
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('without gfm', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createWrapper({
|
|
|
|
text: 'copy me',
|
|
|
|
title: 'Copy this value',
|
|
|
|
cssClass: 'btn-danger',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a button for clipboard', () => {
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(findButton().exists()).toBe(true);
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(wrapper.attributes('data-clipboard-text')).toBe('copy me');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have a tooltip with default values', () => {
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(wrapper.attributes('title')).toBe('Copy this value');
|
2020-01-01 13:55:28 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should render provided classname', () => {
|
|
|
|
expect(wrapper.classes()).toContain('btn-danger');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with gfm', () => {
|
|
|
|
it('sets data-clipboard-text with gfm', () => {
|
|
|
|
createWrapper({
|
|
|
|
text: 'copy me',
|
|
|
|
gfm: '`path/to/file`',
|
|
|
|
title: 'Copy this value',
|
|
|
|
cssClass: 'btn-danger',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.attributes('data-clipboard-text')).toBe(
|
|
|
|
'{"text":"copy me","gfm":"`path/to/file`"}',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
it('renders default slot as button text', () => {
|
|
|
|
createWrapper(
|
|
|
|
{
|
|
|
|
text: 'copy me',
|
|
|
|
title: 'Copy this value',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
slots: {
|
|
|
|
default: 'Foo bar',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(findButton().text()).toBe('Foo bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('re-emits button events', () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
createWrapper(
|
|
|
|
{
|
|
|
|
text: 'copy me',
|
|
|
|
title: 'Copy this value',
|
|
|
|
},
|
|
|
|
{ listeners: { click: onClick } },
|
|
|
|
);
|
|
|
|
|
|
|
|
findButton().trigger('click');
|
|
|
|
|
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
describe('integration', () => {
|
|
|
|
it('actually copies to clipboard', () => {
|
|
|
|
initCopyToClipboard();
|
|
|
|
|
|
|
|
document.execCommand = () => {};
|
|
|
|
jest.spyOn(document, 'execCommand').mockImplementation(() => true);
|
|
|
|
|
|
|
|
createWrapper(
|
|
|
|
{
|
|
|
|
text: 'copy me',
|
|
|
|
title: 'Copy this value',
|
|
|
|
},
|
|
|
|
{ attachTo: document.body },
|
|
|
|
);
|
|
|
|
|
|
|
|
findButton().trigger('click');
|
|
|
|
|
|
|
|
expect(document.execCommand).toHaveBeenCalledWith('copy');
|
|
|
|
});
|
|
|
|
});
|
2020-01-01 13:55:28 +05:30
|
|
|
});
|