Skip to content

Commit

Permalink
chore: generic data handling for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Nov 14, 2023
1 parent d1f8ddd commit 3e4eb77
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
9 changes: 3 additions & 6 deletions src/app/core/services/basket/basket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ export type BasketUpdateType =
| { messageToMerchant: string };

export type BasketItemUpdateType =
| {
quantity?: { value: number; unit: string };
product?: string;
partialOrderNo?: string;
customerProductID?: string;
}
| { quantity?: { value: number; unit: string }; product?: string }
| { customerProductID?: string }
| { partialOrderNo?: string }
| { shippingMethod?: { id: string } }
| { desiredDelivery?: string }
| { calculated: boolean };
Expand Down
53 changes: 40 additions & 13 deletions src/app/core/store/customer/basket/basket-items.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,21 +290,19 @@ describe('Basket Items Effects', () => {
});

it('should call the basketService for updateBasketItem if quantity > 0', done => {
const itemId = 'BIID';
const payload = {
lineItemUpdates: [
{
customerProductID: 'testProductID',
partialOrderNo: 'partialTestOrderID',
itemId: 'BIID',
itemId,
quantity: 2,
},
{
customerProductID: 'testProductID',
itemId: 'BIID',
itemId,
quantity: 3,
},
{
itemId: 'BIID',
itemId,
quantity: 4,
},
],
Expand All @@ -313,13 +311,11 @@ describe('Basket Items Effects', () => {
actions$ = of(action);

effects.updateBasketItems$.subscribe(() => {
verify(basketServiceMock.updateBasketItem(payload.lineItemUpdates[1].itemId, anything())).thrice();
verify(basketServiceMock.updateBasketItem(itemId, anything())).thrice();
expect(capture(basketServiceMock.updateBasketItem).first()).toMatchInlineSnapshot(`
[
"BIID",
{
"customerProductID": "testProductID",
"partialOrderNo": "partialTestOrderID",
"product": undefined,
"quantity": {
"unit": undefined,
Expand All @@ -332,8 +328,6 @@ describe('Basket Items Effects', () => {
[
"BIID",
{
"customerProductID": "testProductID",
"partialOrderNo": undefined,
"product": undefined,
"quantity": {
"unit": undefined,
Expand All @@ -346,8 +340,6 @@ describe('Basket Items Effects', () => {
[
"BIID",
{
"customerProductID": undefined,
"partialOrderNo": undefined,
"product": undefined,
"quantity": {
"unit": undefined,
Expand All @@ -360,6 +352,41 @@ describe('Basket Items Effects', () => {
});
});

it('should call the basketService for updateBasketItem with special attributes', done => {
const itemId = 'BIID';
const payload = {
lineItemUpdates: [
{
customerProductID: 'testProductID',
partialOrderNo: 'partialTestOrderID',
itemId,
quantity: 2,
},
],
};
const action = updateBasketItems(payload);
actions$ = of(action);

effects.updateBasketItems$.subscribe(() => {
verify(basketServiceMock.updateBasketItem(itemId, anything())).once();
expect(capture(basketServiceMock.updateBasketItem).last()).toMatchInlineSnapshot(`
[
"BIID",
{
"customerProductID": "testProductID",
"partialOrderNo": "partialTestOrderID",
"product": undefined,
"quantity": {
"unit": undefined,
"value": 2,
},
},
]
`);
done();
});
});

it('should call the basketService for deleteBasketItem if quantity = 0', done => {
when(basketServiceMock.deleteBasketItem(anyString())).thenReturn(of());

Expand Down
7 changes: 3 additions & 4 deletions src/app/core/store/customer/basket/basket-items.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from 'ish-core/models/line-item-update/line-item-update.helper';
import { BasketService } from 'ish-core/services/basket/basket.service';
import { getProductEntities, loadProduct } from 'ish-core/store/shopping/products';
import { omit } from 'ish-core/utils/functions';
import { mapErrorToAction, mapToPayload, mapToPayloadProperty } from 'ish-core/utils/operators';

import {
Expand Down Expand Up @@ -120,8 +121,7 @@ export class BasketItemsEffects {
.updateBasketItem(lineItem.itemId, {
quantity: lineItem.quantity > 0 ? { value: lineItem.quantity, unit: lineItem.unit } : undefined,
product: lineItem.sku,
customerProductID: lineItem.customerProductID,
partialOrderNo: lineItem.partialOrderNo,
...omit(lineItem, 'itemId', 'quantity', 'sku', 'unit'),
})
.pipe(
map(payload => updateBasketItemSuccess(payload)),
Expand Down Expand Up @@ -154,8 +154,7 @@ export class BasketItemsEffects {
return this.basketService.updateBasketItem(update.itemId, {
quantity: update.quantity > 0 ? { value: update.quantity, unit: update.unit } : undefined,
product: update.sku,
customerProductID: update.customerProductID,
partialOrderNo: update.partialOrderNo,
...omit(update, 'itemId', 'quantity', 'sku', 'unit'),
});
}
})
Expand Down
2 changes: 0 additions & 2 deletions src/app/core/utils/dev/basket-mock-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export class BasketMockData {
displayName: 'test',
},
],
customerProductID: 'testCustomerProductID',
partialOrderNo: 'testPartialOrderNo',
totals: {},
} as LineItemView;
}
Expand Down

0 comments on commit 3e4eb77

Please sign in to comment.