summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/filtered_search_bar/filtered_search_utils_spec.js
blob: 9e96c154546fc958d8e710835983f3b4eb5e3675 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import {
  stripQuotes,
  uniqueTokens,
  prepareTokens,
  processFilters,
  filterToQueryObject,
  urlQueryToFilter,
} from '~/vue_shared/components/filtered_search_bar/filtered_search_utils';

import {
  tokenValueAuthor,
  tokenValueLabel,
  tokenValueMilestone,
  tokenValuePlain,
} from './mock_data';

describe('Filtered Search Utils', () => {
  describe('stripQuotes', () => {
    it.each`
      inputValue     | outputValue
      ${'"Foo Bar"'} | ${'Foo Bar'}
      ${"'Foo Bar'"} | ${'Foo Bar'}
      ${'FooBar'}    | ${'FooBar'}
      ${"Foo'Bar"}   | ${"Foo'Bar"}
      ${'Foo"Bar'}   | ${'Foo"Bar'}
      ${'Foo Bar'}   | ${'Foo Bar'}
    `(
      'returns string $outputValue when called with string $inputValue',
      ({ inputValue, outputValue }) => {
        expect(stripQuotes(inputValue)).toBe(outputValue);
      },
    );
  });

  describe('uniqueTokens', () => {
    it('returns tokens array with duplicates removed', () => {
      expect(
        uniqueTokens([
          tokenValueAuthor,
          tokenValueLabel,
          tokenValueMilestone,
          tokenValueLabel,
          tokenValuePlain,
        ]),
      ).toHaveLength(4); // Removes 2nd instance of tokenValueLabel
    });

    it('returns tokens array as it is if it does not have duplicates', () => {
      expect(
        uniqueTokens([tokenValueAuthor, tokenValueLabel, tokenValueMilestone, tokenValuePlain]),
      ).toHaveLength(4);
    });
  });
});

describe('prepareTokens', () => {
  describe('with empty data', () => {
    it('returns an empty array', () => {
      expect(prepareTokens()).toEqual([]);
      expect(prepareTokens({})).toEqual([]);
      expect(prepareTokens({ milestone: null, author: null, assignees: [], labels: [] })).toEqual(
        [],
      );
    });
  });

  it.each([
    [
      'milestone',
      { value: 'v1.0', operator: '=' },
      [{ type: 'milestone', value: { data: 'v1.0', operator: '=' } }],
    ],
    [
      'author',
      { value: 'mr.popo', operator: '!=' },
      [{ type: 'author', value: { data: 'mr.popo', operator: '!=' } }],
    ],
    [
      'labels',
      [{ value: 'z-fighters', operator: '=' }],
      [{ type: 'labels', value: { data: 'z-fighters', operator: '=' } }],
    ],
    [
      'assignees',
      [
        { value: 'krillin', operator: '=' },
        { value: 'piccolo', operator: '!=' },
      ],
      [
        { type: 'assignees', value: { data: 'krillin', operator: '=' } },
        { type: 'assignees', value: { data: 'piccolo', operator: '!=' } },
      ],
    ],
    [
      'foo',
      [
        { value: 'bar', operator: '!=' },
        { value: 'baz', operator: '!=' },
      ],
      [
        { type: 'foo', value: { data: 'bar', operator: '!=' } },
        { type: 'foo', value: { data: 'baz', operator: '!=' } },
      ],
    ],
  ])('gathers %s=%j into result=%j', (token, value, result) => {
    const res = prepareTokens({ [token]: value });
    expect(res).toEqual(result);
  });
});

describe('processFilters', () => {
  it('processes multiple filter values', () => {
    const result = processFilters([
      { type: 'foo', value: { data: 'foo', operator: '=' } },
      { type: 'bar', value: { data: 'bar1', operator: '=' } },
      { type: 'bar', value: { data: 'bar2', operator: '!=' } },
    ]);

    expect(result).toStrictEqual({
      foo: [{ value: 'foo', operator: '=' }],
      bar: [
        { value: 'bar1', operator: '=' },
        { value: 'bar2', operator: '!=' },
      ],
    });
  });

  it('does not remove wrapping double quotes from the data', () => {
    const result = processFilters([
      { type: 'foo', value: { data: '"value with spaces"', operator: '=' } },
    ]);

    expect(result).toStrictEqual({
      foo: [{ value: '"value with spaces"', operator: '=' }],
    });
  });
});

describe('filterToQueryObject', () => {
  describe('with empty data', () => {
    it('returns an empty object', () => {
      expect(filterToQueryObject()).toEqual({});
      expect(filterToQueryObject({})).toEqual({});
      expect(filterToQueryObject({ author_username: null, label_name: [] })).toEqual({
        author_username: null,
        label_name: null,
        'not[author_username]': null,
        'not[label_name]': null,
      });
    });
  });

  it.each([
    [
      'author_username',
      { value: 'v1.0', operator: '=' },
      { author_username: 'v1.0', 'not[author_username]': null },
    ],
    [
      'author_username',
      { value: 'v1.0', operator: '!=' },
      { author_username: null, 'not[author_username]': 'v1.0' },
    ],
    [
      'label_name',
      [{ value: 'z-fighters', operator: '=' }],
      { label_name: ['z-fighters'], 'not[label_name]': null },
    ],
    [
      'label_name',
      [{ value: 'z-fighters', operator: '!=' }],
      { label_name: null, 'not[label_name]': ['z-fighters'] },
    ],
    [
      'foo',
      [
        { value: 'bar', operator: '=' },
        { value: 'baz', operator: '=' },
      ],
      { foo: ['bar', 'baz'], 'not[foo]': null },
    ],
    [
      'foo',
      [
        { value: 'bar', operator: '!=' },
        { value: 'baz', operator: '!=' },
      ],
      { foo: null, 'not[foo]': ['bar', 'baz'] },
    ],
    [
      'foo',
      [
        { value: 'bar', operator: '!=' },
        { value: 'baz', operator: '=' },
      ],
      { foo: ['baz'], 'not[foo]': ['bar'] },
    ],
  ])('gathers filter values %s=%j into query object=%j', (token, value, result) => {
    const res = filterToQueryObject({ [token]: value });
    expect(res).toEqual(result);
  });
});

describe('urlQueryToFilter', () => {
  describe('with empty data', () => {
    it('returns an empty object', () => {
      expect(urlQueryToFilter()).toEqual({});
      expect(urlQueryToFilter('')).toEqual({});
      expect(urlQueryToFilter('author_username=&milestone_title=&')).toEqual({});
    });
  });

  it.each([
    ['author_username=v1.0', { author_username: { value: 'v1.0', operator: '=' } }],
    ['not[author_username]=v1.0', { author_username: { value: 'v1.0', operator: '!=' } }],
    ['foo=bar&foo=baz', { foo: { value: 'baz', operator: '=' } }],
    ['foo=bar&foo[]=baz', { foo: [{ value: 'baz', operator: '=' }] }],
    ['not[foo]=bar&foo=baz', { foo: { value: 'baz', operator: '=' } }],
    [
      'foo[]=bar&foo[]=baz&not[foo]=',
      {
        foo: [
          { value: 'bar', operator: '=' },
          { value: 'baz', operator: '=' },
        ],
      },
    ],
    [
      'foo[]=&not[foo][]=bar&not[foo][]=baz',
      {
        foo: [
          { value: 'bar', operator: '!=' },
          { value: 'baz', operator: '!=' },
        ],
      },
    ],
    [
      'foo[]=baz&not[foo][]=bar',
      {
        foo: [
          { value: 'baz', operator: '=' },
          { value: 'bar', operator: '!=' },
        ],
      },
    ],
    ['not[foo][]=bar', { foo: [{ value: 'bar', operator: '!=' }] }],
  ])('gathers filter values %s into query object=%j', (query, result) => {
    const res = urlQueryToFilter(query);
    expect(res).toEqual(result);
  });
});