summaryrefslogtreecommitdiff
path: root/spec/javascripts/pipelines/header_component_spec.js
blob: 9043f30397de3fd23e1fe104b2f59e2213b18dba (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
import Vue from 'vue';
import headerComponent from '~/pipelines/components/header_component.vue';
import eventHub from '~/pipelines/event_hub';

describe('Pipeline details header', () => {
  let HeaderComponent;
  let vm;
  let props;

  beforeEach(() => {
    spyOn(eventHub, '$emit');
    HeaderComponent = Vue.extend(headerComponent);

    const threeWeeksAgo = new Date();
    threeWeeksAgo.setDate(threeWeeksAgo.getDate() - 21);

    props = {
      pipeline: {
        details: {
          status: {
            group: 'failed',
            icon: 'status_failed',
            label: 'failed',
            text: 'failed',
            details_path: 'path',
          },
        },
        id: 123,
        created_at: threeWeeksAgo.toISOString(),
        user: {
          web_url: 'path',
          name: 'Foo',
          username: 'foobar',
          email: 'foo@bar.com',
          avatar_url: 'link',
        },
        retry_path: 'retry',
        cancel_path: 'cancel',
        delete_path: 'delete',
      },
      isLoading: false,
    };

    vm = new HeaderComponent({ propsData: props }).$mount();
  });

  afterEach(() => {
    eventHub.$off();
    vm.$destroy();
  });

  const findDeleteModal = () => document.getElementById(headerComponent.DELETE_MODAL_ID);
  const findDeleteModalSubmit = () =>
    [...findDeleteModal().querySelectorAll('.btn')].find(x => x.textContent === 'Delete pipeline');

  it('should render provided pipeline info', () => {
    expect(
      vm.$el
        .querySelector('.header-main-content')
        .textContent.replace(/\s+/g, ' ')
        .trim(),
    ).toContain('failed Pipeline #123 triggered 3 weeks ago by Foo');
  });

  describe('action buttons', () => {
    it('should not trigger eventHub when nothing happens', () => {
      expect(eventHub.$emit).not.toHaveBeenCalled();
    });

    it('should call postAction when retry button action is clicked', () => {
      vm.$el.querySelector('.js-retry-button').click();

      expect(eventHub.$emit).toHaveBeenCalledWith('headerPostAction', 'retry');
    });

    it('should call postAction when cancel button action is clicked', () => {
      vm.$el.querySelector('.js-btn-cancel-pipeline').click();

      expect(eventHub.$emit).toHaveBeenCalledWith('headerPostAction', 'cancel');
    });

    it('does not show delete modal', () => {
      expect(findDeleteModal()).not.toBeVisible();
    });

    describe('when delete button action is clicked', () => {
      beforeEach(done => {
        vm.$el.querySelector('.js-btn-delete-pipeline').click();

        // Modal needs two ticks to show
        vm.$nextTick()
          .then(() => vm.$nextTick())
          .then(done)
          .catch(done.fail);
      });

      it('should show delete modal', () => {
        expect(findDeleteModal()).toBeVisible();
      });

      it('should call delete when modal is submitted', () => {
        findDeleteModalSubmit().click();

        expect(eventHub.$emit).toHaveBeenCalledWith('headerDeleteAction', 'delete');
      });
    });
  });
});