Commit 6ae64a37 authored by ap4y's avatar ap4y

Implement rule removal in policy editor

This commit adds a new button into the rule builder component that
removes rule block from the policy editor.
parent 9668f3dc
...@@ -120,6 +120,9 @@ export default { ...@@ -120,6 +120,9 @@ export default {
const rule = this.policy.rules[ruleIdx]; const rule = this.policy.rules[ruleIdx];
this.policy.rules.splice(ruleIdx, 1, buildRule(ruleType, rule)); this.policy.rules.splice(ruleIdx, 1, buildRule(ruleType, rule));
}, },
removeRule(ruleIdx) {
this.policy.rules.splice(ruleIdx, 1);
},
loadYaml(manifest) { loadYaml(manifest) {
this.yamlEditorValue = manifest; this.yamlEditorValue = manifest;
this.yamlEditorError = null; this.yamlEditorError = null;
...@@ -233,6 +236,7 @@ export default { ...@@ -233,6 +236,7 @@ export default {
@rule-type-change="updateRuleType(idx, $event)" @rule-type-change="updateRuleType(idx, $event)"
@endpoint-match-mode-change="updateEndpointMatchMode" @endpoint-match-mode-change="updateEndpointMatchMode"
@endpoint-labels-change="updateEndpointLabels" @endpoint-labels-change="updateEndpointLabels"
@remove="removeRule(idx)"
/> />
<div class="gl-p-3 gl-rounded-base gl-border-1 gl-border-solid gl-border-gray-100 gl-mb-5"> <div class="gl-p-3 gl-rounded-base gl-border-1 gl-border-solid gl-border-gray-100 gl-mb-5">
......
<script> <script>
import { GlSprintf, GlForm, GlFormSelect, GlFormInput } from '@gitlab/ui'; import { GlSprintf, GlForm, GlFormSelect, GlFormInput, GlButton } from '@gitlab/ui';
import { s__ } from '~/locale'; import { s__ } from '~/locale';
import { import {
RuleTypeNetwork, RuleTypeNetwork,
...@@ -25,6 +25,7 @@ export default { ...@@ -25,6 +25,7 @@ export default {
GlForm, GlForm,
GlFormSelect, GlFormSelect,
GlFormInput, GlFormInput,
GlButton,
PolicyRuleEndpoint, PolicyRuleEndpoint,
PolicyRuleEntity, PolicyRuleEntity,
'policy-rule-cidr': PolicyRuleCIDR, 'policy-rule-cidr': PolicyRuleCIDR,
...@@ -131,7 +132,7 @@ export default { ...@@ -131,7 +132,7 @@ export default {
<template> <template>
<div <div
class="gl-bg-gray-10 gl-border-solid gl-border-1 gl-border-gray-100 gl-rounded-base px-3 pt-3" class="gl-bg-gray-10 gl-border-solid gl-border-1 gl-border-gray-100 gl-rounded-base px-3 pt-3 gl-relative"
> >
<gl-form inline @submit.prevent> <gl-form inline @submit.prevent>
<gl-sprintf :message="sprintfTemplate"> <gl-sprintf :message="sprintfTemplate">
...@@ -226,5 +227,13 @@ export default { ...@@ -226,5 +227,13 @@ export default {
</template> </template>
</gl-sprintf> </gl-sprintf>
</gl-form> </gl-form>
<gl-button
icon="remove"
size="small"
class="gl-absolute gl-top-3 gl-right-3"
data-testid="remove-rule"
@click="$emit('remove')"
/>
</div> </div>
</template> </template>
...@@ -171,6 +171,16 @@ spec: ...@@ -171,6 +171,16 @@ spec:
}); });
}); });
it('removes a new rule', async () => {
findAddRuleButton().vm.$emit('click');
await wrapper.vm.$nextTick();
expect(wrapper.findAll(PolicyRuleBuilder).length).toEqual(1);
wrapper.find(PolicyRuleBuilder).vm.$emit('remove');
await wrapper.vm.$nextTick();
expect(wrapper.findAll(PolicyRuleBuilder).length).toEqual(0);
});
it('updates yaml editor value on switch to yaml editor', async () => { it('updates yaml editor value on switch to yaml editor', async () => {
findPolicyName().vm.$emit('input', 'test-policy'); findPolicyName().vm.$emit('input', 'test-policy');
wrapper.find("[data-testid='editor-mode']").vm.$emit('input', EditorModeYAML); wrapper.find("[data-testid='editor-mode']").vm.$emit('input', EditorModeYAML);
......
...@@ -103,6 +103,11 @@ describe('PolicyRuleBuilder component', () => { ...@@ -103,6 +103,11 @@ describe('PolicyRuleBuilder component', () => {
expect(event[0]).toEqual([RuleTypeEntity]); expect(event[0]).toEqual([RuleTypeEntity]);
}); });
it('emits remove upon remove-button click', () => {
wrapper.find("[data-testid='remove-rule']").trigger('click');
expect(wrapper.emitted().remove.length).toEqual(1);
});
it('renders only endpoint rule component', () => { it('renders only endpoint rule component', () => {
expect(findRuleEndpoint().exists()).toBe(true); expect(findRuleEndpoint().exists()).toBe(true);
expect(findRuleEntity().exists()).toBe(false); expect(findRuleEntity().exists()).toBe(false);
......
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