Fix bug where changing preferred email results in duplicate emails
While changing preferred email, the client-side cache is incorrectly
updated, resulting in duplicate emails in the cache. Fix the issue by
updating the relevant logic.
Bug: Issue 304335624
Release-Notes: Changing preferred email does not result in duplicate emails
Change-Id: Ifea0d2a91426acc27b8b89720b39c858f859f31e
diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.ts b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.ts
index d468cf9..8c4ac6d 100644
--- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.ts
+++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.ts
@@ -818,9 +818,9 @@
if (cachedEmails) {
const emails = cachedEmails.map(entry => {
if (entry.email === email) {
- return {email, preferred: true};
+ return {email: entry.email, preferred: true};
} else {
- return {email};
+ return {email: entry.email, preferred: false};
}
});
this._cache.set('/accounts/self/emails', emails);
diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.js b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.js
index df37502..9c096bf 100644
--- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.js
+++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.js
@@ -458,6 +458,31 @@
assert.deepEqual(sendStub.lastCall.args[0].body, {token: 'foo'});
});
+ test('setPreferredAccountEmail', () => {
+ const email1 = 'email1@example.com';
+ const email2 = 'email2@example.com';
+ const encodedEmail = encodeURIComponent(email2);
+ const sendStub = sinon.stub(element._restApiHelper, 'send').resolves();
+ element._cache.set('/accounts/self/emails', [
+ {email: email1, preferred: true},
+ {email: email2, preferred: false},
+ ]);
+
+ return element.setPreferredAccountEmail(email2).then(() => {
+ assert.isTrue(sendStub.calledOnce);
+ assert.equal(sendStub.lastCall.args[0].method, 'PUT');
+ assert.equal(sendStub.lastCall.args[0].url,
+ `/accounts/self/emails/${encodedEmail}/preferred`
+ );
+ assert.deepEqual(
+ element._restApiHelper._cache.get('/accounts/self/emails'), [
+ {email: email1, preferred: false},
+ {email: email2, preferred: true},
+ ]
+ );
+ });
+ });
+
test('setAccountStatus', () => {
const sendStub = sinon.stub(element._restApiHelper, 'send')
.returns(Promise.resolve('OOO'));