summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_mr_widget/components/mr_widget_header_spec.js
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/vue_mr_widget/components/mr_widget_header_spec.js')
-rw-r--r--spec/frontend/vue_mr_widget/components/mr_widget_header_spec.js35
1 files changed, 35 insertions, 0 deletions
diff --git a/spec/frontend/vue_mr_widget/components/mr_widget_header_spec.js b/spec/frontend/vue_mr_widget/components/mr_widget_header_spec.js
index b492a69fb3d..21058005d29 100644
--- a/spec/frontend/vue_mr_widget/components/mr_widget_header_spec.js
+++ b/spec/frontend/vue_mr_widget/components/mr_widget_header_spec.js
@@ -1,7 +1,13 @@
import Vue from 'vue';
+import Mousetrap from 'mousetrap';
import mountComponent from 'helpers/vue_mount_component_helper';
import headerComponent from '~/vue_merge_request_widget/components/mr_widget_header.vue';
+jest.mock('mousetrap', () => ({
+ bind: jest.fn(),
+ unbind: jest.fn(),
+}));
+
describe('MRWidgetHeader', () => {
let vm;
let Component;
@@ -126,6 +132,35 @@ describe('MRWidgetHeader', () => {
it('renders target branch', () => {
expect(vm.$el.querySelector('.js-target-branch').textContent.trim()).toEqual('master');
});
+
+ describe('keyboard shortcuts', () => {
+ it('binds a keyboard shortcut handler to the "b" key', () => {
+ expect(Mousetrap.bind).toHaveBeenCalledWith('b', expect.any(Function));
+ });
+
+ it('triggers a click on the "copy to clipboard" button when the handler is executed', () => {
+ const testClickHandler = jest.fn();
+ vm.$refs.copyBranchNameButton.$el.addEventListener('click', testClickHandler);
+
+ // Get a reference to the function that was assigned to the "b" shortcut key.
+ const shortcutHandler = Mousetrap.bind.mock.calls[0][1];
+
+ expect(testClickHandler).not.toHaveBeenCalled();
+
+ // Simulate Mousetrap calling the function.
+ shortcutHandler();
+
+ expect(testClickHandler).toHaveBeenCalledTimes(1);
+ });
+
+ it('unbinds the keyboard shortcut when the component is destroyed', () => {
+ expect(Mousetrap.unbind).not.toHaveBeenCalled();
+
+ vm.$destroy();
+
+ expect(Mousetrap.unbind).toHaveBeenCalledWith('b');
+ });
+ });
});
describe('with an open merge request', () => {