debian-mirror-gitlab/spec/frontend/notes/components/discussion_navigator_spec.js

114 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-10-12 21:52:04 +05:30
/* global Mousetrap */
import 'mousetrap';
2022-04-04 11:22:00 +05:30
import { shallowMount } from '@vue/test-utils';
2021-03-11 19:13:27 +05:30
import Vue from 'vue';
2021-04-29 21:17:54 +05:30
import {
keysFor,
MR_NEXT_UNRESOLVED_DISCUSSION,
MR_PREVIOUS_UNRESOLVED_DISCUSSION,
} from '~/behaviors/shortcuts/keybindings';
2020-10-24 23:57:45 +05:30
import DiscussionNavigator from '~/notes/components/discussion_navigator.vue';
import eventHub from '~/notes/event_hub';
2019-10-12 21:52:04 +05:30
2020-10-24 23:57:45 +05:30
describe('notes/components/discussion_navigator', () => {
2020-04-08 14:13:33 +05:30
let wrapper;
let jumpToNextDiscussion;
let jumpToPreviousDiscussion;
2019-10-12 21:52:04 +05:30
2020-04-08 14:13:33 +05:30
const createComponent = () => {
2020-10-24 23:57:45 +05:30
wrapper = shallowMount(DiscussionNavigator, {
2020-04-08 14:13:33 +05:30
mixins: [
2022-04-04 11:22:00 +05:30
{
2020-04-08 14:13:33 +05:30
methods: {
jumpToNextDiscussion,
jumpToPreviousDiscussion,
},
2022-04-04 11:22:00 +05:30
},
2020-04-08 14:13:33 +05:30
],
2019-10-12 21:52:04 +05:30
});
};
beforeEach(() => {
2020-04-08 14:13:33 +05:30
jumpToNextDiscussion = jest.fn();
jumpToPreviousDiscussion = jest.fn();
2019-10-12 21:52:04 +05:30
});
afterEach(() => {
2020-10-24 23:57:45 +05:30
if (wrapper) {
wrapper.destroy();
}
2020-04-08 14:13:33 +05:30
wrapper = null;
2019-10-12 21:52:04 +05:30
});
2020-10-24 23:57:45 +05:30
describe('on create', () => {
let onSpy;
let vm;
beforeEach(() => {
onSpy = jest.spyOn(eventHub, '$on');
2022-04-04 11:22:00 +05:30
vm = new Vue(DiscussionNavigator);
2020-10-24 23:57:45 +05:30
});
it('listens for jumpToFirstUnresolvedDiscussion events', () => {
expect(onSpy).toHaveBeenCalledWith(
'jumpToFirstUnresolvedDiscussion',
vm.jumpToFirstUnresolvedDiscussion,
);
});
});
2020-04-08 14:13:33 +05:30
describe('on mount', () => {
2019-10-12 21:52:04 +05:30
beforeEach(() => {
2020-03-13 15:44:24 +05:30
createComponent();
2019-10-12 21:52:04 +05:30
});
it('calls jumpToNextDiscussion when pressing `n`', () => {
2021-04-29 21:17:54 +05:30
Mousetrap.trigger(keysFor(MR_NEXT_UNRESOLVED_DISCUSSION));
2019-10-12 21:52:04 +05:30
2020-04-08 14:13:33 +05:30
expect(jumpToNextDiscussion).toHaveBeenCalled();
2019-10-12 21:52:04 +05:30
});
it('calls jumpToPreviousDiscussion when pressing `p`', () => {
2021-04-29 21:17:54 +05:30
Mousetrap.trigger(keysFor(MR_PREVIOUS_UNRESOLVED_DISCUSSION));
2019-10-12 21:52:04 +05:30
2020-04-08 14:13:33 +05:30
expect(jumpToPreviousDiscussion).toHaveBeenCalled();
2019-10-12 21:52:04 +05:30
});
});
describe('on destroy', () => {
2020-10-24 23:57:45 +05:30
let jumpFn;
2019-10-12 21:52:04 +05:30
beforeEach(() => {
jest.spyOn(Mousetrap, 'unbind');
2020-10-24 23:57:45 +05:30
jest.spyOn(eventHub, '$off');
2019-10-12 21:52:04 +05:30
createComponent();
2020-10-24 23:57:45 +05:30
jumpFn = wrapper.vm.jumpToFirstUnresolvedDiscussion;
2019-10-12 21:52:04 +05:30
wrapper.destroy();
});
it('unbinds keys', () => {
2021-04-29 21:17:54 +05:30
expect(Mousetrap.unbind).toHaveBeenCalledWith(keysFor(MR_NEXT_UNRESOLVED_DISCUSSION));
expect(Mousetrap.unbind).toHaveBeenCalledWith(keysFor(MR_PREVIOUS_UNRESOLVED_DISCUSSION));
2019-10-12 21:52:04 +05:30
});
2020-10-24 23:57:45 +05:30
it('unbinds event hub listeners', () => {
expect(eventHub.$off).toHaveBeenCalledWith('jumpToFirstUnresolvedDiscussion', jumpFn);
});
2019-10-12 21:52:04 +05:30
it('does not call jumpToNextDiscussion when pressing `n`', () => {
Mousetrap.trigger('n');
2020-04-08 14:13:33 +05:30
expect(jumpToNextDiscussion).not.toHaveBeenCalled();
2019-10-12 21:52:04 +05:30
});
it('does not call jumpToNextDiscussion when pressing `p`', () => {
Mousetrap.trigger('p');
2020-04-08 14:13:33 +05:30
expect(jumpToPreviousDiscussion).not.toHaveBeenCalled();
2019-10-12 21:52:04 +05:30
});
});
});