summaryrefslogtreecommitdiff
path: root/spec/frontend/pipeline_wizard/components/widgets/text_spec.js
blob: a11c0214d156f32a87fc4b6ca280d02eca05a59b (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
import { shallowMount } from '@vue/test-utils';
import { GlFormGroup, GlFormInput } from '@gitlab/ui';
import TextWidget from '~/pipeline_wizard/components/widgets/text.vue';
import { mountExtended } from 'helpers/vue_test_utils_helper';

describe('Pipeline Wizard - Text Widget', () => {
  const defaultProps = {
    label: 'This label',
    description: 'some description',
    placeholder: 'some placeholder',
    pattern: '^[a-z]+$',
    invalidFeedback: 'some feedback',
  };

  let wrapper;

  const findGlFormGroup = () => wrapper.findComponent(GlFormGroup);
  const findGlFormGroupInvalidFeedback = () => findGlFormGroup().find('.invalid-feedback');
  const findGlFormInput = () => wrapper.findComponent(GlFormInput);

  const createComponent = (props = {}, mountFn = mountExtended) => {
    wrapper = mountFn(TextWidget, {
      propsData: {
        ...defaultProps,
        ...props,
      },
    });
  };

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

  it('creates an input element with the correct label', () => {
    createComponent();

    expect(wrapper.findByLabelText(defaultProps.label).exists()).toBe(true);
  });

  it('passes the description', () => {
    createComponent({}, shallowMount);

    expect(findGlFormGroup().attributes('description')).toBe(defaultProps.description);
  });

  it('sets the "text" type on the input component', () => {
    createComponent();

    expect(findGlFormInput().attributes('type')).toBe('text');
  });

  it('passes the placeholder', () => {
    createComponent();

    expect(findGlFormInput().attributes('placeholder')).toBe(defaultProps.placeholder);
  });

  it('emits an update event on input', async () => {
    createComponent();

    const localValue = 'somevalue';
    await findGlFormInput().setValue(localValue);

    expect(wrapper.emitted('input')).toEqual([[localValue]]);
  });

  it('passes invalid feedback message', () => {
    createComponent();

    expect(findGlFormGroupInvalidFeedback().text()).toBe(defaultProps.invalidFeedback);
  });

  it('provides invalid feedback', async () => {
    createComponent({ validate: true });

    await findGlFormInput().setValue('invalid%99');

    expect(findGlFormGroup().classes()).toContain('is-invalid');
    expect(findGlFormInput().classes()).toContain('is-invalid');
  });

  it('provides valid feedback', async () => {
    createComponent({ validate: true });

    await findGlFormInput().setValue('valid');

    expect(findGlFormGroup().classes()).toContain('is-valid');
    expect(findGlFormInput().classes()).toContain('is-valid');
  });

  it('does not show validation state when untouched', () => {
    createComponent({ value: 'invalid99' });

    expect(findGlFormGroup().classes()).not.toContain('is-valid');
    expect(findGlFormGroup().classes()).not.toContain('is-invalid');
  });

  it('shows invalid state on blur', async () => {
    createComponent();

    await findGlFormInput().setValue('invalid%99');

    expect(findGlFormGroup().classes()).not.toContain('is-invalid');

    await findGlFormInput().trigger('blur');

    expect(findGlFormInput().classes()).toContain('is-invalid');
    expect(findGlFormGroup().classes()).toContain('is-invalid');
  });

  it('shows invalid state when toggling `validate` prop', async () => {
    createComponent({
      required: true,
      validate: false,
    });

    expect(findGlFormGroup().classes()).not.toContain('is-invalid');

    await wrapper.setProps({ validate: true });

    expect(findGlFormGroup().classes()).toContain('is-invalid');
  });

  it('does not update validation if not required', async () => {
    createComponent({
      pattern: null,
      validate: true,
    });

    expect(findGlFormGroup().classes()).not.toContain('is-invalid');
  });

  it('sets default value', () => {
    const defaultValue = 'foo';
    createComponent({
      default: defaultValue,
    });

    expect(wrapper.findByLabelText(defaultProps.label).element.value).toBe(defaultValue);
  });

  it('emits default value on setup', () => {
    const defaultValue = 'foo';
    createComponent({
      default: defaultValue,
    });

    expect(wrapper.emitted('input')).toEqual([[defaultValue]]);
  });
});