2021-07-30 00:10:03 +00:00
|
|
|
import { GlLoadingIcon } from '@gitlab/ui';
|
2021-07-23 09:08:49 +00:00
|
|
|
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';
|
|
|
|
import createFlash from '~/flash';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import { visitUrl } from '~/lib/utils/url_utility';
|
|
|
|
|
|
|
|
jest.mock('~/lib/utils/url_utility');
|
|
|
|
jest.mock('~/flash');
|
|
|
|
|
|
|
|
const DEFAULT_OPTS = {
|
|
|
|
provide: {
|
|
|
|
projectEnvironmentsPath: '/projects/environments',
|
|
|
|
updateEnvironmentPath: '/proejcts/environments/1',
|
|
|
|
},
|
2021-08-26 21:11:25 +00:00
|
|
|
propsData: { environment: { id: '0', name: 'foo', external_url: 'https://foo.example.com' } },
|
2021-07-23 09:08:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
2021-08-26 21:11:25 +00:00
|
|
|
const findNameInput = () => wrapper.findByLabelText('Name');
|
|
|
|
const findExternalUrlInput = () => wrapper.findByLabelText('External URL');
|
|
|
|
const findForm = () => wrapper.findByRole('form', { name: 'Edit environment' });
|
|
|
|
|
2021-07-30 00:10:03 +00:00
|
|
|
const showsLoading = () => wrapper.find(GlLoadingIcon).exists();
|
|
|
|
|
|
|
|
const submitForm = async (expected, response) => {
|
2021-07-23 09:08:49 +00:00
|
|
|
mock
|
|
|
|
.onPut(DEFAULT_OPTS.provide.updateEnvironmentPath, {
|
|
|
|
external_url: expected.url,
|
2021-08-26 21:11:25 +00:00
|
|
|
id: '0',
|
2021-07-23 09:08:49 +00:00
|
|
|
})
|
|
|
|
.reply(...response);
|
2021-08-26 21:11:25 +00:00
|
|
|
await findExternalUrlInput().setValue(expected.url);
|
2021-07-23 09:08:49 +00:00
|
|
|
|
2021-08-26 21:11:25 +00:00
|
|
|
await findForm().trigger('submit');
|
2021-07-23 09:08:49 +00:00
|
|
|
await waitForPromises();
|
|
|
|
};
|
|
|
|
|
|
|
|
it('sets the title to Edit environment', () => {
|
|
|
|
const header = wrapper.findByRole('heading', { name: 'Edit environment' });
|
|
|
|
expect(header.exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-07-30 00:10:03 +00:00
|
|
|
it('shows loader after form is submitted', async () => {
|
2021-08-26 21:11:25 +00:00
|
|
|
const expected = { url: 'https://google.ca' };
|
2021-07-30 00:10:03 +00:00
|
|
|
|
|
|
|
expect(showsLoading()).toBe(false);
|
|
|
|
|
|
|
|
await submitForm(expected, [200, { path: '/test' }]);
|
|
|
|
|
|
|
|
expect(showsLoading()).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-07-23 09:08:49 +00:00
|
|
|
it('submits the updated environment on submit', async () => {
|
2021-08-26 21:11:25 +00:00
|
|
|
const expected = { url: 'https://google.ca' };
|
2021-07-23 09:08:49 +00:00
|
|
|
|
2021-07-30 00:10:03 +00:00
|
|
|
await submitForm(expected, [200, { path: '/test' }]);
|
2021-07-23 09:08:49 +00:00
|
|
|
|
|
|
|
expect(visitUrl).toHaveBeenCalledWith('/test');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows errors on error', async () => {
|
2021-08-26 21:11:25 +00:00
|
|
|
const expected = { url: 'https://google.ca' };
|
2021-07-23 09:08:49 +00:00
|
|
|
|
2021-08-26 21:11:25 +00:00
|
|
|
await submitForm(expected, [400, { message: ['uh oh!'] }]);
|
2021-07-23 09:08:49 +00:00
|
|
|
|
2021-08-26 21:11:25 +00:00
|
|
|
expect(createFlash).toHaveBeenCalledWith({ message: 'uh oh!' });
|
2021-07-30 00:10:03 +00:00
|
|
|
expect(showsLoading()).toBe(false);
|
2021-07-23 09:08:49 +00:00
|
|
|
});
|
2021-08-26 21:11:25 +00:00
|
|
|
|
|
|
|
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-07-23 09:08:49 +00:00
|
|
|
});
|