summaryrefslogtreecommitdiff
path: root/spec/javascripts/mini_pipeline_graph_dropdown_spec.js
blob: 009b3fd75b7145dceeeb932e4dfe49325bdbf4a9 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* eslint-disable no-new */

import $ from 'jquery';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import MiniPipelineGraph from '~/mini_pipeline_graph_dropdown';
import timeoutPromise from './helpers/set_timeout_promise_helper';

describe('Mini Pipeline Graph Dropdown', () => {
  preloadFixtures('static/mini_dropdown_graph.html.raw');

  beforeEach(() => {
    loadFixtures('static/mini_dropdown_graph.html.raw');
  });

  describe('When is initialized', () => {
    it('should initialize without errors when no options are given', () => {
      const miniPipelineGraph = new MiniPipelineGraph();

      expect(miniPipelineGraph.dropdownListSelector).toEqual('.js-builds-dropdown-container');
    });

    it('should set the container as the given prop', () => {
      const container = '.foo';

      const miniPipelineGraph = new MiniPipelineGraph({ container });

      expect(miniPipelineGraph.container).toEqual(container);
    });
  });

  describe('When dropdown is clicked', () => {
    let mock;

    beforeEach(() => {
      mock = new MockAdapter(axios);
    });

    afterEach(() => {
      mock.restore();
    });

    it('should call getBuildsList', () => {
      const getBuildsListSpy = spyOn(
        MiniPipelineGraph.prototype,
        'getBuildsList',
      ).and.callFake(function () {});

      new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents();

      document.querySelector('.js-builds-dropdown-button').click();

      expect(getBuildsListSpy).toHaveBeenCalled();
    });

    it('should make a request to the endpoint provided in the html', () => {
      const ajaxSpy = spyOn(axios, 'get').and.callThrough();

      mock.onGet('foobar').reply(200, {
        html: '',
      });

      new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents();

      document.querySelector('.js-builds-dropdown-button').click();
      expect(ajaxSpy.calls.allArgs()[0][0]).toEqual('foobar');
    });

    it('should not close when user uses cmd/ctrl + click', (done) => {
      mock.onGet('foobar').reply(200, {
        html: `<li>
          <a class="mini-pipeline-graph-dropdown-item" href="#">
            <span class="ci-status-icon ci-status-icon-failed"></span>
            <span class="ci-build-text">build</span>
          </a>
          <a class="ci-action-icon-wrapper js-ci-action-icon" href="#"></a>
        </li>`,
      });
      new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents();

      document.querySelector('.js-builds-dropdown-button').click();

      timeoutPromise()
        .then(() => {
          document.querySelector('a.mini-pipeline-graph-dropdown-item').click();
        })
        .then(timeoutPromise)
        .then(() => {
          expect($('.js-builds-dropdown-list').is(':visible')).toEqual(true);
        })
        .then(done)
        .catch(done.fail);
    });

    it('should close the dropdown when request returns an error', (done) => {
      mock.onGet('foobar').networkError();

      new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents();

      document.querySelector('.js-builds-dropdown-button').click();

      setTimeout(() => {
        expect($('.js-builds-dropdown-tests .dropdown').hasClass('open')).toEqual(false);
        done();
      });
    });
  });
});