summaryrefslogtreecommitdiff
path: root/spec/frontend/notes/components/discussion_keyboard_navigator_spec.js
blob: 8881bedf3cc9da8399c82d46718986d7b7f9d102 (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
/* global Mousetrap */
import 'mousetrap';
import { shallowMount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
import DiscussionKeyboardNavigator from '~/notes/components/discussion_keyboard_navigator.vue';
import notesModule from '~/notes/stores/modules';

const localVue = createLocalVue();
localVue.use(Vuex);

const NEXT_ID = 'abc123';
const PREV_ID = 'def456';
const NEXT_DIFF_ID = 'abc123_diff';
const PREV_DIFF_ID = 'def456_diff';

describe('notes/components/discussion_keyboard_navigator', () => {
  let storeOptions;
  let wrapper;
  let store;

  const createComponent = (options = {}) => {
    store = new Vuex.Store(storeOptions);

    wrapper = shallowMount(DiscussionKeyboardNavigator, {
      localVue,
      store,
      ...options,
    });

    wrapper.vm.jumpToDiscussion = jest.fn();
  };

  beforeEach(() => {
    const notes = notesModule();

    notes.getters.nextUnresolvedDiscussionId = () => (currId, isDiff) =>
      isDiff ? NEXT_DIFF_ID : NEXT_ID;
    notes.getters.previousUnresolvedDiscussionId = () => (currId, isDiff) =>
      isDiff ? PREV_DIFF_ID : PREV_ID;

    storeOptions = {
      modules: {
        notes,
      },
    };
  });

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

  describe.each`
    isDiffView | expectedNextId  | expectedPrevId
    ${true}    | ${NEXT_DIFF_ID} | ${PREV_DIFF_ID}
    ${false}   | ${NEXT_ID}      | ${PREV_ID}
  `('when isDiffView is $isDiffView', ({ isDiffView, expectedNextId, expectedPrevId }) => {
    beforeEach(() => {
      createComponent({ propsData: { isDiffView } });
    });

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

      expect(wrapper.vm.jumpToDiscussion).toHaveBeenCalledWith(expectedNextId);
      expect(wrapper.vm.currentDiscussionId).toEqual(expectedNextId);
    });

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

      expect(wrapper.vm.jumpToDiscussion).toHaveBeenCalledWith(expectedPrevId);
      expect(wrapper.vm.currentDiscussionId).toEqual(expectedPrevId);
    });
  });

  describe('on destroy', () => {
    beforeEach(() => {
      jest.spyOn(Mousetrap, 'unbind');

      createComponent();

      wrapper.destroy();
    });

    it('unbinds keys', () => {
      expect(Mousetrap.unbind).toHaveBeenCalledWith('n');
      expect(Mousetrap.unbind).toHaveBeenCalledWith('p');
    });

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

      expect(wrapper.vm.jumpToDiscussion).not.toHaveBeenCalled();
    });

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

      expect(wrapper.vm.jumpToDiscussion).not.toHaveBeenCalled();
    });
  });
});