diff options
author | Andrew Fontaine <afontaine@gitlab.com> | 2019-06-06 09:36:17 +0000 |
---|---|---|
committer | Filipa Lacerda <filipa@gitlab.com> | 2019-06-06 09:36:17 +0000 |
commit | 8bea9eeddfa6e2e602f32c7130e39a8eb5f191ae (patch) | |
tree | 727bcb9e6acab161b3592e152da47066b0e579b0 /spec | |
parent | 7468ed5fd28d08907384787d657d2bbe9af77411 (diff) | |
download | gitlab-ce-8bea9eeddfa6e2e602f32c7130e39a8eb5f191ae.tar.gz |
Add a New Copy Button That Works in Modals
This copy button manages a local instance of the Clipboard plugin
specific to it, which means it is created/destroyed on the
creation/destruction of the component. This allows it to work well in
gitlab-ui modals, as the event listeners are bound on creation of the
button.
It also allows for bindings to the `container` option of the Clipboard
plugin, which allows it to work within the focus trap set by bootstrap's
modals.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/frontend/vue_shared/components/modal_copy_button_spec.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/spec/frontend/vue_shared/components/modal_copy_button_spec.js b/spec/frontend/vue_shared/components/modal_copy_button_spec.js new file mode 100644 index 00000000000..f1943861523 --- /dev/null +++ b/spec/frontend/vue_shared/components/modal_copy_button_spec.js @@ -0,0 +1,40 @@ +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', + title: 'Copy this value into Clipboard!', + }, + }); + }); + + 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'); + expect(wrapper.emitted().success).not.toBeEmpty(); + expect(document.execCommand).toHaveBeenCalledWith('copy'); + }); + it("should propagate the clipboard error event if execCommand doesn't work", () => { + document.execCommand = jest.fn(() => false); + wrapper.trigger('click'); + expect(wrapper.emitted().error).not.toBeEmpty(); + expect(document.execCommand).toHaveBeenCalledWith('copy'); + }); + }); +}); |