summaryrefslogtreecommitdiff
path: root/spec/javascripts/commits_spec.js
blob: e5a5e3293b9e32ebd0a3cb20452cd591a42c3926 (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
import 'vendor/jquery.endless-scroll';
import '~/pager';
import CommitsList from '~/commits';

describe('Commits List', () => {
  beforeEach(() => {
    setFixtures(`
      <form class="commits-search-form" action="/h5bp/html5-boilerplate/commits/master">
        <input id="commits-search">
      </form>
      <ol id="commits-list"></ol>
      `);
  });

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

  describe('processCommits', () => {
    it('should join commit headers', () => {
      CommitsList.$contentList = $(`
        <div>
          <li class="commit-header" data-day="2016-09-20">
            <span class="day">20 Sep, 2016</span>
            <span class="commits-count">1 commit</span>
          </li>
          <li class="commit"></li>
        </div>
      `);

      const data = `
        <li class="commit-header" data-day="2016-09-20">
          <span class="day">20 Sep, 2016</span>
          <span class="commits-count">1 commit</span>
        </li>
        <li class="commit"></li>
      `;

      // The last commit header should be removed
      // since the previous one has the same data-day value.
      expect(CommitsList.processCommits(data).find('li.commit-header').length).toBe(0);
    });
  });

  describe('on entering input', () => {
    let ajaxSpy;

    beforeEach(() => {
      CommitsList.init(25);
      CommitsList.searchField.val('');

      spyOn(history, 'replaceState').and.stub();
      ajaxSpy = spyOn(jQuery, 'ajax').and.callFake((req) => {
        req.success({
          data: '<li>Result</li>',
        });
      });
    });

    it('should save the last search string', () => {
      CommitsList.searchField.val('GitLab');
      CommitsList.filterResults();
      expect(ajaxSpy).toHaveBeenCalled();
      expect(CommitsList.lastSearch).toEqual('GitLab');
    });

    it('should not make ajax call if the input does not change', () => {
      CommitsList.filterResults();
      expect(ajaxSpy).not.toHaveBeenCalled();
      expect(CommitsList.lastSearch).toEqual('');
    });
  });
});