Welcome to mirror list, hosted at ThFree Co, Russian Federation.

environment_actions_spec.js « environments « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ada79e2d4155777f47591e1fc648438f9cd9ce3c (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { GlDropdown, GlDropdownItem, GlLoadingIcon, GlIcon } from '@gitlab/ui';
import { shallowMount, mount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import { TEST_HOST } from 'helpers/test_constants';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
import EnvironmentActions from '~/environments/components/environment_actions.vue';
import eventHub from '~/environments/event_hub';
import actionMutation from '~/environments/graphql/mutations/action.mutation.graphql';
import { confirmAction } from '~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal';
import createMockApollo from 'helpers/mock_apollo_helper';

jest.mock('~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal', () => {
  return {
    confirmAction: jest.fn(),
  };
});

const scheduledJobAction = {
  name: 'scheduled action',
  playPath: `${TEST_HOST}/scheduled/job/action`,
  playable: true,
  scheduledAt: '2063-04-05T00:42:00Z',
};

const expiredJobAction = {
  name: 'expired action',
  playPath: `${TEST_HOST}/expired/job/action`,
  playable: true,
  scheduledAt: '2018-10-05T08:23:00Z',
};

describe('EnvironmentActions Component', () => {
  let wrapper;

  const findEnvironmentActionsButton = () =>
    wrapper.find('[data-testid="environment-actions-button"]');

  function createComponent(props, { mountFn = shallowMount, options = {} } = {}) {
    wrapper = mountFn(EnvironmentActions, {
      propsData: { actions: [], ...props },
      directives: {
        GlTooltip: createMockDirective(),
      },
      ...options,
    });
  }

  function createComponentWithScheduledJobs(opts = {}) {
    return createComponent({ actions: [scheduledJobAction, expiredJobAction] }, opts);
  }

  const findDropdownItem = (action) => {
    const buttons = wrapper.findAll(GlDropdownItem);
    return buttons.filter((button) => button.text().startsWith(action.name)).at(0);
  };

  afterEach(() => {
    wrapper.destroy();
    confirmAction.mockReset();
  });

  it('should render a dropdown button with 2 icons', () => {
    createComponent({}, { mountFn: mount });
    expect(wrapper.find(GlDropdown).findAll(GlIcon).length).toBe(2);
  });

  it('should render a dropdown button with aria-label description', () => {
    createComponent();
    expect(wrapper.find(GlDropdown).attributes('aria-label')).toBe('Deploy to...');
  });

  it('should render a tooltip', () => {
    createComponent();
    const tooltip = getBinding(findEnvironmentActionsButton().element, 'gl-tooltip');
    expect(tooltip).toBeDefined();
  });

  describe('manual actions', () => {
    const actions = [
      {
        name: 'bar',
        play_path: 'https://gitlab.com/play',
      },
      {
        name: 'foo',
        play_path: '#',
      },
      {
        name: 'foo bar',
        play_path: 'url',
        playable: false,
      },
    ];

    beforeEach(() => {
      createComponent({ actions });
    });

    it('should render a dropdown with the provided list of actions', () => {
      expect(wrapper.findAll(GlDropdownItem)).toHaveLength(actions.length);
    });

    it("should render a disabled action when it's not playable", () => {
      const dropdownItems = wrapper.findAll(GlDropdownItem);
      const lastDropdownItem = dropdownItems.at(dropdownItems.length - 1);
      expect(lastDropdownItem.attributes('disabled')).toBe('true');
    });
  });

  describe('scheduled jobs', () => {
    let emitSpy;

    const clickAndConfirm = async ({ confirm = true } = {}) => {
      confirmAction.mockResolvedValueOnce(confirm);

      findDropdownItem(scheduledJobAction).vm.$emit('click');
      await nextTick();
    };

    beforeEach(() => {
      emitSpy = jest.fn();
      eventHub.$on('postAction', emitSpy);
      jest.spyOn(Date, 'now').mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime());
    });

    describe('when postAction event is confirmed', () => {
      beforeEach(async () => {
        createComponentWithScheduledJobs({ mountFn: mount });
        clickAndConfirm();
      });

      it('emits postAction event', () => {
        expect(confirmAction).toHaveBeenCalled();
        expect(emitSpy).toHaveBeenCalledWith({ endpoint: scheduledJobAction.playPath });
      });

      it('should render a dropdown button with a loading icon', () => {
        expect(wrapper.find(GlLoadingIcon).isVisible()).toBe(true);
      });
    });

    describe('when postAction event is denied', () => {
      beforeEach(async () => {
        createComponentWithScheduledJobs({ mountFn: mount });
        clickAndConfirm({ confirm: false });
      });

      it('does not emit postAction event if confirmation is cancelled', () => {
        expect(confirmAction).toHaveBeenCalled();
        expect(emitSpy).not.toHaveBeenCalled();
      });
    });

    it('displays the remaining time in the dropdown', () => {
      createComponentWithScheduledJobs();
      expect(findDropdownItem(scheduledJobAction).text()).toContain('24:00:00');
    });

    it('displays 00:00:00 for expired jobs in the dropdown', () => {
      createComponentWithScheduledJobs();
      expect(findDropdownItem(expiredJobAction).text()).toContain('00:00:00');
    });
  });

  describe('graphql', () => {
    Vue.use(VueApollo);

    const action = {
      name: 'bar',
      play_path: 'https://gitlab.com/play',
    };

    let mockApollo;

    beforeEach(() => {
      mockApollo = createMockApollo();
      createComponent(
        { actions: [action], graphql: true },
        { options: { apolloProvider: mockApollo } },
      );
    });

    it('should trigger a graphql mutation on click', () => {
      jest.spyOn(mockApollo.defaultClient, 'mutate');
      findDropdownItem(action).vm.$emit('click');
      expect(mockApollo.defaultClient.mutate).toHaveBeenCalledWith({
        mutation: actionMutation,
        variables: { action },
      });
    });
  });
});