summaryrefslogtreecommitdiff
path: root/spec/frontend/registry/settings/components/settings_form_spec.js
blob: bd733e965a49d742ba1c5ad0e96c247f9f89d803 (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
import Vuex from 'vuex';
import { mount, createLocalVue } from '@vue/test-utils';
import stubChildren from 'helpers/stub_children';
import component from '~/registry/settings/components/settings_form.vue';
import { createStore } from '~/registry/settings/store/';
import { NAME_REGEX_LENGTH } from '~/registry/settings/constants';
import { stringifiedFormOptions } from '../mock_data';

const localVue = createLocalVue();
localVue.use(Vuex);

describe('Settings Form', () => {
  let wrapper;
  let store;
  let saveSpy;
  let resetSpy;

  const findFormGroup = name => wrapper.find(`#expiration-policy-${name}-group`);
  const findFormElements = (name, father = wrapper) => father.find(`#expiration-policy-${name}`);
  const findCancelButton = () => wrapper.find({ ref: 'cancel-button' });
  const findSaveButton = () => wrapper.find({ ref: 'save-button' });
  const findForm = () => wrapper.find({ ref: 'form-element' });

  const mountComponent = (options = {}) => {
    saveSpy = jest.fn();
    resetSpy = jest.fn();
    wrapper = mount(component, {
      stubs: {
        ...stubChildren(component),
        GlCard: false,
      },
      store,
      methods: {
        saveSettings: saveSpy,
        resetSettings: resetSpy,
      },
      ...options,
    });
  };

  beforeEach(() => {
    store = createStore();
    store.dispatch('setInitialState', stringifiedFormOptions);
    mountComponent();
  });

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

  it('renders', () => {
    expect(wrapper.element).toMatchSnapshot();
  });

  describe.each`
    elementName        | modelName       | value    | disabledByToggle
    ${'toggle'}        | ${'enabled'}    | ${true}  | ${'not disabled'}
    ${'interval'}      | ${'older_than'} | ${'foo'} | ${'disabled'}
    ${'schedule'}      | ${'cadence'}    | ${'foo'} | ${'disabled'}
    ${'latest'}        | ${'keep_n'}     | ${'foo'} | ${'disabled'}
    ${'name-matching'} | ${'name_regex'} | ${'foo'} | ${'disabled'}
  `('$elementName form element', ({ elementName, modelName, value, disabledByToggle }) => {
    let formGroup;
    beforeEach(() => {
      formGroup = findFormGroup(elementName);
    });
    it(`${elementName} form group exist in the dom`, () => {
      expect(formGroup.exists()).toBe(true);
    });

    it(`${elementName} form group has a label-for property`, () => {
      expect(formGroup.attributes('label-for')).toBe(`expiration-policy-${elementName}`);
    });

    it(`${elementName} form group has a label-cols property`, () => {
      expect(formGroup.attributes('label-cols')).toBe(`${wrapper.vm.$options.labelsConfig.cols}`);
    });

    it(`${elementName} form group has a label-align property`, () => {
      expect(formGroup.attributes('label-align')).toBe(`${wrapper.vm.$options.labelsConfig.align}`);
    });

    it(`${elementName} form group contains an input element`, () => {
      expect(findFormElements(elementName, formGroup).exists()).toBe(true);
    });

    it(`${elementName} form element change updated ${modelName} with ${value}`, () => {
      const element = findFormElements(elementName, formGroup);
      const modelUpdateEvent = element.vm.$options.model
        ? element.vm.$options.model.event
        : 'input';
      element.vm.$emit(modelUpdateEvent, value);
      return wrapper.vm.$nextTick().then(() => {
        expect(wrapper.vm[modelName]).toBe(value);
      });
    });

    it(`${elementName} is ${disabledByToggle} by enabled set to false`, () => {
      store.dispatch('updateSettings', { enabled: false });
      const expectation = disabledByToggle === 'disabled' ? 'true' : undefined;
      expect(findFormElements(elementName, formGroup).attributes('disabled')).toBe(expectation);
    });
  });

  describe('form actions', () => {
    let form;
    beforeEach(() => {
      form = findForm();
    });
    it('cancel has type reset', () => {
      expect(findCancelButton().attributes('type')).toBe('reset');
    });

    it('form reset event call the appropriate function', () => {
      form.trigger('reset');
      expect(resetSpy).toHaveBeenCalled();
    });

    it('save has type submit', () => {
      expect(findSaveButton().attributes('type')).toBe('submit');
    });

    it('form submit event call the appropriate function', () => {
      form.trigger('submit');
      expect(saveSpy).toHaveBeenCalled();
    });
  });

  describe('form validation', () => {
    describe(`when name regex is longer than ${NAME_REGEX_LENGTH}`, () => {
      const invalidString = new Array(NAME_REGEX_LENGTH + 2).join(',');
      beforeEach(() => {
        store.dispatch('updateSettings', { name_regex: invalidString });
      });

      it('save btn is disabled', () => {
        expect(findSaveButton().attributes('disabled')).toBeTruthy();
      });

      it('nameRegexState is false', () => {
        expect(wrapper.vm.nameRegexState).toBe(false);
      });
    });

    it('if the user did not type validation is null', () => {
      store.dispatch('updateSettings', { name_regex: null });
      expect(wrapper.vm.nameRegexState).toBe(null);
      return wrapper.vm.$nextTick().then(() => {
        expect(findSaveButton().attributes('disabled')).toBeFalsy();
      });
    });

    it(`if the user typed and is less than ${NAME_REGEX_LENGTH} state is true`, () => {
      store.dispatch('updateSettings', { name_regex: 'abc' });
      expect(wrapper.vm.nameRegexState).toBe(true);
    });
  });

  describe('help text', () => {
    it('toggleDescriptionText text reflects enabled property', () => {
      const toggleHelpText = findFormGroup('toggle').find('span');
      expect(toggleHelpText.html()).toContain('disabled');
      wrapper.vm.enabled = true;
      return wrapper.vm.$nextTick().then(() => {
        expect(toggleHelpText.html()).toContain('enabled');
      });
    });
  });
});