BigW Consortium Gitlab

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

3 4
import MiniPipelineGraph from '~/mini_pipeline_graph_dropdown';
import '~/flash';
5

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

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

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

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

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

23
      const miniPipelineGraph = new MiniPipelineGraph({ container });
24

25
      expect(miniPipelineGraph.container).toEqual(container);
26
    });
27
  });
28

29 30 31 32 33 34
  describe('When dropdown is clicked', () => {
    it('should call getBuildsList', () => {
      const getBuildsListSpy = spyOn(
        MiniPipelineGraph.prototype,
        'getBuildsList',
      ).and.callFake(function () {});
35

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

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

40 41
      expect(getBuildsListSpy).toHaveBeenCalled();
    });
42

43 44
    it('should make a request to the endpoint provided in the html', () => {
      const ajaxSpy = spyOn($, 'ajax').and.callFake(function () {});
45

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

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

52 53 54 55 56 57 58 59 60 61
    it('should not close when user uses cmd/ctrl + click', () => {
      spyOn($, 'ajax').and.callFake(function (params) {
        params.success({
          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>`,
62
        });
63 64
      });
      new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents();
65

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

68
      document.querySelector('a.mini-pipeline-graph-dropdown-item').click();
69

70
      expect($('.js-builds-dropdown-list').is(':visible')).toEqual(true);
71 72
    });
  });
73 74 75 76 77 78 79 80 81 82 83 84 85 86

  it('should close the dropdown when request returns an error', (done) => {
    spyOn($, 'ajax').and.callFake(options => options.error());

    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();
    }, 0);
  });
});