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

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

104 lines
3 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 NewEnvironment from '~/environments/components/new_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 = {
2023-04-23 21:23:45 +05:30
provide: {
projectEnvironmentsPath: '/projects/environments',
protectedEnvironmentSettingsPath: '/projects/not_real/settings/ci_cd',
},
2021-10-27 15:23:28 +05:30
};
describe('~/environments/components/new.vue', () => {
let wrapper;
let mock;
let name;
let url;
let form;
const createWrapper = (opts = {}) =>
mountExtended(NewEnvironment, {
...DEFAULT_OPTS,
...opts,
});
beforeEach(() => {
mock = new MockAdapter(axios);
wrapper = createWrapper();
name = wrapper.findByLabelText('Name');
url = wrapper.findByLabelText('External URL');
form = wrapper.findByRole('form', { name: 'New environment' });
});
afterEach(() => {
mock.restore();
});
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
.onPost(DEFAULT_OPTS.provide.projectEnvironmentsPath, {
name: expected.name,
external_url: expected.url,
})
.reply(...response);
await name.setValue(expected.name);
await url.setValue(expected.url);
await form.trigger('submit');
await waitForPromises();
};
it('sets the title to New environment', () => {
const header = wrapper.findByRole('heading', { name: 'New environment' });
expect(header.exists()).toBe(true);
});
it.each`
input | value
${() => name} | ${'test'}
${() => url} | ${'https://example.org'}
2022-10-11 01:57:18 +05:30
`('changes the value of the input to $value', async ({ input, value }) => {
2021-10-27 15:23:28 +05:30
await input().setValue(value);
expect(input().element.value).toBe(value);
});
it('shows loader after form is submitted', async () => {
const expected = { name: 'test', url: 'https://google.ca' };
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 new environment on submit', async () => {
const expected = { name: 'test', url: 'https://google.ca' };
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 () => {
const expected = { name: 'test', url: 'https://google.ca' };
2023-04-23 21:23:45 +05:30
await submitForm(expected, [HTTP_STATUS_BAD_REQUEST, { message: ['name taken'] }]);
2021-10-27 15:23:28 +05:30
2022-11-25 23:54:43 +05:30
expect(createAlert).toHaveBeenCalledWith({ message: 'name taken' });
2021-10-27 15:23:28 +05:30
expect(showsLoading()).toBe(false);
});
});