diff --git a/src/reducers/sponsors/__tests__/sponsor-customized-form-reducer.test.js b/src/reducers/sponsors/__tests__/sponsor-customized-form-reducer.test.js new file mode 100644 index 000000000..10d4102e1 --- /dev/null +++ b/src/reducers/sponsors/__tests__/sponsor-customized-form-reducer.test.js @@ -0,0 +1,105 @@ +import sponsorCustomizedFormReducer from "../sponsor-customized-form-reducer"; +import { + RECEIVE_SPONSOR_CUSTOMIZED_FORM, + RESET_SPONSOR_CUSTOMIZED_FORM +} from "../../../actions/sponsor-forms-actions"; + +const DEFAULT_ENTITY = { + id: 0, + code: "", + name: "", + allowed_add_ons: [], + opens_at: "", + expires_at: "", + instructions: "", + meta_fields: [], + items: [] +}; + +const DEFAULT_STATE = { entity: DEFAULT_ENTITY }; + +describe("sponsorCustomizedFormReducer", () => { + describe("RECEIVE_SPONSOR_CUSTOMIZED_FORM", () => { + it("maps file_url to file_path for images on each item", () => { + const result = sponsorCustomizedFormReducer(DEFAULT_STATE, { + type: RECEIVE_SPONSOR_CUSTOMIZED_FORM, + payload: { + response: { + id: 1, + code: "FORM1", + items: [ + { + id: 10, + name: "Item A", + images: [ + { id: 100, file_url: "https://example.com/img1.png" }, + { id: 101, file_url: "https://example.com/img2.png" } + ] + } + ] + } + } + }); + + expect(result.entity.items[0].images).toEqual([ + { + id: 100, + file_url: "https://example.com/img1.png", + file_path: "https://example.com/img1.png" + }, + { + id: 101, + file_url: "https://example.com/img2.png", + file_path: "https://example.com/img2.png" + } + ]); + }); + + it("handles items with no images", () => { + const result = sponsorCustomizedFormReducer(DEFAULT_STATE, { + type: RECEIVE_SPONSOR_CUSTOMIZED_FORM, + payload: { + response: { + id: 1, + code: "FORM1", + items: [{ id: 10, name: "Item A", images: [] }] + } + } + }); + + expect(result.entity.items[0].images).toEqual([]); + }); + + it("handles empty items array", () => { + const result = sponsorCustomizedFormReducer(DEFAULT_STATE, { + type: RECEIVE_SPONSOR_CUSTOMIZED_FORM, + payload: { + response: { id: 1, code: "FORM1", items: [] } + } + }); + + expect(result.entity.items).toEqual([]); + }); + + it("handles missing items field", () => { + const result = sponsorCustomizedFormReducer(DEFAULT_STATE, { + type: RECEIVE_SPONSOR_CUSTOMIZED_FORM, + payload: { + response: { id: 1, code: "FORM1" } + } + }); + + expect(result.entity.items).toEqual([]); + }); + }); + + describe("RESET_SPONSOR_CUSTOMIZED_FORM", () => { + it("resets to default state", () => { + const dirty = { entity: { id: 99, items: [{ id: 1 }] } }; + const result = sponsorCustomizedFormReducer(dirty, { + type: RESET_SPONSOR_CUSTOMIZED_FORM + }); + expect(result).toEqual(DEFAULT_STATE); + }); + }); +}); diff --git a/src/reducers/sponsors/sponsor-customized-form-items-list-reducer.js b/src/reducers/sponsors/sponsor-customized-form-items-list-reducer.js index c181d463a..5ca54c7d7 100644 --- a/src/reducers/sponsors/sponsor-customized-form-items-list-reducer.js +++ b/src/reducers/sponsors/sponsor-customized-form-items-list-reducer.js @@ -110,6 +110,10 @@ const sponsorCustomizedFormItemsListReducer = ( const currentItem = { ...item, + images: (item.images || []).map((img) => ({ + ...img, + file_path: img.file_url + })), meta_fields: item.meta_fields.length > 0 ? item.meta_fields : [] }; return { ...state, currentItem }; diff --git a/src/reducers/sponsors/sponsor-customized-form-reducer.js b/src/reducers/sponsors/sponsor-customized-form-reducer.js index 9a2d22a36..807ddad98 100644 --- a/src/reducers/sponsors/sponsor-customized-form-reducer.js +++ b/src/reducers/sponsors/sponsor-customized-form-reducer.js @@ -26,7 +26,8 @@ const DEFAULT_ENTITY = { opens_at: "", expires_at: "", instructions: "", - meta_fields: [] + meta_fields: [], + items: [] }; const DEFAULT_STATE = { @@ -43,10 +44,17 @@ const sponsorCustomizedFormReducer = (state = DEFAULT_STATE, action) => { return DEFAULT_STATE; } case RECEIVE_SPONSOR_CUSTOMIZED_FORM: { - return { - ...state, - entity: payload.response + const entity = { + ...payload.response, + items: (payload.response.items || []).map((it) => ({ + ...it, + images: (it.images || []).map((img) => ({ + ...img, + file_path: img.file_url + })) + })) }; + return { ...state, entity }; } default: return state;