From 9d0b6083b8d82034428975c2a838aeea1937c90f Mon Sep 17 00:00:00 2001 From: Bizz <56281168+dr-bizz@users.noreply.github.com> Date: Tue, 12 Sep 2023 08:33:31 -0400 Subject: [PATCH] Filter suggestedAmounts to remove zero amounts or empty objects (#1052) * Filter suggested amounts to remove zero amounts or empty objects. * Removed zero amounts initially on giving pages --- .../pageOptionsModal/pageOptions.modal.js | 6 ++--- .../pageOptionsModal/pageOptions.spec.js | 24 ++++++++++++++++++- .../productConfigForm.component.js | 2 +- .../productConfigForm.component.spec.js | 15 +++++++++++- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/app/designationEditor/pageOptionsModal/pageOptions.modal.js b/src/app/designationEditor/pageOptionsModal/pageOptions.modal.js index 4961ba20c..7f267d22e 100644 --- a/src/app/designationEditor/pageOptionsModal/pageOptions.modal.js +++ b/src/app/designationEditor/pageOptionsModal/pageOptions.modal.js @@ -12,7 +12,7 @@ class ModalInstanceCtrl { this.facebookPixelId = facebookPixelId this.suggestedAmounts = transform(suggestedAmounts, (result, value, key) => { - if (key === 'jcr:primaryType') { return } + if (key === 'jcr:primaryType' || !value?.amount) return result.push({ amount: Number(value.amount), description: value.description, @@ -23,9 +23,9 @@ class ModalInstanceCtrl { } transformSuggestedAmounts () { - return transform(this.suggestedAmounts, (result, value, i) => { + const filterOutZeroAmounts = this.suggestedAmounts.filter((amount) => amount?.amount) + return transform(filterOutZeroAmounts, (result, value, i) => { delete value.order - value.amount = value.amount || 0 result[i + 1] = value }, {}) } diff --git a/src/app/designationEditor/pageOptionsModal/pageOptions.spec.js b/src/app/designationEditor/pageOptionsModal/pageOptions.spec.js index a2d5ca7c4..a465597dc 100644 --- a/src/app/designationEditor/pageOptionsModal/pageOptions.spec.js +++ b/src/app/designationEditor/pageOptionsModal/pageOptions.spec.js @@ -15,7 +15,9 @@ describe('Designation Editor Page Options', function () { facebookPixelId: '635334562464', suggestedAmounts: { 'jcr:primaryType': 'nt:unstructured', 1: { 'jcr:primaryType': 'nt:unstructured', description: '1 Bible', amount: 100 }, - 2: { 'jcr:primaryType': 'nt:unstructured', description: '2 Bibles', amount: 200 } }, + 2: { 'jcr:primaryType': 'nt:unstructured', description: '2 Bibles', amount: 200 }, + 3: { 'jcr:primaryType': 'nt:unstructured', description: '2 Bibles', amount: 0 }, + 4: { }, }, $scope: $scope }) })) @@ -42,4 +44,24 @@ describe('Designation Editor Page Options', function () { 2: { amount: 200, description: '2 Bibles' } }) }) + + it('should remove zero or empty objects', function () { + $ctrl.suggestedAmounts.push({ + amount: 0, + description: '3 Bibles', + }); + $ctrl.suggestedAmounts.push({ + amount: 400, + description: '4 Bibles', + }); + $ctrl.suggestedAmounts.push({ description: '5 Bibles' }); + + expect($ctrl.transformSuggestedAmounts()).toEqual({ + 1: { amount: 100, description: '1 Bible' }, + 2: { amount: 200, description: '2 Bibles' }, + 3: { amount: 400, description: '4 Bibles' } + }) + }) + + }) diff --git a/src/app/productConfig/productConfigForm/productConfigForm.component.js b/src/app/productConfig/productConfigForm/productConfigForm.component.js index 588466480..4d6e3a604 100644 --- a/src/app/productConfig/productConfigForm/productConfigForm.component.js +++ b/src/app/productConfig/productConfigForm/productConfigForm.component.js @@ -138,7 +138,7 @@ class ProductConfigFormController { const suggestedAmountsObservable = this.designationsService.suggestedAmounts(this.code, this.itemConfig) .do(suggestedAmounts => { - this.suggestedAmounts = suggestedAmounts + this.suggestedAmounts = suggestedAmounts.filter((amount) => amount?.amount) this.useSuggestedAmounts = !isEmpty(this.suggestedAmounts) }) diff --git a/src/app/productConfig/productConfigForm/productConfigForm.component.spec.js b/src/app/productConfig/productConfigForm/productConfigForm.component.spec.js index 3eef7f5c4..a7588e5ef 100644 --- a/src/app/productConfig/productConfigForm/productConfigForm.component.spec.js +++ b/src/app/productConfig/productConfigForm/productConfigForm.component.spec.js @@ -161,7 +161,7 @@ describe('product config form component', function () { jest.spyOn($ctrl.commonService, 'getNextDrawDate').mockReturnValue(Observable.of('2016-10-02')) - jest.spyOn($ctrl.designationsService, 'suggestedAmounts').mockReturnValue(Observable.of([{ amount: 5 }, { amount: 10 }])) + jest.spyOn($ctrl.designationsService, 'suggestedAmounts').mockReturnValue(Observable.of([{ amount: 5 }, { amount: 10 }, { amount: 0 }, { }])) jest.spyOn($ctrl.designationsService, 'givingLinks').mockReturnValue(Observable.of([])) jest.spyOn($ctrl.analyticsFactory, 'giveGiftModal').mockReturnValue(() => {}) @@ -231,6 +231,19 @@ describe('product config form component', function () { }) }) + + describe('loadData but suggestedAmounts only has 0 amounts or empty objects', () => { + beforeEach(() => { + jest.spyOn($ctrl.designationsService, 'suggestedAmounts').mockReturnValue(Observable.of([{ amount: 0 }, { }])) + }) + + it('should use default amounts', () => { + $ctrl.loadData() + expect($ctrl.suggestedAmounts).toEqual([]) + expect($ctrl.useSuggestedAmounts).toEqual(false) + }) + }) + describe('setDefaultAmount', () => { beforeEach(() => { $ctrl.itemConfig = {}