Welcome to mirror list, hosted at ThFree Co, Russian Federation.

registry_search_spec.js « registry « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f86406d05cb3946660263e9196463ce07a500437 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { GlSorting, GlSortingItem, GlFilteredSearch } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { FILTERED_SEARCH_TERM } from '~/vue_shared/components/filtered_search_bar/constants';
import component from '~/vue_shared/components/registry/registry_search.vue';

describe('Registry Search', () => {
  let wrapper;

  const findPackageListSorting = () => wrapper.findComponent(GlSorting);
  const findSortingItems = () => wrapper.findAllComponents(GlSortingItem);
  const findFilteredSearch = () => wrapper.findComponent(GlFilteredSearch);

  const defaultProps = {
    filters: [],
    sorting: { sort: 'asc', orderBy: 'name' },
    tokens: [{ type: 'foo' }],
    sortableFields: [
      { label: 'name', orderBy: 'name' },
      { label: 'baz', orderBy: 'bar' },
    ],
  };

  const defaultQueryChangedPayload = {
    foo: '',
    orderBy: 'name',
    search: [],
    sort: 'asc',
    after: null,
    before: null,
  };

  const mountComponent = (propsData = defaultProps) => {
    wrapper = shallowMount(component, {
      propsData,
      stubs: {
        GlSortingItem,
      },
    });
  };

  describe('searching', () => {
    it('has a filtered-search component', () => {
      mountComponent();

      expect(findFilteredSearch().exists()).toBe(true);
    });

    it('binds the correct props to filtered-search', () => {
      mountComponent();

      expect(findFilteredSearch().props()).toMatchObject({
        value: [],
        placeholder: 'Filter results',
        availableTokens: wrapper.vm.tokens,
      });
    });

    it('emits filter:changed when value changes', () => {
      mountComponent();

      findFilteredSearch().vm.$emit('input', 'foo');

      expect(wrapper.emitted('filter:changed')).toEqual([['foo']]);
    });

    it('emits filter:submit and query:changed on submit event', () => {
      mountComponent();

      findFilteredSearch().vm.$emit('submit');
      expect(wrapper.emitted('filter:submit')).toEqual([[]]);
      expect(wrapper.emitted('query:changed')).toEqual([[defaultQueryChangedPayload]]);
    });

    it('emits filter:changed, filter:submit and query:changed on clear event', () => {
      mountComponent();

      findFilteredSearch().vm.$emit('clear');

      expect(wrapper.emitted('filter:changed')).toEqual([[[]]]);
      expect(wrapper.emitted('filter:submit')).toEqual([[]]);
      expect(wrapper.emitted('query:changed')).toEqual([[defaultQueryChangedPayload]]);
    });

    it('binds tokens prop', () => {
      mountComponent();

      expect(findFilteredSearch().props('availableTokens')).toEqual(defaultProps.tokens);
    });
  });

  describe('sorting', () => {
    it('has all the sortable items', () => {
      mountComponent();

      expect(findSortingItems()).toHaveLength(defaultProps.sortableFields.length);
    });

    it('on sort change emits sorting:changed event', () => {
      mountComponent();

      findPackageListSorting().vm.$emit('sortDirectionChange');
      expect(wrapper.emitted('sorting:changed')).toEqual([[{ sort: 'desc' }]]);
      expect(wrapper.emitted('query:changed')).toEqual([
        [{ ...defaultQueryChangedPayload, sort: 'desc' }],
      ]);
    });

    it('on sort item click emits sorting:changed event', () => {
      mountComponent();

      findSortingItems().at(1).vm.$emit('click');

      expect(wrapper.emitted('sorting:changed')).toEqual([
        [{ orderBy: defaultProps.sortableFields[1].orderBy }],
      ]);
      expect(wrapper.emitted('query:changed')).toEqual([
        [{ ...defaultQueryChangedPayload, orderBy: 'bar' }],
      ]);
    });
  });

  describe('query string calculation', () => {
    const filters = [
      { type: FILTERED_SEARCH_TERM, value: { data: 'one' } },
      { type: FILTERED_SEARCH_TERM, value: { data: 'two' } },
      { type: 'typeOne', value: { data: 'value_one' } },
      { type: 'typeTwo', value: { data: 'value_two' } },
    ];

    it('aggregates the filter in the correct object', () => {
      mountComponent({ ...defaultProps, filters });

      findFilteredSearch().vm.$emit('submit');

      expect(wrapper.emitted('query:changed')).toEqual([
        [
          {
            ...defaultQueryChangedPayload,
            search: ['one', 'two'],
            typeOne: 'value_one',
            typeTwo: 'value_two',
          },
        ],
      ]);
    });
  });
});