summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/filtered_search_bar/filtered_search_utils_spec.js
blob: b2ed79cd75ac0c9c7b03d9d50a3fb2d38b24e6d0 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import { useLocalStorageSpy } from 'helpers/local_storage_helper';

import AccessorUtilities from '~/lib/utils/accessor';
import {
  stripQuotes,
  uniqueTokens,
  prepareTokens,
  processFilters,
  filterToQueryObject,
  urlQueryToFilter,
  getRecentlyUsedTokenValues,
  setTokenValueToRecentlyUsed,
} from '~/vue_shared/components/filtered_search_bar/filtered_search_utils';

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

const mockStorageKey = 'recent-tokens';

function setLocalStorageAvailability(isAvailable) {
  jest.spyOn(AccessorUtilities, 'isLocalStorageAccessSafe').mockReturnValue(isAvailable);
}

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);
  });
});

describe('getRecentlyUsedTokenValues', () => {
  useLocalStorageSpy();

  beforeEach(() => {
    localStorage.removeItem(mockStorageKey);
  });

  it('returns array containing recently used token values from provided recentTokenValuesStorageKey', () => {
    setLocalStorageAvailability(true);

    const mockExpectedArray = [{ foo: 'bar' }];
    localStorage.setItem(mockStorageKey, JSON.stringify(mockExpectedArray));

    expect(getRecentlyUsedTokenValues(mockStorageKey)).toEqual(mockExpectedArray);
  });

  it('returns empty array when provided recentTokenValuesStorageKey does not have anything in localStorage', () => {
    setLocalStorageAvailability(true);

    expect(getRecentlyUsedTokenValues(mockStorageKey)).toEqual([]);
  });

  it('returns empty array when when access to localStorage is not available', () => {
    setLocalStorageAvailability(false);

    expect(getRecentlyUsedTokenValues(mockStorageKey)).toEqual([]);
  });
});

describe('setTokenValueToRecentlyUsed', () => {
  const mockTokenValue1 = { foo: 'bar' };
  const mockTokenValue2 = { bar: 'baz' };
  useLocalStorageSpy();

  beforeEach(() => {
    localStorage.removeItem(mockStorageKey);
  });

  it('adds provided tokenValue to localStorage for recentTokenValuesStorageKey', () => {
    setLocalStorageAvailability(true);

    setTokenValueToRecentlyUsed(mockStorageKey, mockTokenValue1);

    expect(JSON.parse(localStorage.getItem(mockStorageKey))).toEqual([mockTokenValue1]);
  });

  it('adds provided tokenValue to localStorage at the top of existing values (i.e. Stack order)', () => {
    setLocalStorageAvailability(true);

    setTokenValueToRecentlyUsed(mockStorageKey, mockTokenValue1);
    setTokenValueToRecentlyUsed(mockStorageKey, mockTokenValue2);

    expect(JSON.parse(localStorage.getItem(mockStorageKey))).toEqual([
      mockTokenValue2,
      mockTokenValue1,
    ]);
  });

  it('ensures that provided tokenValue is not added twice', () => {
    setLocalStorageAvailability(true);

    setTokenValueToRecentlyUsed(mockStorageKey, mockTokenValue1);
    setTokenValueToRecentlyUsed(mockStorageKey, mockTokenValue1);

    expect(JSON.parse(localStorage.getItem(mockStorageKey))).toEqual([mockTokenValue1]);
  });

  it('does not add any value when acess to localStorage is not available', () => {
    setLocalStorageAvailability(false);

    setTokenValueToRecentlyUsed(mockStorageKey, mockTokenValue1);

    expect(JSON.parse(localStorage.getItem(mockStorageKey))).toBeNull();
  });
});