summaryrefslogtreecommitdiff
path: root/spec/frontend/integrations/edit/components/dynamic_field_spec.js
blob: e5710641f812ba5b7666ddc5ebbef2a4f099f9ed (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
import { mount } from '@vue/test-utils';
import DynamicField from '~/integrations/edit/components/dynamic_field.vue';
import { GlFormGroup, GlFormCheckbox, GlFormInput, GlFormSelect, GlFormTextarea } from '@gitlab/ui';

describe('DynamicField', () => {
  let wrapper;

  const defaultProps = {
    help: 'The URL of the project',
    name: 'project_url',
    placeholder: 'https://jira.example.com',
    title: 'Project URL',
    type: 'text',
    value: '1',
  };

  const createComponent = props => {
    wrapper = mount(DynamicField, {
      propsData: { ...defaultProps, ...props },
    });
  };

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

  const findGlFormGroup = () => wrapper.find(GlFormGroup);
  const findGlFormCheckbox = () => wrapper.find(GlFormCheckbox);
  const findGlFormInput = () => wrapper.find(GlFormInput);
  const findGlFormSelect = () => wrapper.find(GlFormSelect);
  const findGlFormTextarea = () => wrapper.find(GlFormTextarea);

  describe('template', () => {
    describe('dynamic field', () => {
      describe('type is checkbox', () => {
        beforeEach(() => {
          createComponent({
            type: 'checkbox',
          });
        });

        it('renders GlFormCheckbox', () => {
          expect(findGlFormCheckbox().exists()).toBe(true);
        });

        it('does not render other types of input', () => {
          expect(findGlFormSelect().exists()).toBe(false);
          expect(findGlFormTextarea().exists()).toBe(false);
          expect(findGlFormInput().exists()).toBe(false);
        });
      });

      describe('type is select', () => {
        beforeEach(() => {
          createComponent({
            type: 'select',
            choices: [['all', 'All details'], ['standard', 'Standard']],
          });
        });

        it('renders findGlFormSelect', () => {
          expect(findGlFormSelect().exists()).toBe(true);
          expect(findGlFormSelect().findAll('option')).toHaveLength(2);
        });

        it('does not render other types of input', () => {
          expect(findGlFormCheckbox().exists()).toBe(false);
          expect(findGlFormTextarea().exists()).toBe(false);
          expect(findGlFormInput().exists()).toBe(false);
        });
      });

      describe('type is textarea', () => {
        beforeEach(() => {
          createComponent({
            type: 'textarea',
          });
        });

        it('renders findGlFormTextarea', () => {
          expect(findGlFormTextarea().exists()).toBe(true);
        });

        it('does not render other types of input', () => {
          expect(findGlFormCheckbox().exists()).toBe(false);
          expect(findGlFormSelect().exists()).toBe(false);
          expect(findGlFormInput().exists()).toBe(false);
        });
      });

      describe('type is password', () => {
        beforeEach(() => {
          createComponent({
            type: 'password',
          });
        });

        it('renders GlFormInput', () => {
          expect(findGlFormInput().exists()).toBe(true);
          expect(findGlFormInput().attributes('type')).toBe('password');
        });

        it('does not render other types of input', () => {
          expect(findGlFormCheckbox().exists()).toBe(false);
          expect(findGlFormSelect().exists()).toBe(false);
          expect(findGlFormTextarea().exists()).toBe(false);
        });
      });

      describe('type is text', () => {
        beforeEach(() => {
          createComponent({
            type: 'text',
            required: true,
          });
        });

        it('renders GlFormInput', () => {
          expect(findGlFormInput().exists()).toBe(true);
          expect(findGlFormInput().attributes()).toMatchObject({
            type: 'text',
            id: 'service_project_url',
            name: 'service[project_url]',
            placeholder: defaultProps.placeholder,
            required: 'required',
          });
        });

        it('does not render other types of input', () => {
          expect(findGlFormCheckbox().exists()).toBe(false);
          expect(findGlFormSelect().exists()).toBe(false);
          expect(findGlFormTextarea().exists()).toBe(false);
        });
      });
    });

    describe('help text', () => {
      it('renders description with help text', () => {
        createComponent();

        expect(
          findGlFormGroup()
            .find('small')
            .text(),
        ).toBe(defaultProps.help);
      });
    });

    describe('label text', () => {
      it('renders label with title', () => {
        createComponent();

        expect(
          findGlFormGroup()
            .find('label')
            .text(),
        ).toBe(defaultProps.title);
      });

      describe('for password field with some value (hidden by backend)', () => {
        it('renders label with new password title', () => {
          createComponent({
            type: 'password',
            value: 'true',
          });

          expect(
            findGlFormGroup()
              .find('label')
              .text(),
          ).toBe(`Enter new ${defaultProps.title}`);
        });
      });
    });
  });
});