summaryrefslogtreecommitdiff
path: root/spec/frontend/activities_spec.js
blob: ebace21217a418fad714d3ea0f8d81c3686cb8a2 (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
/* eslint-disable no-unused-expressions, no-prototype-builtins, no-new, no-shadow */

import $ from 'jquery';
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import Activities from '~/activities';
import Pager from '~/pager';

describe('Activities', () => {
  window.gon || (window.gon = {});
  const fixtureTemplate = 'static/event_filter.html';
  const filters = [
    {
      id: 'all',
    },
    {
      id: 'push',
      name: 'push events',
    },
    {
      id: 'merged',
      name: 'merge events',
    },
    {
      id: 'comments',
    },
    {
      id: 'team',
    },
  ];

  function getEventName(index) {
    const filter = filters[index];
    return filter.hasOwnProperty('name') ? filter.name : filter.id;
  }

  function getSelector(index) {
    const filter = filters[index];
    return `#${filter.id}_event_filter`;
  }

  beforeEach(() => {
    loadHTMLFixture(fixtureTemplate);
    jest.spyOn(Pager, 'init').mockImplementation(() => {});
    new Activities();
  });

  afterEach(() => {
    resetHTMLFixture();
  });

  for (let i = 0; i < filters.length; i += 1) {
    ((i) => {
      describe(`when selecting ${getEventName(i)}`, () => {
        beforeEach(() => {
          $(getSelector(i)).click();
        });

        for (let x = 0; x < filters.length; x += 1) {
          ((x) => {
            const shouldHighlight = i === x;
            const testName = shouldHighlight ? 'should highlight' : 'should not highlight';

            it(`${testName} ${getEventName(x)}`, () => {
              expect($(getSelector(x)).parent().hasClass('active')).toEqual(shouldHighlight);
            });
          })(x);
        }
      });
    })(i);
  }
});