summaryrefslogtreecommitdiff
path: root/spec/frontend/packages/list/components/packages_sort_spec.js
blob: ff3e8e19413a82fa39f31fcd2e435050c87f4bd4 (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
import Vuex from 'vuex';
import { GlSorting } from '@gitlab/ui';
import { mount, createLocalVue } from '@vue/test-utils';
import stubChildren from 'helpers/stub_children';
import PackagesSort from '~/packages/list/components/packages_sort.vue';

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

describe('packages_sort', () => {
  let wrapper;
  let store;
  let sorting;
  let sortingItems;

  const GlSortingItem = { name: 'sorting-item-stub', template: '<div><slot></slot></div>' };

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

  const createStore = isGroupPage => {
    const state = {
      config: {
        isGroupPage,
      },
      sorting: {
        orderBy: 'version',
        sort: 'desc',
      },
    };
    store = new Vuex.Store({
      state,
    });
    store.dispatch = jest.fn();
  };

  const mountComponent = (isGroupPage = false) => {
    createStore(isGroupPage);

    wrapper = mount(PackagesSort, {
      localVue,
      store,
      stubs: {
        ...stubChildren(PackagesSort),
        GlSortingItem,
      },
    });
  };

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

  describe('when is in projects', () => {
    beforeEach(() => {
      mountComponent();
      sorting = findPackageListSorting();
      sortingItems = findSortingItems();
    });

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

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

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

  describe('when is in group', () => {
    beforeEach(() => {
      mountComponent(true);
      sorting = findPackageListSorting();
      sortingItems = findSortingItems();
    });

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