debian-mirror-gitlab/spec/frontend/integrations/edit/components/dynamic_field_spec.js

251 lines
8.2 KiB
JavaScript
Raw Normal View History

2020-06-23 00:09:42 +05:30
import { GlFormGroup, GlFormCheckbox, GlFormInput, GlFormSelect, GlFormTextarea } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { mount } from '@vue/test-utils';
2021-09-04 01:27:46 +05:30
2020-10-24 23:57:45 +05:30
import DynamicField from '~/integrations/edit/components/dynamic_field.vue';
2022-04-04 11:22:00 +05:30
import { mockField } from '../mock_data';
2020-06-23 00:09:42 +05:30
describe('DynamicField', () => {
let wrapper;
2020-07-28 23:09:34 +05:30
const createComponent = (props, isInheriting = false) => {
2020-06-23 00:09:42 +05:30
wrapper = mount(DynamicField, {
2022-04-04 11:22:00 +05:30
propsData: { ...mockField, ...props },
2020-07-28 23:09:34 +05:30
computed: {
isInheriting: () => isInheriting,
},
2020-06-23 00:09:42 +05:30
});
};
afterEach(() => {
2021-09-04 01:27:46 +05:30
wrapper.destroy();
2020-06-23 00:09:42 +05:30
});
2021-09-04 01:27:46 +05:30
const findGlFormGroup = () => wrapper.findComponent(GlFormGroup);
const findGlFormCheckbox = () => wrapper.findComponent(GlFormCheckbox);
const findGlFormInput = () => wrapper.findComponent(GlFormInput);
const findGlFormSelect = () => wrapper.findComponent(GlFormSelect);
const findGlFormTextarea = () => wrapper.findComponent(GlFormTextarea);
2020-06-23 00:09:42 +05:30
describe('template', () => {
2021-12-11 22:18:48 +05:30
describe.each`
isInheriting | disabled | readonly | checkboxLabel
${true} | ${'disabled'} | ${'readonly'} | ${undefined}
${false} | ${undefined} | ${undefined} | ${'Custom checkbox label'}
`(
'dynamic field, when isInheriting = `%p`',
({ isInheriting, disabled, readonly, checkboxLabel }) => {
describe('type is checkbox', () => {
beforeEach(() => {
createComponent(
{
type: 'checkbox',
checkboxLabel,
},
isInheriting,
);
});
2020-06-23 00:09:42 +05:30
2021-12-11 22:18:48 +05:30
it(`renders GlFormCheckbox, which ${isInheriting ? 'is' : 'is not'} disabled`, () => {
expect(findGlFormCheckbox().exists()).toBe(true);
expect(findGlFormCheckbox().find('[type=checkbox]').attributes('disabled')).toBe(
disabled,
);
});
2020-06-23 00:09:42 +05:30
2021-12-11 22:18:48 +05:30
it(`renders GlFormCheckbox with correct text content when checkboxLabel is ${checkboxLabel}`, () => {
2022-04-04 11:22:00 +05:30
expect(findGlFormCheckbox().text()).toContain(checkboxLabel ?? mockField.title);
2021-12-11 22:18:48 +05:30
});
2020-06-23 00:09:42 +05:30
2021-12-11 22:18:48 +05:30
it('does not render other types of input', () => {
expect(findGlFormSelect().exists()).toBe(false);
expect(findGlFormTextarea().exists()).toBe(false);
expect(findGlFormInput().exists()).toBe(false);
});
2020-06-23 00:09:42 +05:30
});
2021-12-11 22:18:48 +05:30
describe('type is select', () => {
beforeEach(() => {
createComponent(
{
type: 'select',
choices: [
['all', 'All details'],
['standard', 'Standard'],
],
},
isInheriting,
);
});
2020-06-23 00:09:42 +05:30
2021-12-11 22:18:48 +05:30
it(`renders GlFormSelect, which ${isInheriting ? 'is' : 'is not'} disabled`, () => {
expect(findGlFormSelect().exists()).toBe(true);
expect(findGlFormSelect().findAll('option')).toHaveLength(2);
expect(findGlFormSelect().find('select').attributes('disabled')).toBe(disabled);
});
2020-06-23 00:09:42 +05:30
2021-12-11 22:18:48 +05:30
it('does not render other types of input', () => {
expect(findGlFormCheckbox().exists()).toBe(false);
expect(findGlFormTextarea().exists()).toBe(false);
expect(findGlFormInput().exists()).toBe(false);
});
2021-03-08 18:12:59 +05:30
});
2020-06-23 00:09:42 +05:30
2021-12-11 22:18:48 +05:30
describe('type is textarea', () => {
beforeEach(() => {
createComponent(
{
type: 'textarea',
},
isInheriting,
);
});
2020-06-23 00:09:42 +05:30
2021-12-11 22:18:48 +05:30
it(`renders GlFormTextarea, which ${isInheriting ? 'is' : 'is not'} readonly`, () => {
expect(findGlFormTextarea().exists()).toBe(true);
expect(findGlFormTextarea().find('textarea').attributes('readonly')).toBe(readonly);
});
2020-06-23 00:09:42 +05:30
2021-12-11 22:18:48 +05:30
it('does not render other types of input', () => {
expect(findGlFormCheckbox().exists()).toBe(false);
expect(findGlFormSelect().exists()).toBe(false);
expect(findGlFormInput().exists()).toBe(false);
});
2021-03-08 18:12:59 +05:30
});
2020-06-23 00:09:42 +05:30
2021-12-11 22:18:48 +05:30
describe('type is password', () => {
beforeEach(() => {
createComponent(
{
type: 'password',
},
isInheriting,
);
});
it(`renders GlFormInput, which ${isInheriting ? 'is' : 'is not'} readonly`, () => {
expect(findGlFormInput().exists()).toBe(true);
expect(findGlFormInput().attributes('type')).toBe('password');
expect(findGlFormInput().attributes('readonly')).toBe(readonly);
});
it('does not render other types of input', () => {
expect(findGlFormCheckbox().exists()).toBe(false);
expect(findGlFormSelect().exists()).toBe(false);
expect(findGlFormTextarea().exists()).toBe(false);
2020-07-28 23:09:34 +05:30
});
2020-06-23 00:09:42 +05:30
});
2021-03-08 18:12:59 +05:30
2021-12-11 22:18:48 +05:30
describe('type is text', () => {
beforeEach(() => {
createComponent(
{
type: 'text',
required: true,
},
isInheriting,
);
});
it(`renders GlFormInput, which ${isInheriting ? 'is' : 'is not'} readonly`, () => {
expect(findGlFormInput().exists()).toBe(true);
expect(findGlFormInput().attributes()).toMatchObject({
type: 'text',
id: 'service_project_url',
name: 'service[project_url]',
2022-04-04 11:22:00 +05:30
placeholder: mockField.placeholder,
2021-12-11 22:18:48 +05:30
required: 'required',
});
expect(findGlFormInput().attributes('readonly')).toBe(readonly);
});
it('does not render other types of input', () => {
expect(findGlFormCheckbox().exists()).toBe(false);
expect(findGlFormSelect().exists()).toBe(false);
expect(findGlFormTextarea().exists()).toBe(false);
});
2021-03-08 18:12:59 +05:30
});
2021-12-11 22:18:48 +05:30
},
);
2020-06-23 00:09:42 +05:30
describe('help text', () => {
it('renders description with help text', () => {
createComponent();
2022-04-04 11:22:00 +05:30
expect(findGlFormGroup().find('small').text()).toBe(mockField.help);
2020-06-23 00:09:42 +05:30
});
2020-07-28 23:09:34 +05:30
2022-03-02 08:16:31 +05:30
describe('when type is checkbox', () => {
it('renders description with help text', () => {
createComponent({
type: 'checkbox',
});
expect(findGlFormGroup().find('small').exists()).toBe(false);
2022-04-04 11:22:00 +05:30
expect(findGlFormCheckbox().text()).toContain(mockField.help);
2022-03-02 08:16:31 +05:30
});
});
2020-07-28 23:09:34 +05:30
it('renders description with help text as HTML', () => {
const helpHTML = 'The <strong>URL</strong> of the project';
createComponent({
help: helpHTML,
});
2021-03-08 18:12:59 +05:30
expect(findGlFormGroup().find('small').html()).toContain(helpHTML);
2020-07-28 23:09:34 +05:30
});
2021-10-27 15:23:28 +05:30
it('strips unsafe HTML from the help text', () => {
const helpHTML =
'[<code>1</code> <iframe>2</iframe> <a href="javascript:alert(document.cookie)">3</a> <a href="foo" target="_blank">4</a>]';
createComponent({
help: helpHTML,
});
expect(findGlFormGroup().find('small').html()).toContain(
'[<code>1</code> <a>3</a> <a target="_blank" href="foo">4</a>]',
);
});
2020-06-23 00:09:42 +05:30
});
describe('label text', () => {
it('renders label with title', () => {
createComponent();
2022-04-04 11:22:00 +05:30
expect(findGlFormGroup().find('label').text()).toBe(mockField.title);
2020-06-23 00:09:42 +05:30
});
});
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
describe('password field validations', () => {
describe('without value', () => {
it('requires validation', () => {
2020-07-28 23:09:34 +05:30
createComponent({
type: 'password',
required: true,
value: null,
2022-04-04 11:22:00 +05:30
isValidated: true,
2020-07-28 23:09:34 +05:30
});
2022-04-04 11:22:00 +05:30
expect(findGlFormGroup().classes('is-invalid')).toBe(true);
expect(findGlFormInput().classes('is-invalid')).toBe(true);
2020-07-28 23:09:34 +05:30
});
2022-04-04 11:22:00 +05:30
});
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
describe('with value', () => {
it('does not require validation', () => {
createComponent({
type: 'password',
required: true,
value: 'test value',
isValidated: true,
2020-07-28 23:09:34 +05:30
});
2022-04-04 11:22:00 +05:30
expect(findGlFormGroup().classes('is-valid')).toBe(true);
expect(findGlFormInput().classes('is-valid')).toBe(true);
2020-07-28 23:09:34 +05:30
});
});
});
2020-06-23 00:09:42 +05:30
});
});