diff --git a/src/amo/constants.js b/src/amo/constants.js index 3534b3dd09e..0f9983fd12f 100644 --- a/src/amo/constants.js +++ b/src/amo/constants.js @@ -328,8 +328,8 @@ export const SPOTLIGHT = 'spotlight'; export const STRATEGIC = 'strategic'; export const ALL_PROMOTED_CATEGORIES = [ - LINE, RECOMMENDED, + LINE, SPOTLIGHT, STRATEGIC, ]; diff --git a/src/amo/utils/addons.js b/src/amo/utils/addons.js index ed7aae57cde..770b08bf33c 100644 --- a/src/amo/utils/addons.js +++ b/src/amo/utils/addons.js @@ -10,6 +10,7 @@ import { FATAL_INSTALL_ERROR, FATAL_UNINSTALL_ERROR, INSTALL_FAILED, + ALL_PROMOTED_CATEGORIES, } from 'amo/constants'; import log from 'amo/logger'; import { getPreviewImage } from 'amo/imageUtils'; @@ -135,12 +136,15 @@ export const getPromotedCategories = ({ }); // Special logic if we're using the category for badging. - if ( - forBadging && - categories.every((item) => !BADGE_CATEGORIES.includes(item)) - ) { - categories = []; + // We shouldn't add badges that are in BADGE_CATEGORIES. + if (forBadging) { + categories = categories.filter((category) => + BADGE_CATEGORIES.includes(category), + ); } - return categories; + return categories.sort( + (a, b) => + ALL_PROMOTED_CATEGORIES.indexOf(a) - ALL_PROMOTED_CATEGORIES.indexOf(b), + ); }; diff --git a/tests/unit/amo/pages/TestAddon.js b/tests/unit/amo/pages/TestAddon.js index 27b1cedd411..cf1a3215b45 100644 --- a/tests/unit/amo/pages/TestAddon.js +++ b/tests/unit/amo/pages/TestAddon.js @@ -27,6 +27,9 @@ import { INCOMPATIBLE_UNSUPPORTED_PLATFORM, INSTALLING, RECOMMENDED, + LINE, + SPOTLIGHT, + STRATEGIC, REVIEWER_TOOLS_VIEW, SET_VIEW_CONTEXT, STATIC_THEMES_REVIEW, @@ -2965,6 +2968,20 @@ describe(__filename, () => { }, ); + it('does not render the strategic or spotlight badges and correctly render the LINE and RECOMMENDED badges in the correct order', () => { + const categories = [LINE, RECOMMENDED, STRATEGIC, SPOTLIGHT]; + addon.promoted = categories.map((category) => ({ + category, + apps: [clientApp], + })); + renderWithAddon(); + const badges = screen.getAllByClassName('PromotedBadge'); + expect(badges).toHaveLength(2); + // Recommended should be on top. + expect(badges[0]).toHaveClass(`PromotedBadge--recommended`); + expect(badges[1]).toHaveClass(`PromotedBadge--line`); + }); + // See https://github.com/mozilla/addons-frontend/issues/8285. it('does not pass an alt property to IconPromotedBadge', () => { renderWithPromotedCategory(); diff --git a/tests/unit/amo/utils/test_addons.js b/tests/unit/amo/utils/test_addons.js index b770410f63c..f16e02a81d8 100644 --- a/tests/unit/amo/utils/test_addons.js +++ b/tests/unit/amo/utils/test_addons.js @@ -249,6 +249,70 @@ describe(__filename, () => { ).toEqual([category]); }); + it('returns multiple categories if the addon is promoted for the specified app', () => { + const categories = [RECOMMENDED, STRATEGIC, SPOTLIGHT]; + const promoted = categories.map((category) => ({ + category, + apps: [CLIENT_APP_ANDROID], + })); + + const addon = createInternalAddonWithLang({ + ...fakeAddon, + promoted, + }); + const suggestion = createInternalSuggestionWithLang( + createFakeAutocompleteResult({ + promoted, + }), + ); + + expect( + getPromotedCategories({ + addon, + clientApp: CLIENT_APP_ANDROID, + }), + ).toEqual(categories); + expect( + getPromotedCategories({ + addon: suggestion, + clientApp: CLIENT_APP_ANDROID, + }), + ).toEqual(categories); + }); + + it('returns the filtered categories if forBadging is True and the addon is promoted for the specified app', () => { + const categories = [RECOMMENDED, STRATEGIC, SPOTLIGHT]; + const promoted = categories.map((category) => ({ + category, + apps: [CLIENT_APP_ANDROID], + })); + + const addon = createInternalAddonWithLang({ + ...fakeAddon, + promoted, + }); + const suggestion = createInternalSuggestionWithLang( + createFakeAutocompleteResult({ + promoted, + }), + ); + + expect( + getPromotedCategories({ + addon, + clientApp: CLIENT_APP_ANDROID, + forBadging: true, + }), + ).toEqual([categories[0]]); + expect( + getPromotedCategories({ + addon: suggestion, + clientApp: CLIENT_APP_ANDROID, + forBadging: true, + }), + ).toEqual([categories[0]]); + }); + it('returns the empty list if the addon is not promoted via null', () => { const addon = createInternalAddonWithLang({ ...fakeAddon,