summaryrefslogtreecommitdiff
path: root/spec/frontend/releases/components/app_edit_spec.js
blob: 09bafe4aa9b2b4c907e5c1b13794d7798ad4b351 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import Vuex from 'vuex';
import { mount } from '@vue/test-utils';
import ReleaseEditApp from '~/releases/components/app_edit.vue';
import { release as originalRelease } from '../mock_data';
import * as commonUtils from '~/lib/utils/common_utils';
import { BACK_URL_PARAM } from '~/releases/constants';
import AssetLinksForm from '~/releases/components/asset_links_form.vue';
import { merge } from 'lodash';

describe('Release edit component', () => {
  let wrapper;
  let release;
  let actions;
  let getters;
  let state;

  const factory = ({ featureFlags = {}, store: storeUpdates = {} } = {}) => {
    state = {
      release,
      markdownDocsPath: 'path/to/markdown/docs',
      updateReleaseApiDocsPath: 'path/to/update/release/api/docs',
      releasesPagePath: 'path/to/releases/page',
    };

    actions = {
      fetchRelease: jest.fn(),
      updateRelease: jest.fn(),
      addEmptyAssetLink: jest.fn(),
    };

    getters = {
      isValid: () => true,
      validationErrors: () => ({
        assets: {
          links: [],
        },
      }),
    };

    const store = new Vuex.Store(
      merge(
        {
          modules: {
            detail: {
              namespaced: true,
              actions,
              state,
              getters,
            },
          },
        },
        storeUpdates,
      ),
    );

    wrapper = mount(ReleaseEditApp, {
      store,
      provide: {
        glFeatures: featureFlags,
      },
    });
  };

  beforeEach(() => {
    gon.api_version = 'v4';

    release = commonUtils.convertObjectPropsToCamelCase(originalRelease, { deep: true });
  });

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

  const findSubmitButton = () => wrapper.find('button[type=submit]');

  describe(`basic functionality tests: all tests unrelated to the "${BACK_URL_PARAM}" parameter`, () => {
    beforeEach(() => {
      factory();
    });

    it('calls fetchRelease when the component is created', () => {
      expect(actions.fetchRelease).toHaveBeenCalledTimes(1);
    });

    it('renders the description text at the top of the page', () => {
      expect(wrapper.find('.js-subtitle-text').text()).toBe(
        'Releases are based on Git tags. We recommend tags that use semantic versioning, for example v1.0, v2.0-pre.',
      );
    });

    it('renders the correct tag name in the "Tag name" field', () => {
      expect(wrapper.find('#git-ref').element.value).toBe(release.tagName);
    });

    it('renders the correct help text under the "Tag name" field', () => {
      const helperText = wrapper.find('#tag-name-help');
      const helperTextLink = helperText.find('a');
      const helperTextLinkAttrs = helperTextLink.attributes();

      expect(helperText.text()).toBe(
        'Changing a Release tag is only supported via Releases API. More information',
      );
      expect(helperTextLink.text()).toBe('More information');
      expect(helperTextLinkAttrs).toEqual(
        expect.objectContaining({
          href: state.updateReleaseApiDocsPath,
          rel: 'noopener noreferrer',
          target: '_blank',
        }),
      );
    });

    it('renders the correct release title in the "Release title" field', () => {
      expect(wrapper.find('#release-title').element.value).toBe(release.name);
    });

    it('renders the release notes in the "Release notes" textarea', () => {
      expect(wrapper.find('#release-notes').element.value).toBe(release.description);
    });

    it('renders the "Save changes" button as type="submit"', () => {
      expect(findSubmitButton().attributes('type')).toBe('submit');
    });

    it('calls updateRelease when the form is submitted', () => {
      wrapper.find('form').trigger('submit');
      expect(actions.updateRelease).toHaveBeenCalledTimes(1);
    });
  });

  describe(`when the URL does not contain a "${BACK_URL_PARAM}" parameter`, () => {
    beforeEach(() => {
      factory();
    });

    it(`renders a "Cancel" button with an href pointing to "${BACK_URL_PARAM}"`, () => {
      const cancelButton = wrapper.find('.js-cancel-button');
      expect(cancelButton.attributes().href).toBe(state.releasesPagePath);
    });
  });

  describe(`when the URL contains a "${BACK_URL_PARAM}" parameter`, () => {
    const backUrl = 'https://example.gitlab.com/back/url';

    beforeEach(() => {
      commonUtils.getParameterByName = jest
        .fn()
        .mockImplementation(paramToGet => ({ [BACK_URL_PARAM]: backUrl }[paramToGet]));

      factory();
    });

    it('renders a "Cancel" button with an href pointing to the main Releases page', () => {
      const cancelButton = wrapper.find('.js-cancel-button');
      expect(cancelButton.attributes().href).toBe(backUrl);
    });
  });

  describe('asset links form', () => {
    const findAssetLinksForm = () => wrapper.find(AssetLinksForm);

    describe('when the release_asset_link_editing feature flag is disabled', () => {
      beforeEach(() => {
        factory({ featureFlags: { releaseAssetLinkEditing: false } });
      });

      it('does not render the asset links portion of the form', () => {
        expect(findAssetLinksForm().exists()).toBe(false);
      });
    });

    describe('when the release_asset_link_editing feature flag is enabled', () => {
      beforeEach(() => {
        factory({ featureFlags: { releaseAssetLinkEditing: true } });
      });

      it('renders the asset links portion of the form', () => {
        expect(findAssetLinksForm().exists()).toBe(true);
      });
    });
  });

  describe('validation', () => {
    describe('when the form is valid', () => {
      beforeEach(() => {
        factory({
          store: {
            modules: {
              detail: {
                getters: {
                  isValid: () => true,
                },
              },
            },
          },
        });
      });

      it('renders the submit button as enabled', () => {
        expect(findSubmitButton().attributes('disabled')).toBeUndefined();
      });
    });

    describe('when the form is invalid', () => {
      beforeEach(() => {
        factory({
          store: {
            modules: {
              detail: {
                getters: {
                  isValid: () => false,
                },
              },
            },
          },
        });
      });

      it('renders the submit button as disabled', () => {
        expect(findSubmitButton().attributes('disabled')).toBe('disabled');
      });
    });
  });
});