summaryrefslogtreecommitdiff
path: root/spec/frontend/environments/environment_details/components/deployment_actions_spec.js
blob: a0eb4c494e67bf7ad29648ae8fe01f0def00ad83 (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
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { GlButton } from '@gitlab/ui';
import DeploymentActions from '~/environments/environment_details/components/deployment_actions.vue';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { translations } from '~/environments/environment_details/constants';
import ActionsComponent from '~/environments/components/environment_actions.vue';

describe('~/environments/environment_details/components/deployment_actions.vue', () => {
  Vue.use(VueApollo);
  let wrapper;

  const actionsData = [
    {
      playable: true,
      playPath: 'http://www.example.com/play',
      name: 'deploy-staging',
      scheduledAt: '2023-01-18T08:50:08.390Z',
    },
  ];

  const rollbackData = {
    id: '123',
    name: 'enironment-name',
    lastDeployment: {
      commit: {
        shortSha: 'abcd1234',
      },
      isLast: true,
    },
    retryUrl: 'deployment/retry',
  };

  const mockSetEnvironmentToRollback = jest.fn();
  const mockResolvers = {
    Mutation: {
      setEnvironmentToRollback: mockSetEnvironmentToRollback,
    },
  };
  const createWrapper = ({ actions, rollback, approvalEnvironment }) => {
    const mockApollo = createMockApollo([], mockResolvers);
    return mountExtended(DeploymentActions, {
      apolloProvider: mockApollo,
      provide: {
        projectPath: 'fullProjectPath',
      },
      propsData: {
        actions,
        rollback,
        approvalEnvironment,
      },
    });
  };

  const findRollbackButton = () => wrapper.findComponent(GlButton);

  describe('deployment actions', () => {
    describe('when there is no actions provided', () => {
      beforeEach(() => {
        wrapper = createWrapper({ actions: [] });
      });

      it('should not render actions component', () => {
        const actionsComponent = wrapper.findComponent(ActionsComponent);
        expect(actionsComponent.exists()).toBe(false);
      });
    });

    describe('when there are actions provided', () => {
      beforeEach(() => {
        wrapper = createWrapper({ actions: actionsData });
      });

      it('should render actions component', () => {
        const actionsComponent = wrapper.findComponent(ActionsComponent);
        expect(actionsComponent.exists()).toBe(true);
        expect(actionsComponent.props().actions).toBe(actionsData);
      });
    });
  });

  describe('rollback action', () => {
    describe('when there is no rollback data available', () => {
      it('should not show a rollback button', () => {
        wrapper = createWrapper({ actions: [] });
        const button = findRollbackButton();
        expect(button.exists()).toBe(false);
      });
    });

    describe.each([
      { isLast: true, buttonTitle: translations.redeployButtonTitle, icon: 'repeat' },
      { isLast: false, buttonTitle: translations.rollbackButtonTitle, icon: 'redo' },
    ])(
      `when there is a rollback data available and the deployment isLast=$isLast`,
      ({ isLast, buttonTitle, icon }) => {
        let rollback;
        beforeEach(() => {
          const lastDeployment = { ...rollbackData.lastDeployment, isLast };
          rollback = { ...rollbackData };
          rollback.lastDeployment = lastDeployment;
          wrapper = createWrapper({ actions: [], rollback });
        });

        it('should show the rollback button', () => {
          const button = findRollbackButton();
          expect(button.exists()).toBe(true);
        });

        it(`the rollback button should have "${icon}" icon`, () => {
          const button = findRollbackButton();
          expect(button.props().icon).toBe(icon);
        });

        it(`the rollback button should have "${buttonTitle}" title`, () => {
          const button = findRollbackButton();
          expect(button.attributes().title).toBe(buttonTitle);
        });

        it(`the rollback button click should send correct mutation`, async () => {
          const button = findRollbackButton();
          button.vm.$emit('click');
          await waitForPromises();
          expect(mockSetEnvironmentToRollback).toHaveBeenCalledWith(
            expect.anything(),
            { environment: rollback },
            expect.anything(),
            expect.anything(),
          );
        });
      },
    );
  });
});