summaryrefslogtreecommitdiff
path: root/spec/frontend/filterable_list_spec.js
blob: 67d18611661e96999654ebd6e6ec98659d976707 (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
import FilterableList from '~/filterable_list';
import { getJSONFixture, setHTMLFixture } from './helpers/fixtures';

describe('FilterableList', () => {
  let List;
  let form;
  let filter;
  let holder;

  beforeEach(() => {
    setHTMLFixture(`
      <form id="project-filter-form">
        <input name="name" class="js-projects-list-filter" />
      </div>
      <div class="js-projects-list-holder"></div>
    `);
    getJSONFixture('static/projects.json');
    form = document.querySelector('form#project-filter-form');
    filter = document.querySelector('.js-projects-list-filter');
    holder = document.querySelector('.js-projects-list-holder');
    List = new FilterableList(form, filter, holder);
  });

  it('processes input parameters', () => {
    expect(List.filterForm).toEqual(form);
    expect(List.listFilterElement).toEqual(filter);
    expect(List.listHolderElement).toEqual(holder);
  });

  describe('getPagePath', () => {
    it('returns properly constructed base endpoint', () => {
      List.filterForm.action = '/foo/bar/';
      List.listFilterElement.value = 'blah';

      expect(List.getPagePath()).toEqual('/foo/bar/?name=blah');
    });

    it('properly appends custom parameters to existing URL', () => {
      List.filterForm.action = '/foo/bar?alpha=beta';
      List.listFilterElement.value = 'blah';

      expect(List.getPagePath()).toEqual('/foo/bar?alpha=beta&name=blah');
    });
  });

  describe('getFilterEndpoint', () => {
    it('returns getPagePath by default', () => {
      jest.spyOn(List, 'getPagePath').mockReturnValue('blah/blah/foo');

      expect(List.getFilterEndpoint()).toEqual(List.getPagePath());
    });
  });
});