mini_pipeline_graph_dropdown_spec.js 3.3 KB
Newer Older
1 2
/* eslint-disable no-new */

3
import $ from 'jquery';
4 5
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
6
import MiniPipelineGraph from '~/mini_pipeline_graph_dropdown';
7
import timeoutPromise from './helpers/set_timeout_promise_helper';
8

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

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

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

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

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

26
      const miniPipelineGraph = new MiniPipelineGraph({ container });
27

28
      expect(miniPipelineGraph.container).toEqual(container);
29
    });
30
  });
31

32
  describe('When dropdown is clicked', () => {
33 34 35 36 37 38 39 40 41 42
    let mock;

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

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

43 44 45 46 47
    it('should call getBuildsList', () => {
      const getBuildsListSpy = spyOn(
        MiniPipelineGraph.prototype,
        'getBuildsList',
      ).and.callFake(function () {});
48

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

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

53 54
      expect(getBuildsListSpy).toHaveBeenCalled();
    });
55

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

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

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

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

69 70 71 72 73 74 75 76 77
    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>`,
78 79
      });
      new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents();
80

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

83 84 85 86 87 88 89 90 91 92
      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);
93
    });
94

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

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

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

102 103 104 105 106
      setTimeout(() => {
        expect($('.js-builds-dropdown-tests .dropdown').hasClass('open')).toEqual(false);
        done();
      });
    });
107 108
  });
});