summaryrefslogtreecommitdiff
path: root/spec/frontend/pipeline_new/components/pipeline_new_form_spec.js
blob: d1e6b6b938ac7fc42b230037c36e1a0810691ddd (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 { mount, shallowMount } from '@vue/test-utils';
import { GlNewDropdown, GlNewDropdownItem, GlForm } from '@gitlab/ui';
import Api from '~/api';
import PipelineNewForm from '~/pipeline_new/components/pipeline_new_form.vue';
import { mockRefs, mockParams, mockPostParams, mockProjectId } from '../mock_data';

describe('Pipeline New Form', () => {
  let wrapper;

  const dummySubmitEvent = {
    preventDefault() {},
  };

  const findForm = () => wrapper.find(GlForm);
  const findDropdown = () => wrapper.find(GlNewDropdown);
  const findDropdownItems = () => wrapper.findAll(GlNewDropdownItem);
  const findVariableRows = () => wrapper.findAll('[data-testid="ci-variable-row"]');
  const findRemoveIcons = () => wrapper.findAll('[data-testid="remove-ci-variable-row"]');
  const findKeyInputs = () => wrapper.findAll('[data-testid="pipeline-form-ci-variable-key"]');

  const createComponent = (term = '', props = {}, method = shallowMount) => {
    wrapper = method(PipelineNewForm, {
      propsData: {
        projectId: mockProjectId,
        pipelinesPath: '',
        refs: mockRefs,
        defaultBranch: 'master',
        settingsLink: '',
        ...props,
      },
      data() {
        return {
          searchTerm: term,
        };
      },
    });
  };

  beforeEach(() => {
    jest.spyOn(Api, 'createPipeline').mockResolvedValue({ data: { web_url: '/' } });
  });

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

  describe('Dropdown with branches and tags', () => {
    it('displays dropdown with all branches and tags', () => {
      createComponent();
      expect(findDropdownItems().length).toBe(mockRefs.length);
    });

    it('when user enters search term the list is filtered', () => {
      createComponent('master');

      expect(findDropdownItems().length).toBe(1);
      expect(
        findDropdownItems()
          .at(0)
          .text(),
      ).toBe('master');
    });
  });

  describe('Form', () => {
    beforeEach(() => {
      createComponent('', mockParams, mount);
    });
    it('displays the correct values for the provided query params', () => {
      expect(findDropdown().props('text')).toBe('tag-1');

      return wrapper.vm.$nextTick().then(() => {
        expect(findVariableRows().length).toBe(3);
      });
    });

    it('does not display remove icon for last row', () => {
      expect(findRemoveIcons().length).toBe(2);
    });

    it('removes ci variable row on remove icon button click', () => {
      findRemoveIcons()
        .at(1)
        .trigger('click');

      return wrapper.vm.$nextTick().then(() => {
        expect(findVariableRows().length).toBe(2);
      });
    });

    it('creates a pipeline on submit', () => {
      findForm().vm.$emit('submit', dummySubmitEvent);

      expect(Api.createPipeline).toHaveBeenCalledWith(mockProjectId, mockPostParams);
    });

    it('creates blank variable on input change event', () => {
      findKeyInputs()
        .at(2)
        .trigger('change');

      return wrapper.vm.$nextTick().then(() => {
        expect(findVariableRows().length).toBe(4);
      });
    });
  });
});