summaryrefslogtreecommitdiff
path: root/spec/frontend/releases/components/releases_sort_spec.js
blob: 7774532bc122135f8aadb68468a21678bd47f800 (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
import { GlSorting, GlSortingItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex';
import ReleasesSort from '~/releases/components/releases_sort.vue';
import createStore from '~/releases/stores';
import createIndexModule from '~/releases/stores/modules/index';

Vue.use(Vuex);

describe('~/releases/components/releases_sort.vue', () => {
  let wrapper;
  let store;
  let indexModule;
  const projectId = 8;

  const createComponent = () => {
    indexModule = createIndexModule({ projectId });

    store = createStore({
      modules: {
        index: indexModule,
      },
    });

    store.dispatch = jest.fn();

    wrapper = shallowMount(ReleasesSort, {
      store,
      stubs: {
        GlSortingItem,
      },
    });
  };

  const findReleasesSorting = () => wrapper.find(GlSorting);
  const findSortingItems = () => wrapper.findAll(GlSortingItem);

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

  beforeEach(() => {
    createComponent();
  });

  it('has all the sortable items', () => {
    expect(findSortingItems()).toHaveLength(wrapper.vm.sortOptions.length);
  });

  it('on sort change set sorting in vuex and emit event', () => {
    findReleasesSorting().vm.$emit('sortDirectionChange');
    expect(store.dispatch).toHaveBeenCalledWith('index/setSorting', { sort: 'asc' });
    expect(wrapper.emitted('sort:changed')).toBeTruthy();
  });

  it('on sort item click set sorting and emit event', () => {
    const item = findSortingItems().at(0);
    const { orderBy } = wrapper.vm.sortOptions[0];
    item.vm.$emit('click');
    expect(store.dispatch).toHaveBeenCalledWith('index/setSorting', { orderBy });
    expect(wrapper.emitted('sort:changed')).toBeTruthy();
  });
});