Commit a5180ac3 authored by Fernando's avatar Fernando

Apply code review feedback

* Add conditional rendering if configuration provided
* Support enable/disable checkbox
parent 48cb4dfe
<script> <script>
import { cloneDeep } from 'lodash';
import { GlFormCheckbox, GlFormGroup } from '@gitlab/ui'; import { GlFormCheckbox, GlFormGroup } from '@gitlab/ui';
import DynamicFields from './dynamic_fields.vue'; import DynamicFields from './dynamic_fields.vue';
import { isValidAnalyzerEntity } from './utils'; import { isValidAnalyzerEntity } from './utils';
export default { export default {
components: { components: {
GlFormGroup, GlFormGroup,
...@@ -15,11 +13,6 @@ export default { ...@@ -15,11 +13,6 @@ export default {
prop: 'entity', prop: 'entity',
event: 'input', event: 'input',
}, },
data() {
return {
configurationEntities: cloneDeep(this.entity.configuration),
};
},
props: { props: {
// SastCiConfigurationAnalyzersEntity from GraphQL endpoint // SastCiConfigurationAnalyzersEntity from GraphQL endpoint
entity: { entity: {
...@@ -28,15 +21,20 @@ export default { ...@@ -28,15 +21,20 @@ export default {
validator: isValidAnalyzerEntity, validator: isValidAnalyzerEntity,
}, },
}, },
computed: {
hasConfiguration() {
return this.entity.configuration?.length > 0;
},
},
methods: { methods: {
onToggle(value) { onToggle(value) {
const entity = { ...this.entity, enabled: value }; const entity = { ...this.entity, enabled: value };
this.$emit('input', entity); this.$emit('input', entity);
}, },
onConfigurationUpdate(value) { onConfigurationUpdate(configuration) {
const entity = { ...this.entity, configuration: formEntities} const entity = { ...this.entity, configuration };
this.$emit('input', entity) this.$emit('input', entity);
} },
}, },
}; };
</script> </script>
...@@ -48,6 +46,12 @@ export default { ...@@ -48,6 +46,12 @@ export default {
<span v-if="entity.description" class="gl-text-gray-500">({{ entity.description }})</span> <span v-if="entity.description" class="gl-text-gray-500">({{ entity.description }})</span>
</gl-form-checkbox> </gl-form-checkbox>
<dynamic-fields v-model="configurationEntities" @input="onConfigurationUpdate" /> <dynamic-fields
v-if="hasConfiguration"
:disabled="!entity.enabled"
class="gl-ml-6"
:entities="entity.configuration"
@input="onConfigurationUpdate"
/>
</gl-form-group> </gl-form-group>
</template> </template>
...@@ -6,18 +6,11 @@ import { __, s__ } from '~/locale'; ...@@ -6,18 +6,11 @@ import { __, s__ } from '~/locale';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import { redirectTo } from '~/lib/utils/url_utility'; import { redirectTo } from '~/lib/utils/url_utility';
import DynamicFields from './dynamic_fields.vue'; import DynamicFields from './dynamic_fields.vue';
// START TODO: Only for debugging
import AnalyzerConfiguration from './analyzer_configuration.vue';
// END TODO
import { isValidConfigurationEntity } from './utils'; import { isValidConfigurationEntity } from './utils';
import analyzer_configurationVue from './analyzer_configuration.vue';
export default { export default {
components: { components: {
DynamicFields, DynamicFields,
// START TODO: Only for debugging
AnalyzerConfiguration,
// END TODO
GlAlert, GlAlert,
GlButton, GlButton,
}, },
...@@ -41,17 +34,6 @@ export default { ...@@ -41,17 +34,6 @@ export default {
data() { data() {
return { return {
formEntities: cloneDeep(this.entities), formEntities: cloneDeep(this.entities),
// START TODO: Only for debugging
analyzerEntity: {
name: 'name',
label: 'label',
description: 'description',
enabled: true,
configuration: [{
defaultValue:'def', description: 'desc', field:'foo', type: 'string', value:'val', label: 'label'
}]
},
// END TODO
hasSubmissionError: false, hasSubmissionError: false,
isSubmitting: false, isSubmitting: false,
}; };
...@@ -99,10 +81,6 @@ export default { ...@@ -99,10 +81,6 @@ export default {
<form @submit.prevent="onSubmit"> <form @submit.prevent="onSubmit">
<dynamic-fields v-model="formEntities" /> <dynamic-fields v-model="formEntities" />
<!-- START TODO: Only for debugging -->
<analyzer-configuration v-model="analyzerEntity" />
<!-- END TODO -->
<hr /> <hr />
<gl-alert v-if="hasSubmissionError" class="gl-mb-5" variant="danger" :dismissible="false">{{ <gl-alert v-if="hasSubmissionError" class="gl-mb-5" variant="danger" :dismissible="false">{{
......
<script> <script>
import { GlFormGroup } from '@gitlab/ui';
import FormInput from './form_input.vue'; import FormInput from './form_input.vue';
import { isValidConfigurationEntity } from './utils'; import { isValidConfigurationEntity } from './utils';
export default { export default {
components: { components: {
GlFormGroup,
FormInput, FormInput,
}, },
model: { model: {
...@@ -47,7 +49,7 @@ export default { ...@@ -47,7 +49,7 @@ export default {
</script> </script>
<template> <template>
<div> <gl-form-group>
<component <component
:is="componentForEntity(entity)" :is="componentForEntity(entity)"
v-for="entity in entities" v-for="entity in entities"
...@@ -56,5 +58,5 @@ export default { ...@@ -56,5 +58,5 @@ export default {
v-bind="entity" v-bind="entity"
@input="onInput(entity.field, $event)" @input="onInput(entity.field, $event)"
/> />
</div> </gl-form-group>
</template> </template>
import { mount } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import AnalyzerConfiguration from 'ee/security_configuration/sast/components/analyzer_configuration.vue'; import AnalyzerConfiguration from 'ee/security_configuration/sast/components/analyzer_configuration.vue';
import DynamicFields from 'ee/security_configuration/sast/components/dynamic_fields.vue';
describe('AnalyzerConfiguration component', () => { describe('AnalyzerConfiguration component', () => {
let wrapper; let wrapper;
...@@ -66,4 +67,49 @@ describe('AnalyzerConfiguration component', () => { ...@@ -66,4 +67,49 @@ describe('AnalyzerConfiguration component', () => {
}); });
}); });
}); });
describe('configuration form', () => {
describe('when there are no SastCiConfigurationEntity', () => {
beforeEach(() => {
createComponent({
props: { entity },
});
});
it('does not render the nested dynamic forms', () => {
expect(wrapper.find(DynamicFields).exists()).toBe(false);
});
});
describe('when there are one or more SastCiConfigurationEntity', () => {
const analyzerEntity = {
...entity,
enabled: false,
configuration: [
{
defaultValue: 'defaultVal',
description: 'desc',
field: 'field',
type: 'string',
value: 'val',
label: 'label',
},
],
};
beforeEach(() => {
createComponent({
props: { entity: analyzerEntity },
});
});
it('it renders the nested dynamic forms', () => {
expect(wrapper.find(DynamicFields).exists()).toBe(true);
});
it('passes the disabled prop to dynamic fields component', () => {
expect(wrapper.find(DynamicFields).vm.$attrs.disabled).toBe(!analyzerEntity.enabled);
});
});
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment