Commit 632aefed authored by Frédéric Caplette's avatar Frédéric Caplette Committed by Simon Knox

Disable the PA commit button when there are no changes

Before we allowed a user to commits with an empty diff.
Now if the user has not changed anything, they cannot
commit their file.

Changelog: changed
parent e09a291d
...@@ -31,6 +31,14 @@ export default { ...@@ -31,6 +31,14 @@ export default {
required: false, required: false,
default: '', default: '',
}, },
hasUnsavedChanges: {
type: Boolean,
required: true,
},
isNewCiConfigFile: {
type: Boolean,
required: true,
},
isSaving: { isSaving: {
type: Boolean, type: Boolean,
required: false, required: false,
...@@ -50,11 +58,14 @@ export default { ...@@ -50,11 +58,14 @@ export default {
}; };
}, },
computed: { computed: {
isCommitFormFilledOut() {
return this.message && this.targetBranch;
},
isCurrentBranchTarget() { isCurrentBranchTarget() {
return this.targetBranch === this.currentBranch; return this.targetBranch === this.currentBranch;
}, },
submitDisabled() { isSubmitDisabled() {
return !(this.message && this.targetBranch); return !this.isCommitFormFilledOut || (!this.hasUnsavedChanges && !this.isNewCiConfigFile);
}, },
}, },
watch: { watch: {
...@@ -143,7 +154,7 @@ export default { ...@@ -143,7 +154,7 @@ export default {
category="primary" category="primary"
variant="confirm" variant="confirm"
data-qa-selector="commit_changes_button" data-qa-selector="commit_changes_button"
:disabled="submitDisabled" :disabled="isSubmitDisabled"
:loading="isSaving" :loading="isSaving"
> >
{{ $options.i18n.commitChanges }} {{ $options.i18n.commitChanges }}
......
...@@ -37,6 +37,10 @@ export default { ...@@ -37,6 +37,10 @@ export default {
required: false, required: false,
default: '', default: '',
}, },
hasUnsavedChanges: {
type: Boolean,
required: true,
},
isNewCiConfigFile: { isNewCiConfigFile: {
type: Boolean, type: Boolean,
required: false, required: false,
...@@ -151,6 +155,8 @@ export default { ...@@ -151,6 +155,8 @@ export default {
<commit-form <commit-form
:current-branch="currentBranch" :current-branch="currentBranch"
:default-message="defaultCommitMessage" :default-message="defaultCommitMessage"
:has-unsaved-changes="hasUnsavedChanges"
:is-new-ci-config-file="isNewCiConfigFile"
:is-saving="isSaving" :is-saving="isSaving"
:scroll-to-commit-form="scrollToCommitForm" :scroll-to-commit-form="scrollToCommitForm"
v-on="$listeners" v-on="$listeners"
......
...@@ -131,6 +131,7 @@ export default { ...@@ -131,6 +131,7 @@ export default {
:ref="$options.commitSectionRef" :ref="$options.commitSectionRef"
:ci-file-content="ciFileContent" :ci-file-content="ciFileContent"
:commit-sha="commitSha" :commit-sha="commitSha"
:has-unsaved-changes="hasUnsavedChanges"
:is-new-ci-config-file="isNewCiConfigFile" :is-new-ci-config-file="isNewCiConfigFile"
:scroll-to-commit-form="scrollToCommitForm" :scroll-to-commit-form="scrollToCommitForm"
@scrolled-to-commit-form="setScrollToCommitForm(false)" @scrolled-to-commit-form="setScrollToCommitForm(false)"
......
...@@ -55,6 +55,10 @@ RSpec.describe 'Pipeline Editor', :js do ...@@ -55,6 +55,10 @@ RSpec.describe 'Pipeline Editor', :js do
it 'displays new branch as selected after commiting on a new branch' do it 'displays new branch as selected after commiting on a new branch' do
find('#target-branch-field').set('new_branch', clear: :backspace) find('#target-branch-field').set('new_branch', clear: :backspace)
page.within('#source-editor-') do
find('textarea').send_keys '123'
end
click_button 'Commit changes' click_button 'Commit changes'
page.within('[data-testid="branch-selector"]') do page.within('[data-testid="branch-selector"]') do
......
...@@ -17,6 +17,8 @@ describe('Pipeline Editor | Commit Form', () => { ...@@ -17,6 +17,8 @@ describe('Pipeline Editor | Commit Form', () => {
propsData: { propsData: {
defaultMessage: mockCommitMessage, defaultMessage: mockCommitMessage,
currentBranch: mockDefaultBranch, currentBranch: mockDefaultBranch,
hasUnsavedChanges: true,
isNewCiConfigFile: false,
...props, ...props,
}, },
...@@ -82,6 +84,27 @@ describe('Pipeline Editor | Commit Form', () => { ...@@ -82,6 +84,27 @@ describe('Pipeline Editor | Commit Form', () => {
}); });
}); });
describe('submit button', () => {
it.each`
hasUnsavedChanges | isNewCiConfigFile | isDisabled | btnState
${false} | ${false} | ${true} | ${'disabled'}
${true} | ${false} | ${false} | ${'enabled'}
${true} | ${true} | ${false} | ${'enabled'}
${false} | ${true} | ${false} | ${'enabled'}
`(
'is $btnState when hasUnsavedChanges:$hasUnsavedChanges and isNewCiConfigfile:$isNewCiConfigFile',
({ hasUnsavedChanges, isNewCiConfigFile, isDisabled }) => {
createComponent({ props: { hasUnsavedChanges, isNewCiConfigFile } });
if (isDisabled) {
expect(findSubmitBtn().attributes('disabled')).toBe('true');
} else {
expect(findSubmitBtn().attributes('disabled')).toBeUndefined();
}
},
);
});
describe('when user inputs values', () => { describe('when user inputs values', () => {
const anotherMessage = 'Another commit message'; const anotherMessage = 'Another commit message';
const anotherBranch = 'my-branch'; const anotherBranch = 'my-branch';
......
...@@ -51,6 +51,7 @@ describe('Pipeline Editor | Commit section', () => { ...@@ -51,6 +51,7 @@ describe('Pipeline Editor | Commit section', () => {
const defaultProps = { const defaultProps = {
ciFileContent: mockCiYml, ciFileContent: mockCiYml,
commitSha: mockCommitSha, commitSha: mockCommitSha,
hasUnsavedChanges: true,
isNewCiConfigFile: false, isNewCiConfigFile: 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