summaryrefslogtreecommitdiff
path: root/spec/javascripts/issuable_spec.js
blob: 45f55395d3a81004e952a47ab6e19428cd9c2926 (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
/* global IssuableIndex */

import '~/lib/utils/url_utility';
import '~/issuable_index';

(() => {
  const BASE_URL = '/user/project/issues?scope=all&state=closed';
  const DEFAULT_PARAMS = '&utf8=%E2%9C%93';

  function updateForm(formValues, form) {
    $.each(formValues, (id, value) => {
      $(`#${id}`, form).val(value);
    });
  }

  function resetForm(form) {
    $('input[name!="utf8"]', form).each((index, input) => {
      input.setAttribute('value', '');
    });
  }

  describe('Issuable', () => {
    preloadFixtures('static/issuable_filter.html.raw');

    beforeEach(() => {
      loadFixtures('static/issuable_filter.html.raw');
      IssuableIndex.init();
    });

    it('should be defined', () => {
      expect(window.IssuableIndex).toBeDefined();
    });

    describe('filtering', () => {
      let $filtersForm;

      beforeEach(() => {
        $filtersForm = $('.js-filter-form');
        loadFixtures('static/issuable_filter.html.raw');
        resetForm($filtersForm);
      });

      it('should contain only the default parameters', () => {
        spyOn(gl.utils, 'visitUrl');

        IssuableIndex.filterResults($filtersForm);

        expect(gl.utils.visitUrl).toHaveBeenCalledWith(BASE_URL + DEFAULT_PARAMS);
      });

      it('should filter for the phrase "broken"', () => {
        spyOn(gl.utils, 'visitUrl');

        updateForm({ search: 'broken' }, $filtersForm);
        IssuableIndex.filterResults($filtersForm);
        const params = `${DEFAULT_PARAMS}&search=broken`;

        expect(gl.utils.visitUrl).toHaveBeenCalledWith(BASE_URL + params);
      });

      it('should keep query parameters after modifying filter', () => {
        spyOn(gl.utils, 'visitUrl');

        // initial filter
        updateForm({ milestone_title: 'v1.0' }, $filtersForm);

        IssuableIndex.filterResults($filtersForm);
        let params = `${DEFAULT_PARAMS}&milestone_title=v1.0`;
        expect(gl.utils.visitUrl).toHaveBeenCalledWith(BASE_URL + params);

        // update filter
        updateForm({ label_name: 'Frontend' }, $filtersForm);

        IssuableIndex.filterResults($filtersForm);
        params = `${DEFAULT_PARAMS}&milestone_title=v1.0&label_name=Frontend`;
        expect(gl.utils.visitUrl).toHaveBeenCalledWith(BASE_URL + params);
      });
    });
  });
})();