summaryrefslogtreecommitdiff
path: root/spec/frontend/search/index_spec.js
blob: 31b5aa3686b27857ca8f4c43e30ba36b8392b886 (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
import { initSearchApp } from '~/search';
import createStore from '~/search/store';

jest.mock('~/search/store');
jest.mock('~/search/topbar');
jest.mock('~/search/sidebar');

describe('initSearchApp', () => {
  let defaultLocation;

  const setUrl = query => {
    window.location.href = `https://localhost:3000/search${query}`;
    window.location.search = query;
  };

  beforeEach(() => {
    defaultLocation = window.location;
    Object.defineProperty(window, 'location', {
      writable: true,
      value: { href: '', search: '' },
    });
  });

  afterEach(() => {
    window.location = defaultLocation;
  });

  describe.each`
    search                           | decodedSearch
    ${'test'}                        | ${'test'}
    ${'%2520'}                       | ${'%20'}
    ${'test%2Bthis%2Bstuff'}         | ${'test+this+stuff'}
    ${'test+this+stuff'}             | ${'test this stuff'}
    ${'test+%2B+this+%2B+stuff'}     | ${'test + this + stuff'}
    ${'test%2B+%2Bthis%2B+%2Bstuff'} | ${'test+ +this+ +stuff'}
    ${'test+%2520+this+%2520+stuff'} | ${'test %20 this %20 stuff'}
  `('parameter decoding', ({ search, decodedSearch }) => {
    beforeEach(() => {
      setUrl(`?search=${search}`);
      initSearchApp();
    });

    it(`decodes ${search} to ${decodedSearch}`, () => {
      expect(createStore).toHaveBeenCalledWith({ query: { search: decodedSearch } });
    });
  });
});