Skip to content

Commit

Permalink
fix(checkout): CHECKOUT-4536 Fix error when adding a address with mul…
Browse files Browse the repository at this point in the history
…tishipping
  • Loading branch information
ankurbigcomm committed Nov 13, 2019
1 parent cc7e093 commit de610b9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/app/shipping/Shipping.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ describe('Shipping Component', () => {
jest.spyOn(checkoutService, 'loadShippingOptions')
.mockResolvedValue({} as CheckoutSelectors);

jest.spyOn(checkoutService, 'deleteConsignment')
.mockResolvedValue({} as CheckoutSelectors);

jest.spyOn(checkoutState.data, 'getCart')
.mockReturnValue({
...getCart(),
Expand Down Expand Up @@ -236,6 +239,45 @@ describe('Shipping Component', () => {
expect(defaultProps.navigateNextStep).toHaveBeenCalledWith(true);
});

it('calls delete consignment if consignments exist when adding a new address', async () => {
jest.spyOn(checkoutState.data, 'getCustomer').mockReturnValue({
...getCustomer(),
});

jest.spyOn(checkoutState.data, 'getConsignments')
.mockReturnValue([getConsignment()]);

component = mount(<ComponentTest { ...defaultProps } />);
await new Promise(resolve => process.nextTick(resolve));

component.update();
component.find('#addressToggle').simulate('click');
component.find('#addressDropdown li').first().find('a').simulate('click');

await new Promise(resolve => process.nextTick(resolve));
component.update();

expect(checkoutService.deleteConsignment).toHaveBeenCalled();
});

it('does not Call delete consignment if consignment doesnot exist when adding a new address', async () => {
jest.spyOn(checkoutState.data, 'getCustomer').mockReturnValue({
...getCustomer(),
});

jest.spyOn(checkoutState.data, 'getConsignments')
.mockReturnValue([]);

component = mount(<ComponentTest { ...defaultProps } />);
await new Promise(resolve => process.nextTick(resolve));

component.update();
component.find('#addressToggle').simulate('click');
component.find('#addressDropdown li').first().find('a').simulate('click');

expect(checkoutService.deleteConsignment).not.toHaveBeenCalled();
});

describe('when multishipping mode is on', () => {
describe('when shopper is signed', () => {
beforeEach(async () => {
Expand Down
6 changes: 5 additions & 1 deletion src/app/shipping/Shipping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ const deleteConsignmentsSelector = createSelector(
({ checkoutService: { deleteConsignment } }: CheckoutContextProps) => deleteConsignment,
({ checkoutState: { data } }: CheckoutContextProps) => data.getConsignments(),
(deleteConsignment, consignments) => async () => {
const [{ data }] = await Promise.all((consignments || []).map(({ id }) =>
if (!consignments || !consignments.length) {
return;
}

const [{ data }] = await Promise.all(consignments.map(({ id }) =>
deleteConsignment(id)
));

Expand Down

0 comments on commit de610b9

Please sign in to comment.