summaryrefslogtreecommitdiff
path: root/spec/frontend/notes/components/sort_discussion_spec.js
blob: 575f1057db2370170b803fbb5a19d1b302b0a814 (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
import { shallowMount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
import SortDiscussion from '~/notes/components/sort_discussion.vue';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';
import createStore from '~/notes/stores';
import { ASC, DESC } from '~/notes/constants';
import Tracking from '~/tracking';

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

describe('Sort Discussion component', () => {
  let wrapper;
  let store;

  const createComponent = () => {
    jest.spyOn(store, 'dispatch').mockImplementation();

    wrapper = shallowMount(SortDiscussion, {
      localVue,
      store,
    });
  };

  const findLocalStorageSync = () => wrapper.find(LocalStorageSync);

  beforeEach(() => {
    store = createStore();
    jest.spyOn(Tracking, 'event');
  });

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

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

    it('has local storage sync', () => {
      expect(findLocalStorageSync().exists()).toBe(true);
    });

    it('calls setDiscussionSortDirection when update is emitted', () => {
      findLocalStorageSync().vm.$emit('input', ASC);

      expect(store.dispatch).toHaveBeenCalledWith('setDiscussionSortDirection', ASC);
    });
  });

  describe('when asc', () => {
    describe('when the dropdown is clicked', () => {
      it('calls the right actions', () => {
        createComponent();

        wrapper.find('.js-newest-first').trigger('click');

        expect(store.dispatch).toHaveBeenCalledWith('setDiscussionSortDirection', DESC);
        expect(Tracking.event).toHaveBeenCalledWith(undefined, 'change_discussion_sort_direction', {
          property: DESC,
        });
      });
    });

    it('shows the "Oldest First" as the dropdown', () => {
      createComponent();

      expect(wrapper.find('.js-dropdown-text').text()).toBe('Oldest first');
    });
  });

  describe('when desc', () => {
    beforeEach(() => {
      store.state.discussionSortOrder = DESC;
      createComponent();
    });

    describe('when the dropdown item is clicked', () => {
      it('calls the right actions', () => {
        wrapper.find('.js-oldest-first').trigger('click');

        expect(store.dispatch).toHaveBeenCalledWith('setDiscussionSortDirection', ASC);
        expect(Tracking.event).toHaveBeenCalledWith(undefined, 'change_discussion_sort_direction', {
          property: ASC,
        });
      });

      it('applies the active class to the correct button in the dropdown', () => {
        expect(wrapper.find('.js-newest-first').classes()).toContain('is-active');
      });
    });

    it('shows the "Newest First" as the dropdown', () => {
      expect(wrapper.find('.js-dropdown-text').text()).toBe('Newest first');
    });
  });
});