summaryrefslogtreecommitdiff
path: root/spec/frontend/jobs/components/job/manual_variables_form_spec.js
blob: 5806f9f75f9c815eb5ed5d13a236dc0b355aede0 (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
import { GlSprintf, GlLink } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import ManualVariablesForm from '~/jobs/components/job/manual_variables_form.vue';

Vue.use(Vuex);

describe('Manual Variables Form', () => {
  let wrapper;
  let store;

  const requiredProps = {
    action: {
      path: '/play',
      method: 'post',
      button_title: 'Trigger this manual action',
    },
  };

  const createComponent = (props = {}) => {
    store = new Vuex.Store({
      actions: {
        triggerManualJob: jest.fn(),
      },
    });

    wrapper = extendedWrapper(
      mount(ManualVariablesForm, {
        propsData: { ...requiredProps, ...props },
        store,
        stubs: {
          GlSprintf,
        },
      }),
    );
  };

  const findHelpText = () => wrapper.findComponent(GlSprintf);
  const findHelpLink = () => wrapper.findComponent(GlLink);

  const findTriggerBtn = () => wrapper.findByTestId('trigger-manual-job-btn');
  const findDeleteVarBtn = () => wrapper.findByTestId('delete-variable-btn');
  const findAllDeleteVarBtns = () => wrapper.findAllByTestId('delete-variable-btn');
  const findDeleteVarBtnPlaceholder = () => wrapper.findByTestId('delete-variable-btn-placeholder');
  const findCiVariableKey = () => wrapper.findByTestId('ci-variable-key');
  const findAllCiVariableKeys = () => wrapper.findAllByTestId('ci-variable-key');
  const findCiVariableValue = () => wrapper.findByTestId('ci-variable-value');
  const findAllVariables = () => wrapper.findAllByTestId('ci-variable-row');

  const setCiVariableKey = () => {
    findCiVariableKey().setValue('new key');
    findCiVariableKey().vm.$emit('change');
    nextTick();
  };

  const setCiVariableKeyByPosition = (position, value) => {
    findAllCiVariableKeys().at(position).setValue(value);
    findAllCiVariableKeys().at(position).vm.$emit('change');
    nextTick();
  };

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

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

  it('creates a new variable when user enters a new key value', async () => {
    expect(findAllVariables()).toHaveLength(1);

    await setCiVariableKey();

    expect(findAllVariables()).toHaveLength(2);
  });

  it('does not create extra empty variables', async () => {
    expect(findAllVariables()).toHaveLength(1);

    await setCiVariableKey();

    expect(findAllVariables()).toHaveLength(2);

    await setCiVariableKey();

    expect(findAllVariables()).toHaveLength(2);
  });

  it('removes the correct variable row', async () => {
    const variableKeyNameOne = 'key-one';
    const variableKeyNameThree = 'key-three';

    await setCiVariableKeyByPosition(0, variableKeyNameOne);

    await setCiVariableKeyByPosition(1, 'key-two');

    await setCiVariableKeyByPosition(2, variableKeyNameThree);

    expect(findAllVariables()).toHaveLength(4);

    await findAllDeleteVarBtns().at(1).trigger('click');

    expect(findAllVariables()).toHaveLength(3);

    expect(findAllCiVariableKeys().at(0).element.value).toBe(variableKeyNameOne);
    expect(findAllCiVariableKeys().at(1).element.value).toBe(variableKeyNameThree);
    expect(findAllCiVariableKeys().at(2).element.value).toBe('');
  });

  it('trigger button is disabled after trigger action', async () => {
    expect(findTriggerBtn().props('disabled')).toBe(false);

    await findTriggerBtn().trigger('click');

    expect(findTriggerBtn().props('disabled')).toBe(true);
  });

  it('delete variable button should only show when there is more than one variable', async () => {
    expect(findDeleteVarBtn().exists()).toBe(false);

    await setCiVariableKey();

    expect(findDeleteVarBtn().exists()).toBe(true);
  });

  it('delete variable button placeholder should only exist when a user cannot remove', async () => {
    expect(findDeleteVarBtnPlaceholder().exists()).toBe(true);
  });

  it('renders help text with provided link', () => {
    expect(findHelpText().exists()).toBe(true);
    expect(findHelpLink().attributes('href')).toBe(
      '/help/ci/variables/index#add-a-cicd-variable-to-a-project',
    );
  });

  it('passes variables in correct format', async () => {
    jest.spyOn(store, 'dispatch');

    await setCiVariableKey();

    await findCiVariableValue().setValue('new value');

    await findTriggerBtn().trigger('click');

    expect(store.dispatch).toHaveBeenCalledWith('triggerManualJob', [
      {
        key: 'new key',
        secret_value: 'new value',
      },
    ]);
  });
});