Welcome to mirror list, hosted at ThFree Co, Russian Federation.

settings_form_spec.js « components « settings « registry « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b3e529b283905a7462ed7def484178477e867be (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
180
import { shallowMount } from '@vue/test-utils';
import Tracking from '~/tracking';
import component from '~/registry/settings/components/settings_form.vue';
import expirationPolicyFields from '~/registry/shared/components/expiration_policy_fields.vue';
import { createStore } from '~/registry/settings/store/';
import {
  UPDATE_SETTINGS_ERROR_MESSAGE,
  UPDATE_SETTINGS_SUCCESS_MESSAGE,
} from '~/registry/shared/constants';
import { stringifiedFormOptions } from '../../shared/mock_data';

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

  const GlLoadingIcon = { name: 'gl-loading-icon-stub', template: '<svg></svg>' };
  const GlCard = {
    name: 'gl-card-stub',
    template: `
  <div>
    <slot name="header"></slot>
    <slot></slot>
    <slot name="footer"></slot>
  </div>
  `,
  };

  const trackingPayload = {
    label: 'docker_container_retention_and_expiration_policies',
  };

  const findForm = () => wrapper.find({ ref: 'form-element' });
  const findFields = () => wrapper.find(expirationPolicyFields);
  const findCancelButton = () => wrapper.find({ ref: 'cancel-button' });
  const findSaveButton = () => wrapper.find({ ref: 'save-button' });
  const findLoadingIcon = (parent = wrapper) => parent.find(GlLoadingIcon);

  const mountComponent = () => {
    wrapper = shallowMount(component, {
      stubs: {
        GlCard,
        GlLoadingIcon,
      },
      mocks: {
        $toast: {
          show: jest.fn(),
        },
      },
      store,
    });
  };

  beforeEach(() => {
    store = createStore();
    store.dispatch('setInitialState', stringifiedFormOptions);
    dispatchSpy = jest.spyOn(store, 'dispatch');
    mountComponent();
    jest.spyOn(Tracking, 'event');
  });

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

  describe('form', () => {
    let form;
    beforeEach(() => {
      form = findForm();
      dispatchSpy.mockReturnValue();
    });

    describe('data binding', () => {
      it('v-model change update the settings property', () => {
        findFields().vm.$emit('input', 'foo');
        expect(dispatchSpy).toHaveBeenCalledWith('updateSettings', { settings: 'foo' });
      });
    });

    describe('form reset event', () => {
      beforeEach(() => {
        form.trigger('reset');
      });
      it('calls the appropriate function', () => {
        expect(dispatchSpy).toHaveBeenCalledWith('resetSettings');
      });

      it('tracks the reset event', () => {
        expect(Tracking.event).toHaveBeenCalledWith(undefined, 'reset_form', trackingPayload);
      });
    });

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

      it('dispatches the saveSettings action', () => {
        dispatchSpy.mockResolvedValue();
        form.trigger('submit');
        expect(dispatchSpy).toHaveBeenCalledWith('saveSettings');
      });

      it('tracks the submit event', () => {
        dispatchSpy.mockResolvedValue();
        form.trigger('submit');
        expect(Tracking.event).toHaveBeenCalledWith(undefined, 'submit_form', trackingPayload);
      });

      it('show a success toast when submit succeed', () => {
        dispatchSpy.mockResolvedValue();
        form.trigger('submit');
        return wrapper.vm.$nextTick().then(() => {
          expect(wrapper.vm.$toast.show).toHaveBeenCalledWith(UPDATE_SETTINGS_SUCCESS_MESSAGE, {
            type: 'success',
          });
        });
      });

      it('show an error toast when submit fails', () => {
        dispatchSpy.mockRejectedValue();
        form.trigger('submit');
        return wrapper.vm.$nextTick().then(() => {
          expect(wrapper.vm.$toast.show).toHaveBeenCalledWith(UPDATE_SETTINGS_ERROR_MESSAGE, {
            type: 'error',
          });
        });
      });
    });
  });

  describe('form actions', () => {
    describe('cancel button', () => {
      beforeEach(() => {
        store.commit('SET_SETTINGS', { foo: 'bar' });
      });

      it('has type reset', () => {
        expect(findCancelButton().attributes('type')).toBe('reset');
      });

      it('is disabled when isEdited is false', () =>
        wrapper.vm.$nextTick().then(() => {
          expect(findCancelButton().attributes('disabled')).toBe('true');
        }));

      it('is disabled isLoading is true', () => {
        store.commit('TOGGLE_LOADING');
        store.commit('UPDATE_SETTINGS', { settings: { foo: 'baz' } });
        return wrapper.vm.$nextTick().then(() => {
          expect(findCancelButton().attributes('disabled')).toBe('true');
          store.commit('TOGGLE_LOADING');
        });
      });

      it('is enabled when isLoading is false and isEdited is true', () => {
        store.commit('UPDATE_SETTINGS', { settings: { foo: 'baz' } });
        return wrapper.vm.$nextTick().then(() => {
          expect(findCancelButton().attributes('disabled')).toBe(undefined);
        });
      });
    });

    describe('when isLoading is true', () => {
      beforeEach(() => {
        store.commit('TOGGLE_LOADING');
      });
      afterEach(() => {
        store.commit('TOGGLE_LOADING');
      });

      it('submit button is disabled and shows a spinner', () => {
        const button = findSaveButton();
        expect(button.attributes('disabled')).toBeTruthy();
        expect(findLoadingIcon(button).exists()).toBe(true);
      });
    });
  });
});