debian-mirror-gitlab/spec/frontend/environments/edit_environment_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

106 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-10-27 15:23:28 +05:30
import { GlLoadingIcon } from '@gitlab/ui';
import MockAdapter from 'axios-mock-adapter';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import EditEnvironment from '~/environments/components/edit_environment.vue';
2023-05-27 22:25:52 +05:30
import { createAlert } from '~/alert';
2021-10-27 15:23:28 +05:30
import axios from '~/lib/utils/axios_utils';
2023-04-23 21:23:45 +05:30
import { HTTP_STATUS_BAD_REQUEST, HTTP_STATUS_OK } from '~/lib/utils/http_status';
2021-10-27 15:23:28 +05:30
import { visitUrl } from '~/lib/utils/url_utility';
jest.mock('~/lib/utils/url_utility');
2023-05-27 22:25:52 +05:30
jest.mock('~/alert');
2021-10-27 15:23:28 +05:30
const DEFAULT_OPTS = {
provide: {
projectEnvironmentsPath: '/projects/environments',
updateEnvironmentPath: '/proejcts/environments/1',
2023-04-23 21:23:45 +05:30
protectedEnvironmentSettingsPath: '/projects/1/settings/ci_cd',
2021-10-27 15:23:28 +05:30
},
2021-11-11 11:23:49 +05:30
propsData: { environment: { id: '0', name: 'foo', external_url: 'https://foo.example.com' } },
2021-10-27 15:23:28 +05:30
};
describe('~/environments/components/edit.vue', () => {
let wrapper;
let mock;
const createWrapper = (opts = {}) =>
mountExtended(EditEnvironment, {
...DEFAULT_OPTS,
...opts,
});
beforeEach(() => {
mock = new MockAdapter(axios);
wrapper = createWrapper();
});
afterEach(() => {
mock.restore();
});
2021-11-11 11:23:49 +05:30
const findNameInput = () => wrapper.findByLabelText('Name');
const findExternalUrlInput = () => wrapper.findByLabelText('External URL');
const findForm = () => wrapper.findByRole('form', { name: 'Edit environment' });
2022-08-27 11:52:29 +05:30
const showsLoading = () => wrapper.findComponent(GlLoadingIcon).exists();
2021-10-27 15:23:28 +05:30
const submitForm = async (expected, response) => {
mock
.onPut(DEFAULT_OPTS.provide.updateEnvironmentPath, {
external_url: expected.url,
2021-11-11 11:23:49 +05:30
id: '0',
2021-10-27 15:23:28 +05:30
})
.reply(...response);
2021-11-11 11:23:49 +05:30
await findExternalUrlInput().setValue(expected.url);
2021-10-27 15:23:28 +05:30
2021-11-11 11:23:49 +05:30
await findForm().trigger('submit');
2021-10-27 15:23:28 +05:30
await waitForPromises();
};
it('sets the title to Edit environment', () => {
const header = wrapper.findByRole('heading', { name: 'Edit environment' });
expect(header.exists()).toBe(true);
});
it('shows loader after form is submitted', async () => {
2021-11-11 11:23:49 +05:30
const expected = { url: 'https://google.ca' };
2021-10-27 15:23:28 +05:30
expect(showsLoading()).toBe(false);
2023-04-23 21:23:45 +05:30
await submitForm(expected, [HTTP_STATUS_OK, { path: '/test' }]);
2021-10-27 15:23:28 +05:30
expect(showsLoading()).toBe(true);
});
it('submits the updated environment on submit', async () => {
2021-11-11 11:23:49 +05:30
const expected = { url: 'https://google.ca' };
2021-10-27 15:23:28 +05:30
2023-04-23 21:23:45 +05:30
await submitForm(expected, [HTTP_STATUS_OK, { path: '/test' }]);
2021-10-27 15:23:28 +05:30
expect(visitUrl).toHaveBeenCalledWith('/test');
});
it('shows errors on error', async () => {
2021-11-11 11:23:49 +05:30
const expected = { url: 'https://google.ca' };
2021-10-27 15:23:28 +05:30
2023-04-23 21:23:45 +05:30
await submitForm(expected, [HTTP_STATUS_BAD_REQUEST, { message: ['uh oh!'] }]);
2021-10-27 15:23:28 +05:30
2022-11-25 23:54:43 +05:30
expect(createAlert).toHaveBeenCalledWith({ message: 'uh oh!' });
2021-10-27 15:23:28 +05:30
expect(showsLoading()).toBe(false);
});
2021-11-11 11:23:49 +05:30
it('renders a disabled "Name" field', () => {
const nameInput = findNameInput();
expect(nameInput.attributes().disabled).toBe('disabled');
expect(nameInput.element.value).toBe('foo');
});
it('renders an "External URL" field', () => {
const urlInput = findExternalUrlInput();
expect(urlInput.element.value).toBe('https://foo.example.com');
});
2021-10-27 15:23:28 +05:30
});