Commit 7f7d24ee authored by Fernando's avatar Fernando

Add unit tests and run prettier and linter

* Add unit tests that factor in feature flag
parent 86c322d6
...@@ -33,9 +33,8 @@ export default { ...@@ -33,9 +33,8 @@ export default {
* otherwise populate with existing rule * otherwise populate with existing rule
*/ */
return state.data?.initRuleField ? undefined : state.data; return state.data?.initRuleField ? undefined : state.data;
} }
return state.data; return state.data;
}, },
originalData: 'data', originalData: 'data',
}), }),
......
<script> <script>
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin'; import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { mapState, mapActions } from 'vuex'; import { mapState, mapActions } from 'vuex';
import { __, n__, sprintf } from '~/locale'; import { s__, n__, sprintf } from '~/locale';
import { RULE_TYPE_ANY_APPROVER, RULE_TYPE_REGULAR } from '../../constants'; import { RULE_TYPE_ANY_APPROVER, RULE_TYPE_REGULAR } from '../../constants';
import UserAvatarList from '~/vue_shared/components/user_avatar/user_avatar_list.vue'; import UserAvatarList from '~/vue_shared/components/user_avatar/user_avatar_list.vue';
import Rules from '../rules.vue'; import Rules from '../rules.vue';
...@@ -67,21 +67,21 @@ export default { ...@@ -67,21 +67,21 @@ export default {
return [ return [
{ {
name: 'Vulnerability-Check', name: 'Vulnerability-Check',
description: __( description: s__(
'One or more of the security scanners must be enabled %{linkStart}more information%{linkEnd}', 'SecurityApprovals|One or more of the security scanners must be enabled %{linkStart}more information%{linkEnd}',
), ),
enableDescription: __( enableDescription: s__(
'Requires approval for vulnerabilties of Critical, High, or Unknown severity %{linkStart}more information%{linkEnd}', 'SecurityApprovals|Requires approval for vulnerabilties of Critical, High, or Unknown severity %{linkStart}more information%{linkEnd}',
), ),
docsPath: this.vulnerabilityCheckHelpPagePath, docsPath: this.vulnerabilityCheckHelpPagePath,
}, },
{ {
name: 'License-Check', name: 'License-Check',
description: __( description: s__(
'License Scanning must be enabled %{linkStart}more information%{linkEnd}', 'SecurityApprovals|License Scanning must be enabled %{linkStart}more information%{linkEnd}',
), ),
enableDescription: __( enableDescription: s__(
'Requires license policy rules for licenses of Allowed, or Denied %{linkStart}more information%{linkEnd}', 'SecurityApprovals|Requires license policy rules for licenses of Allowed, or Denied %{linkStart}more information%{linkEnd}',
), ),
docsPath: this.licenseCheckHelpPagePath, docsPath: this.licenseCheckHelpPagePath,
}, },
......
...@@ -55,21 +55,20 @@ export default { ...@@ -55,21 +55,20 @@ export default {
containsHiddenGroups: false, containsHiddenGroups: false,
...this.getInitialData(), ...this.getInitialData(),
}; };
} }
return { return {
name: '', name: '',
approvalsRequired: 1, approvalsRequired: 1,
minApprovalsRequired: 0, minApprovalsRequired: 0,
approvers: [], approvers: [],
approversToAdd: [], approversToAdd: [],
branches: [], branches: [],
branchesToAdd: [], branchesToAdd: [],
showValidation: false, showValidation: false,
isFallback: false, isFallback: false,
containsHiddenGroups: false, containsHiddenGroups: false,
...this.getInitialData(), ...this.getInitialData(),
}; };
}, },
computed: { computed: {
...mapState(['settings']), ...mapState(['settings']),
...@@ -168,9 +167,8 @@ export default { ...@@ -168,9 +167,8 @@ export default {
return ( return (
Boolean(this.isPersisted || this.initRuleFieldName) && READONLY_NAMES.includes(this.name) Boolean(this.isPersisted || this.initRuleFieldName) && READONLY_NAMES.includes(this.name)
); );
} }
return this.isPersisted && READONLY_NAMES.includes(this.name); return this.isPersisted && READONLY_NAMES.includes(this.name);
}, },
removeHiddenGroups() { removeHiddenGroups() {
return this.containsHiddenGroups && !this.approversByType[TYPE_HIDDEN_GROUPS]; return this.containsHiddenGroups && !this.approversByType[TYPE_HIDDEN_GROUPS];
......
...@@ -113,4 +113,41 @@ describe('Approvals ModalRuleCreate', () => { ...@@ -113,4 +113,41 @@ describe('Approvals ModalRuleCreate', () => {
expect(form.props('initRule')).toEqual(TEST_RULE); expect(form.props('initRule')).toEqual(TEST_RULE);
}); });
}); });
describe('with approvalSuggestions feature flag', () => {
beforeEach(() => {
createModalState.data = { ...TEST_RULE, initRuleField: true, name: 'Vulnerability-Check' };
factory({
provide: {
glFeatures: { approvalSuggestions: true },
},
});
});
it('renders add rule modal', () => {
const modal = wrapper.find(GlModalVuex);
expect(modal.exists()).toBe(true);
expect(modal.attributes('title')).toEqual('Add approval rule');
expect(modal.attributes('ok-title')).toEqual('Add approval rule');
});
it('renders form with initRuleFieldName', () => {
const modal = wrapper.find(GlModalVuex);
const form = modal.find(RuleForm);
expect(form.props().initRuleFieldName).toBe('Vulnerability-Check');
expect(form.exists()).toBe(true);
});
it('renders the form without passing in an existing rule', () => {
const modal = wrapper.find(GlModalVuex);
const form = modal.find(RuleForm);
expect(form.exists()).toBe(true);
expect(form.props('initRule')).toEqual(null);
});
});
}); });
...@@ -5,6 +5,7 @@ import projectSettingsModule from 'ee/approvals/stores/modules/project_settings' ...@@ -5,6 +5,7 @@ import projectSettingsModule from 'ee/approvals/stores/modules/project_settings'
import ProjectRules from 'ee/approvals/components/project_settings/project_rules.vue'; import ProjectRules from 'ee/approvals/components/project_settings/project_rules.vue';
import RuleInput from 'ee/approvals/components/mr_edit/rule_input.vue'; import RuleInput from 'ee/approvals/components/mr_edit/rule_input.vue';
import UserAvatarList from '~/vue_shared/components/user_avatar/user_avatar_list.vue'; import UserAvatarList from '~/vue_shared/components/user_avatar/user_avatar_list.vue';
import UnconfiguredSecurityRule from 'ee/approvals/components/security_configuration/unconfigured_security_rule.vue';
import { createProjectRules } from '../../mocks'; import { createProjectRules } from '../../mocks';
const TEST_RULES = createProjectRules(); const TEST_RULES = createProjectRules();
...@@ -29,11 +30,12 @@ describe('Approvals ProjectRules', () => { ...@@ -29,11 +30,12 @@ describe('Approvals ProjectRules', () => {
let wrapper; let wrapper;
let store; let store;
const factory = (props = {}) => { const factory = (props = {}, options = {}) => {
wrapper = mount(localVue.extend(ProjectRules), { wrapper = mount(localVue.extend(ProjectRules), {
propsData: props, propsData: props,
store: new Vuex.Store(store), store: new Vuex.Store(store),
localVue, localVue,
...options,
}); });
}; };
...@@ -121,5 +123,35 @@ describe('Approvals ProjectRules', () => { ...@@ -121,5 +123,35 @@ describe('Approvals ProjectRules', () => {
expect(nameCell.find('.js-help').exists()).toBeFalsy(); expect(nameCell.find('.js-help').exists()).toBeFalsy();
}); });
it('should not the unconfigured-security-rule component', () => {
const unconfiguredRules = wrapper.find(UnconfiguredSecurityRule);
expect(unconfiguredRules.exists()).toBe(false);
});
});
describe('when the approvalSuggestions feature flag is enabled', () => {
beforeEach(() => {
const rules = createProjectRules();
rules[0].name = 'Vulnerability-Check';
store.modules.approvals.state.rules = rules;
store.state.settings.allowMultiRule = true;
});
beforeEach(() => {
factory(
{},
{
provide: {
glFeatures: { approvalSuggestions: true },
},
},
);
});
it('should render the unconfigured-security-rule component', () => {
const unconfiguredRules = wrapper.find(UnconfiguredSecurityRule);
expect(unconfiguredRules.exists()).toBe(true);
});
}); });
}); });
...@@ -39,13 +39,13 @@ describe('EE Approvals RuleForm', () => { ...@@ -39,13 +39,13 @@ describe('EE Approvals RuleForm', () => {
let store; let store;
let actions; let actions;
const createComponent = (props = {}) => { const createComponent = (props = {}, options = {}) => {
wrapper = shallowMount(localVue.extend(RuleForm), { wrapper = shallowMount(localVue.extend(RuleForm), {
propsData: props, propsData: props,
store: new Vuex.Store(store), store: new Vuex.Store(store),
localVue, localVue,
provide: { provide: {
glFeatures: { scopedApprovalRules: true }, glFeatures: { scopedApprovalRules: true, ...options.provide?.glFeatures },
}, },
}); });
}; };
...@@ -482,6 +482,68 @@ describe('EE Approvals RuleForm', () => { ...@@ -482,6 +482,68 @@ describe('EE Approvals RuleForm', () => {
}); });
}); });
describe('with approvalSuggestions enabled', () => {
describe('with initRuleFieldName set to Vulnerability-Check', () => {
beforeEach(() => {
createComponent(
{
initRule: null,
initRuleFieldName: 'Vulnerability-Check',
},
{
provide: {
glFeatures: { approvalSuggestions: true },
},
},
);
});
it('it disables the name text field', () => {
expect(findNameInput().attributes('disabled')).toBe('disabled');
});
});
describe('with initRuleFieldName set to License-Check', () => {
beforeEach(() => {
createComponent(
{
initRule: null,
initRuleFieldName: 'License-Check',
},
{
provide: {
glFeatures: { approvalSuggestions: true },
},
},
);
});
it('it disables the name text field', () => {
expect(findNameInput().attributes('disabled')).toBe('disabled');
});
});
describe('with initRuleFieldName set to any other string', () => {
beforeEach(() => {
createComponent(
{
initRule: null,
initRuleFieldName: 'Foo Bar Baz',
},
{
provide: {
glFeatures: { approvalSuggestions: true },
},
},
);
});
it('does not disable the name text field', () => {
expect(findNameInput().attributes('disabled')).toBe(undefined);
});
});
});
describe('with new License-Check rule', () => { describe('with new License-Check rule', () => {
beforeEach(() => { beforeEach(() => {
createComponent({ createComponent({
...@@ -494,6 +556,18 @@ describe('EE Approvals RuleForm', () => { ...@@ -494,6 +556,18 @@ describe('EE Approvals RuleForm', () => {
}); });
}); });
describe('with new Vulnerability-Check rule', () => {
beforeEach(() => {
createComponent({
initRule: { ...TEST_RULE, id: null, name: 'Vulnerability-Check' },
});
});
it('does not disable the name text field', () => {
expect(findNameInput().attributes('disabled')).toBe(undefined);
});
});
describe('with editing the License-Check rule', () => { describe('with editing the License-Check rule', () => {
beforeEach(() => { beforeEach(() => {
createComponent({ createComponent({
...@@ -505,6 +579,18 @@ describe('EE Approvals RuleForm', () => { ...@@ -505,6 +579,18 @@ describe('EE Approvals RuleForm', () => {
expect(findNameInput().attributes('disabled')).toBe('disabled'); expect(findNameInput().attributes('disabled')).toBe('disabled');
}); });
}); });
describe('with editing the Vulnerability-Check rule', () => {
beforeEach(() => {
createComponent({
initRule: { ...TEST_RULE, name: 'Vulnerability-Check' },
});
});
it('disables the name text field', () => {
expect(findNameInput().attributes('disabled')).toBe('disabled');
});
});
}); });
describe('when allow only single rule', () => { describe('when allow only single rule', () => {
......
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