diff --git a/src/utils/__tests__/methods.test.js b/src/utils/__tests__/methods.test.js index 32fee9298..ac5275c43 100644 --- a/src/utils/__tests__/methods.test.js +++ b/src/utils/__tests__/methods.test.js @@ -86,6 +86,24 @@ describe("getMediaInputValue", () => { }); }); + it.each([[], null, undefined])( + "should return apply_to_all true when allSelected is true and items is %s", + (items) => { + expect( + normalizeSelectAllField(items, "apply_to_all", "items", true) + ).toEqual({ apply_to_all: true, items: [] }); + } + ); + + it.each([[], null, undefined])( + "should return apply_to_all false when allSelected is false and items is %s", + (items) => { + expect( + normalizeSelectAllField(items, "apply_to_all", "items", false) + ).toEqual({ apply_to_all: false, items: [] }); + } + ); + it("should return array of ids when items are objects with id", () => { expect( normalizeSelectAllField([{ id: 1 }, { id: 2 }], "apply_to_all", "items") diff --git a/src/utils/methods.js b/src/utils/methods.js index b52505028..cb93e33d2 100644 --- a/src/utils/methods.js +++ b/src/utils/methods.js @@ -601,19 +601,19 @@ export const normalizeSelectAllField = ( listName, allSelected ) => { - if (!items?.length) + const isAllSelected = allSelected ?? items?.includes("all"); + if (isAllSelected) { return { - [flagName]: false, + [flagName]: true, [listName]: [] }; + } - const isAllSelected = allSelected ?? items.includes("all"); - if (isAllSelected) { + if (!items?.length) return { - [flagName]: true, + [flagName]: false, [listName]: [] }; - } return { [flagName]: false, [listName]: items.map((a) => a.id ?? a)