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

packages_search_spec.js « list « components « package_registry « packages_and_registries « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6a1c34df5963e1baef19710b3bdf95d79bde5126 (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
148
149
150
151
152
153
154
155
156
157
import { nextTick } from 'vue';
import { GlFilteredSearchToken } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { sortableFields } from '~/packages_and_registries/package_registry/utils';
import component from '~/packages_and_registries/package_registry/components/list/package_search.vue';
import PackageTypeToken from '~/packages_and_registries/package_registry/components/list/tokens/package_type_token.vue';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';
import PersistedSearch from '~/packages_and_registries/shared/components/persisted_search.vue';
import { LIST_KEY_CREATED_AT } from '~/packages_and_registries/package_registry/constants';

import {
  OPERATORS_IS,
  TOKEN_TYPE_TYPE,
  TOKEN_TYPE_VERSION,
} from '~/vue_shared/components/filtered_search_bar/constants';

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

  const defaultQueryParamsMock = {
    filters: ['foo'],
    sorting: { sort: 'desc' },
  };

  const findPersistedSearch = () => wrapper.findComponent(PersistedSearch);
  const findLocalStorageSync = () => wrapper.findComponent(LocalStorageSync);

  const mountComponent = (isGroupPage = false) => {
    wrapper = shallowMountExtended(component, {
      provide() {
        return {
          isGroupPage,
        };
      },
      stubs: {
        LocalStorageSync,
      },
    });
  };

  it('has a registry search component', async () => {
    mountComponent();

    await nextTick();

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

  it('registry search is mounted after mount', () => {
    mountComponent();

    expect(findPersistedSearch().exists()).toBe(false);
  });

  it('has a LocalStorageSync component', () => {
    mountComponent();

    expect(findLocalStorageSync().props()).toMatchObject({
      storageKey: 'package_registry_list_sorting',
      value: {
        orderBy: LIST_KEY_CREATED_AT,
        sort: 'desc',
      },
    });
  });

  it.each`
    isGroupPage | page
    ${false}    | ${'project'}
    ${true}     | ${'group'}
  `('in a $page page binds the right props', async ({ isGroupPage }) => {
    mountComponent(isGroupPage);

    await nextTick();

    expect(findPersistedSearch().props()).toMatchObject({
      tokens: expect.arrayContaining([
        expect.objectContaining({
          token: PackageTypeToken,
          type: TOKEN_TYPE_TYPE,
          icon: 'package',
          operators: OPERATORS_IS,
        }),
        expect.objectContaining({
          token: GlFilteredSearchToken,
          type: TOKEN_TYPE_VERSION,
          icon: 'doc-versions',
          operators: OPERATORS_IS,
        }),
      ]),
      sortableFields: sortableFields(isGroupPage),
    });
  });

  it('on update event re-emits update event and updates internal sort', async () => {
    const payload = {
      sort: 'CREATED_FOO',
      filters: defaultQueryParamsMock.filters,
      sorting: { sort: 'foo', orderBy: 'created_at' },
    };

    mountComponent();

    await nextTick();

    findPersistedSearch().vm.$emit('update', payload);

    await nextTick();

    expect(findLocalStorageSync().props('value')).toEqual({ sort: 'foo', orderBy: 'created_at' });

    expect(wrapper.emitted('update')[0]).toEqual([
      {
        filters: {
          packageName: '',
          packageType: undefined,
          packageVersion: '',
        },
        sort: payload.sort,
        sorting: payload.sorting,
      },
    ]);
  });

  it('on update event, re-emits update event with formatted filters', async () => {
    const payload = {
      sort: 'CREATED_FOO',
      filters: [
        { type: 'type', value: { data: 'Generic', operator: '=' }, id: 'token-3' },
        { type: 'version', value: { data: '1.0.1', operator: '=' }, id: 'token-6' },
        { id: 'token-4', type: 'filtered-search-term', value: { data: 'gl' } },
        { id: 'token-5', type: 'filtered-search-term', value: { data: '' } },
      ],
      sorting: { sort: 'foo', orderBy: 'created_at' },
    };

    mountComponent();

    await nextTick();

    findPersistedSearch().vm.$emit('update', payload);

    await nextTick();

    expect(wrapper.emitted('update')[0]).toEqual([
      {
        filters: {
          packageName: 'gl',
          packageType: 'GENERIC',
          packageVersion: '1.0.1',
        },
        sort: payload.sort,
        sorting: payload.sorting,
      },
    ]);
  });
});