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

badge_form_spec.js « components « badges « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d7519f1f80df317194aa15243d451f442d68e51f (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
181
182
import MockAdapter from 'axios-mock-adapter';
import Vue from 'vue';
import Vuex from 'vuex';
import { mount } from '@vue/test-utils';
import { DUMMY_IMAGE_URL, TEST_HOST } from 'helpers/test_constants';
import BadgeForm from '~/badges/components/badge_form.vue';
import createEmptyBadge from '~/badges/empty_badge';

import createState from '~/badges/store/state';
import mutations from '~/badges/store/mutations';
import actions from '~/badges/store/actions';

import axios from '~/lib/utils/axios_utils';

Vue.use(Vuex);

describe('BadgeForm component', () => {
  let axiosMock;
  let mockedActions;
  let wrapper;

  const createComponent = (propsData, customState = {}) => {
    mockedActions = Object.fromEntries(Object.keys(actions).map((name) => [name, jest.fn()]));

    const store = new Vuex.Store({
      state: {
        ...createState(),
        ...customState,
      },
      mutations,
      actions: mockedActions,
    });

    wrapper = mount(BadgeForm, {
      store,
      propsData,
      attachTo: document.body,
    });
  };

  beforeEach(() => {
    axiosMock = new MockAdapter(axios);
  });

  afterEach(() => {
    axiosMock.restore();
  });

  it('stops editing when cancel button is clicked', async () => {
    createComponent({ isEditing: true });

    const cancelButton = wrapper.find('.row-content-block button');

    await cancelButton.trigger('click');

    expect(mockedActions.stopEditing).toHaveBeenCalled();
  });

  const sharedSubmitTests = (submitAction, props) => {
    const nameSelector = '#badge-name';
    const imageUrlSelector = '#badge-image-url';
    const findImageUrl = () => wrapper.find(imageUrlSelector);
    const linkUrlSelector = '#badge-link-url';
    const findLinkUrl = () => wrapper.find(linkUrlSelector);
    const setValue = (inputElementSelector, value) => {
      const input = wrapper.find(inputElementSelector);
      return input.setValue(value);
    };
    const submitForm = () => {
      const submitButton = wrapper.find('button[type="submit"]');
      return submitButton.trigger('click');
    };
    const expectInvalidInput = (inputElementSelector) => {
      const input = wrapper.find(inputElementSelector);

      expect(input.element.checkValidity()).toBe(false);
      const feedbackElement = wrapper.find(`${inputElementSelector} + .invalid-feedback`);

      expect(feedbackElement.isVisible()).toBe(true);
    };

    beforeEach(() => {
      createComponent(props, {
        badgeInAddForm: createEmptyBadge(),
        badgeInEditForm: createEmptyBadge(),
        isSaving: false,
      });

      setValue(nameSelector, 'TestBadge');
      setValue(linkUrlSelector, `${TEST_HOST}/link/url`);
      setValue(imageUrlSelector, `${window.location.origin}${DUMMY_IMAGE_URL}`);
    });

    it('returns immediately if imageUrl is empty', async () => {
      await setValue(imageUrlSelector, '');

      await submitForm();

      expectInvalidInput(imageUrlSelector);

      expect(mockedActions[submitAction]).not.toHaveBeenCalled();
    });

    it('returns immediately if imageUrl is malformed', async () => {
      await setValue(imageUrlSelector, 'not-a-url');

      await submitForm();

      expectInvalidInput(imageUrlSelector);

      expect(mockedActions[submitAction]).not.toHaveBeenCalled();
    });

    it('returns immediately if linkUrl is empty', async () => {
      await setValue(linkUrlSelector, '');

      await submitForm();

      expectInvalidInput(linkUrlSelector);

      expect(mockedActions[submitAction]).not.toHaveBeenCalled();
    });

    it('returns immediately if linkUrl is malformed', async () => {
      await setValue(linkUrlSelector, 'not-a-url');

      await submitForm();

      expectInvalidInput(linkUrlSelector);

      expect(mockedActions[submitAction]).not.toHaveBeenCalled();
    });

    it(`calls ${submitAction}`, async () => {
      await submitForm();

      expect(findImageUrl().element.checkValidity()).toBe(true);
      expect(findLinkUrl().element.checkValidity()).toBe(true);
      expect(mockedActions[submitAction]).toHaveBeenCalled();
    });
  };

  describe('if isEditing is false', () => {
    const props = { isEditing: false };

    it('renders one button', () => {
      createComponent(props);

      expect(wrapper.find('.row-content-block').exists()).toBe(false);
      const buttons = wrapper.findAll('.form-group:last-of-type button');

      expect(buttons).toHaveLength(1);
      const buttonAddWrapper = buttons.at(0);

      expect(buttonAddWrapper.isVisible()).toBe(true);
      expect(buttonAddWrapper.text()).toBe('Add badge');
    });

    sharedSubmitTests('addBadge', props);
  });

  describe('if isEditing is true', () => {
    const props = { isEditing: true };

    it('renders two buttons', () => {
      createComponent(props);
      const buttons = wrapper.findAll('.row-content-block button');

      expect(buttons).toHaveLength(2);

      const saveButton = buttons.at(1);
      expect(saveButton.isVisible()).toBe(true);
      expect(saveButton.text()).toBe('Save changes');

      const cancelButton = buttons.at(0);
      expect(cancelButton.isVisible()).toBe(true);
      expect(cancelButton.text()).toBe('Cancel');
    });

    sharedSubmitTests('saveBadge', props);
  });
});