summaryrefslogtreecommitdiff
path: root/spec/frontend/pipelines/header_component_spec.js
blob: 5388d624d3cba1ee302e2dfc15f31f1740eabab6 (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
109
110
111
112
113
114
115
116
import { shallowMount } from '@vue/test-utils';
import { GlModal } from '@gitlab/ui';
import HeaderComponent from '~/pipelines/components/header_component.vue';
import CiHeader from '~/vue_shared/components/header_ci_component.vue';
import eventHub from '~/pipelines/event_hub';

describe('Pipeline details header', () => {
  let wrapper;
  let glModalDirective;

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

  const findDeleteModal = () => wrapper.find(GlModal);

  const defaultProps = {
    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,
  };

  const createComponent = (props = {}) => {
    glModalDirective = jest.fn();

    wrapper = shallowMount(HeaderComponent, {
      propsData: {
        ...props,
      },
      directives: {
        glModal: {
          bind(el, { value }) {
            glModalDirective(value);
          },
        },
      },
    });
  };

  beforeEach(() => {
    jest.spyOn(eventHub, '$emit');

    createComponent(defaultProps);
  });

  afterEach(() => {
    eventHub.$off();

    wrapper.destroy();
    wrapper = null;
  });

  it('should render provided pipeline info', () => {
    expect(wrapper.find(CiHeader).props()).toMatchObject({
      status: defaultProps.pipeline.details.status,
      itemId: defaultProps.pipeline.id,
      time: defaultProps.pipeline.created_at,
      user: defaultProps.pipeline.user,
    });
  });

  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', () => {
      wrapper.find('[data-testid="retryButton"]').vm.$emit('click');

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

    it('should call postAction when cancel button action is clicked', () => {
      wrapper.find('[data-testid="cancelPipeline"]').vm.$emit('click');

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

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

    describe('when delete button action is clicked', () => {
      it('displays delete modal', () => {
        expect(findDeleteModal().props('modalId')).toBe(wrapper.vm.$options.DELETE_MODAL_ID);
        expect(glModalDirective).toHaveBeenCalledWith(wrapper.vm.$options.DELETE_MODAL_ID);
      });

      it('should call delete when modal is submitted', () => {
        findDeleteModal().vm.$emit('ok');

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