commit 7306ff6db0c6351139b0c338e6828ac165b86058 Author: Ben Rohlfs Date: Fri Oct 9 21:54:05 2020 +0200 Fix js error when `all` is not set in LabelInfo Change-Id: I3a5627f7ce1f38a7a6b91a0fea712a011126b985 diff --git a/polygerrit-ui/app/types/common.ts b/polygerrit-ui/app/types/common.ts index e71f09b..7d0c9d7 100644 --- a/polygerrit-ui/app/types/common.ts +++ b/polygerrit-ui/app/types/common.ts @@ -170,8 +170,8 @@ export interface QuickLabelInfo extends LabelCommonInfo { * https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#_fields_set_by_code_detailed_labels_code */ export interface DetailedLabelInfo extends LabelCommonInfo { - // Docs claim that 'all' is optional, but it is actually always set. - all: ApprovalInfo[]; + // This is not set when the change has no reviewers. + all?: ApprovalInfo[]; // Docs claim that 'values' is optional, but it is actually always set. values: LabelValueToDescriptionMap; // A map of all values that are allowed for this label default_value?: number; diff --git a/polygerrit-ui/app/utils/label-util.ts b/polygerrit-ui/app/utils/label-util.ts index 78151be..578ae11 100644 --- a/polygerrit-ui/app/utils/label-util.ts +++ b/polygerrit-ui/app/utils/label-util.ts @@ -33,7 +33,7 @@ export function getVotingRange(label?: LabelInfo): VotingRangeInfo { } export function getMaxAccounts(label?: LabelInfo): ApprovalInfo[] { - if (!label || !isDetailedLabelInfo(label)) return []; + if (!label || !isDetailedLabelInfo(label) || !label.all) return []; const votingRange = getVotingRange(label); return label.all.filter(account => account.value === votingRange.max); } diff --git a/polygerrit-ui/app/utils/label-util_test.js b/polygerrit-ui/app/utils/label-util_test.js index 5bd48a1..eadd0a9 100644 --- a/polygerrit-ui/app/utils/label-util_test.js +++ b/polygerrit-ui/app/utils/label-util_test.js @@ -17,33 +17,54 @@ import '../test/common-test-setup-karma.js'; import { - getVotingRange, + getVotingRange, getMaxAccounts, } from './label-util.js'; +const VALUES_1 = { + '-1': 'bad', + '0': 'neutral', + '+1': 'good', +}; + +const VALUES_2 = { + '-1': 'bad', + '+2': 'perfect', + '0': 'neutral', + '-2': 'blocking', + '+1': 'good', +}; + suite('label-util', () => { test('getVotingRange -1 to +1', () => { - const label = { - values: { - '-1': 'bad', - '0': 'neutral', - '+1': 'good', - }, - }; + const label = {values: VALUES_1}; const expectedRange = {min: -1, max: 1}; assert.deepEqual(getVotingRange(label), expectedRange); }); test('getVotingRange -2 to +2', () => { - const label = { - values: { - '-1': 'bad', - '+2': 'perfect', - '0': 'neutral', - '-2': 'blocking', - '+1': 'good', - }, - }; + const label = {values: VALUES_2}; const expectedRange = {min: -2, max: 2}; assert.deepEqual(getVotingRange(label), expectedRange); }); + + test('getMaxAccounts', () => { + const label = { + values: VALUES_2, + all: [ + {value: 2, _account_id: 314}, + {value: 1, _account_id: 777}, + ], + }; + + const maxAccounts = getMaxAccounts(label); + + assert.equal(maxAccounts.length, 1); + assert.equal(maxAccounts[0]._account_id, 314); + }); + + test('getMaxAccounts unset parameters', () => { + assert.isEmpty(getMaxAccounts()); + assert.isEmpty(getMaxAccounts({})); + assert.isEmpty(getMaxAccounts({values: VALUES_2})); + }); });