Fix Attach AI fixes on Save Button click
Change 420297 introduced bug what AI fixes are not added to draft
object when user clicks on Save button; they are added only by
auto-save, so it works when auto-save is triggered, but not on quick
comments where user clicks on save. This change removes
the buggy code that caused the issue of not attaching AI fixes
after user clicks on Save.
Save() is setting editing to false before calling somethingToSave()
which calls isFixSuggestionChanged(). So editing is always false
in isFixSuggestionChanged called by save() and it makes no sense to
return false when !this.editing in isFixSuggestionChanged().
Added unit tests that check adding AI fixes on user clicks. One fails
without this change.
Google-Bug-Id: b/343632982
Release-Notes: skip
Change-Id: Ic404fa17285d97b56b2f8d222f064b9f0780ec67
diff --git a/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts b/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
index 3d76c82..9313739 100644
--- a/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
+++ b/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
@@ -1921,9 +1921,6 @@
}
isFixSuggestionChanged(): boolean {
- // Check to not change fix suggestion when draft is not being edited only
- // when user quickly disable generating suggestions and click save
- if (!this.editing && this.generateSuggestion) return false;
return !deepEqual(this.comment?.fix_suggestions, this.getFixSuggestions());
}
diff --git a/polygerrit-ui/app/elements/shared/gr-comment/gr-comment_test.ts b/polygerrit-ui/app/elements/shared/gr-comment/gr-comment_test.ts
index 444a43f..5273439 100644
--- a/polygerrit-ui/app/elements/shared/gr-comment/gr-comment_test.ts
+++ b/polygerrit-ui/app/elements/shared/gr-comment/gr-comment_test.ts
@@ -40,7 +40,7 @@
import {GrConfirmDeleteCommentDialog} from '../gr-confirm-delete-comment-dialog/gr-confirm-delete-comment-dialog';
import {assertIsDefined} from '../../../utils/common-util';
import {Key, Modifier} from '../../../utils/dom-util';
-import {SinonStubbedMember} from 'sinon';
+import {SinonStub, SinonStubbedMember} from 'sinon';
import {fixture, html, assert} from '@open-wc/testing';
import {GrButton} from '../gr-button/gr-button';
import {testResolver} from '../../../test/common-test-setup';
@@ -49,6 +49,7 @@
commentsModelToken,
} from '../../../models/comments/comments-model';
import {KnownExperimentId} from '../../../services/flags/flags';
+import {GrSuggestionDiffPreview} from '../gr-suggestion-diff-preview/gr-suggestion-diff-preview';
suite('gr-comment tests', () => {
let element: GrComment;
@@ -1123,5 +1124,72 @@
/* HTML */ '<gr-suggestion-diff-preview id="suggestionDiffPreview"> </gr-suggestion-diff-preview>'
);
});
+
+ suite('save', () => {
+ const savePromise = mockPromise<DraftInfo>();
+ let saveDraftStub: SinonStub;
+ const textToSave = 'something, not important';
+ setup(async () => {
+ const comment = createDraft();
+ element = await fixture(
+ html`<gr-comment
+ .account=${account}
+ .showPatchset=${true}
+ .comment=${comment}
+ .initiallyCollapsed=${false}
+ ></gr-comment>`
+ );
+ saveDraftStub = sinon
+ .stub(commentsModel, 'saveDraft')
+ .returns(savePromise);
+ sinon.stub(element, 'showGeneratedSuggestion').returns(true);
+ element.editing = true;
+ await element.updateComplete;
+ element.messageText = textToSave;
+ element.unresolved = true;
+ element.generateSuggestion = true;
+ element.generatedFixSuggestion = generatedFixSuggestion;
+ await element.updateComplete;
+ });
+
+ test('save fix suggestion when previewed', async () => {
+ const suggestionDiffPreview = queryAndAssert<GrSuggestionDiffPreview>(
+ element,
+ '#suggestionDiffPreview'
+ );
+ suggestionDiffPreview.previewed = true;
+ await element.updateComplete;
+ element.save();
+ await element.updateComplete;
+ waitUntilCalled(saveDraftStub, 'saveDraft()');
+ assert.equal(
+ saveDraftStub.lastCall.firstArg.fix_suggestions[0]?.fix_id,
+ generatedFixSuggestion.fix_id
+ );
+ assert.isFalse(element.editing);
+
+ savePromise.resolve();
+ });
+
+ test("don't save fix suggestion when not previewed", async () => {
+ const suggestionDiffPreview = queryAndAssert<GrSuggestionDiffPreview>(
+ element,
+ '#suggestionDiffPreview'
+ );
+ suggestionDiffPreview.previewed = false;
+ await element.updateComplete;
+ element.save();
+ await element.updateComplete;
+ waitUntilCalled(saveDraftStub, 'saveDraft()');
+ assert.equal(saveDraftStub.lastCall.firstArg.message, textToSave);
+ assert.equal(
+ saveDraftStub.lastCall.firstArg.fix_suggestions,
+ undefined
+ );
+ assert.isFalse(element.editing);
+
+ savePromise.resolve();
+ });
+ });
});
});