summaryrefslogtreecommitdiff
path: root/spec/frontend/notes/components/discussion_navigator_spec.js
blob: e430e18b76ad3d4158290f11e458bd64e4f13d67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* global Mousetrap */
import 'mousetrap';
import { shallowMount, createLocalVue } from '@vue/test-utils';
import Vue from 'vue';
import {
  keysFor,
  MR_NEXT_UNRESOLVED_DISCUSSION,
  MR_PREVIOUS_UNRESOLVED_DISCUSSION,
} from '~/behaviors/shortcuts/keybindings';
import DiscussionNavigator from '~/notes/components/discussion_navigator.vue';
import eventHub from '~/notes/event_hub';

describe('notes/components/discussion_navigator', () => {
  const localVue = createLocalVue();

  let wrapper;
  let jumpToNextDiscussion;
  let jumpToPreviousDiscussion;

  const createComponent = () => {
    wrapper = shallowMount(DiscussionNavigator, {
      mixins: [
        localVue.extend({
          methods: {
            jumpToNextDiscussion,
            jumpToPreviousDiscussion,
          },
        }),
      ],
    });
  };

  beforeEach(() => {
    jumpToNextDiscussion = jest.fn();
    jumpToPreviousDiscussion = jest.fn();
  });

  afterEach(() => {
    if (wrapper) {
      wrapper.destroy();
    }
    wrapper = null;
  });

  describe('on create', () => {
    let onSpy;
    let vm;

    beforeEach(() => {
      onSpy = jest.spyOn(eventHub, '$on');
      vm = new (Vue.extend(DiscussionNavigator))();
    });

    it('listens for jumpToFirstUnresolvedDiscussion events', () => {
      expect(onSpy).toHaveBeenCalledWith(
        'jumpToFirstUnresolvedDiscussion',
        vm.jumpToFirstUnresolvedDiscussion,
      );
    });
  });

  describe('on mount', () => {
    beforeEach(() => {
      createComponent();
    });

    it('calls jumpToNextDiscussion when pressing `n`', () => {
      Mousetrap.trigger(keysFor(MR_NEXT_UNRESOLVED_DISCUSSION));

      expect(jumpToNextDiscussion).toHaveBeenCalled();
    });

    it('calls jumpToPreviousDiscussion when pressing `p`', () => {
      Mousetrap.trigger(keysFor(MR_PREVIOUS_UNRESOLVED_DISCUSSION));

      expect(jumpToPreviousDiscussion).toHaveBeenCalled();
    });
  });

  describe('on destroy', () => {
    let jumpFn;

    beforeEach(() => {
      jest.spyOn(Mousetrap, 'unbind');
      jest.spyOn(eventHub, '$off');

      createComponent();

      jumpFn = wrapper.vm.jumpToFirstUnresolvedDiscussion;

      wrapper.destroy();
    });

    it('unbinds keys', () => {
      expect(Mousetrap.unbind).toHaveBeenCalledWith(keysFor(MR_NEXT_UNRESOLVED_DISCUSSION));
      expect(Mousetrap.unbind).toHaveBeenCalledWith(keysFor(MR_PREVIOUS_UNRESOLVED_DISCUSSION));
    });

    it('unbinds event hub listeners', () => {
      expect(eventHub.$off).toHaveBeenCalledWith('jumpToFirstUnresolvedDiscussion', jumpFn);
    });

    it('does not call jumpToNextDiscussion when pressing `n`', () => {
      Mousetrap.trigger('n');

      expect(jumpToNextDiscussion).not.toHaveBeenCalled();
    });

    it('does not call jumpToNextDiscussion when pressing `p`', () => {
      Mousetrap.trigger('p');

      expect(jumpToPreviousDiscussion).not.toHaveBeenCalled();
    });
  });
});