Skip to content

Commit

Permalink
feat: pickup in store for ApplePay (#18643)
Browse files Browse the repository at this point in the history
CXSPA-5536
  • Loading branch information
FollowTheFlo authored Mar 21, 2024
1 parent 6edc7f7 commit 312ad0b
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
import { TestBed } from '@angular/core/testing';
import { DeliveryMode } from '@spartacus/cart/base/root';
import { Product, WindowRef } from '@spartacus/core';
import { OpfCartHandlerService } from '@spartacus/opf/base/core';
import {
OpfCartHandlerService,
OpfPickupInStoreHandlerService,
} from '@spartacus/opf/base/core';
import {
ApplePayObservableConfig,
ApplePayShippingType,
ApplePayTransactionInput,
OpfPaymentFacade,
OpfQuickBuyDeliveryInfo,
OpfQuickBuyDeliveryType,
} from '@spartacus/opf/base/root';
import { Subject, of, throwError } from 'rxjs';
import { Observable, Subject, of, throwError } from 'rxjs';
import { map } from 'rxjs/operators';
import { OpfQuickBuyService } from '../opf-quick-buy.service';
import { ApplePaySessionFactory } from './apple-pay-session/apple-pay-session.factory';
import { ApplePayService } from './apple-pay.service';
import { ApplePayObservableFactory } from './observable/apple-pay-observable.factory';
Expand Down Expand Up @@ -110,7 +118,15 @@ describe('ApplePayService', () => {
let applePayObservableFactoryMock: jasmine.SpyObj<ApplePayObservableFactory>;
let applePaySessionFactoryMock: jasmine.SpyObj<ApplePaySessionFactory>;
let applePayObservableTestController: Subject<ApplePayJS.ApplePayPaymentAuthorizationResult>;
let opfQuickBuyServiceMock: jasmine.SpyObj<OpfQuickBuyService>;
let opfPickupInStoreHandlerServiceMock: jasmine.SpyObj<OpfPickupInStoreHandlerService>;
beforeEach(() => {
opfQuickBuyServiceMock = jasmine.createSpyObj('OpfQuickBuyService', [
'getQuickBuyLocationContext',
'getQuickBuyProviderConfig',
'getQuickBuyDeliveryInfo',
]);

applePaySessionFactoryMock = jasmine.createSpyObj(
'ApplePaySessionFactory',
['startApplePaySession']
Expand Down Expand Up @@ -142,6 +158,10 @@ describe('ApplePayService', () => {
'removeProductFromOriginalCart',
'loadCartAfterSingleProductTransaction',
]);
opfPickupInStoreHandlerServiceMock = jasmine.createSpyObj(
'OpfPickupInStoreHandlerService',
['getSingleProductDeliveryInfo']
);
TestBed.configureTestingModule({
providers: [
ApplePayService,
Expand All @@ -156,6 +176,11 @@ describe('ApplePayService', () => {
provide: ApplePaySessionFactory,
useValue: applePaySessionFactoryMock,
},
{ provide: OpfQuickBuyService, useValue: opfQuickBuyServiceMock },
{
provide: OpfPickupInStoreHandlerService,
useValue: opfPickupInStoreHandlerServiceMock,
},
],
});
service = TestBed.inject(ApplePayService);
Expand All @@ -179,6 +204,11 @@ describe('ApplePayService', () => {
config = actualConfig;
return applePayObservableTestController;
});
opfQuickBuyServiceMock.getQuickBuyDeliveryInfo.and.returnValue(
of({
type: OpfQuickBuyDeliveryType.SHIPPING,
})
);
});

it('should handle validateMerchant change', () => {
Expand Down Expand Up @@ -567,11 +597,79 @@ describe('ApplePayService', () => {
.start({ product: mockProduct, quantity: 1, countryCode: 'us' })
.subscribe({ error: () => {} });
});

it('should handle setApplePayRequestConfig with Pickup delivery type on Cart page', (done) => {
const transactionInputMock: ApplePayTransactionInput = {
product: undefined,
cart: mockCart,
quantity: 0,
countryCode: 'us',
};

const deliveryInfoMock: OpfQuickBuyDeliveryInfo = {
type: OpfQuickBuyDeliveryType.PICKUP,
pickupDetails: { name: 'Nakano' },
};

opfQuickBuyServiceMock.getQuickBuyDeliveryInfo.and.returnValue(
of(deliveryInfoMock)
);

const setApplePayRequestConfig = service['setApplePayRequestConfig'](
transactionInputMock
) as Observable<ApplePayJS.ApplePayPaymentRequest>;
setApplePayRequestConfig.subscribe((config) => {
expect(config.requiredShippingContactFields).toEqual([]);
expect(config.shippingType).toEqual(ApplePayShippingType.STORE_PICKUP);
expect(
opfPickupInStoreHandlerServiceMock.getSingleProductDeliveryInfo
).not.toHaveBeenCalled();
done();
});
});

it('should handle setApplePayRequestConfig with Pickup delivery type on Product page', (done) => {
const transactionInputMock: ApplePayTransactionInput = {
product: mockProduct,
cart: undefined,
quantity: 1,
countryCode: 'us',
};

const deliveryInfoMock: OpfQuickBuyDeliveryInfo = {
type: OpfQuickBuyDeliveryType.PICKUP,
pickupDetails: { name: 'Nakano' },
};

opfQuickBuyServiceMock.getQuickBuyDeliveryInfo.and.returnValue(
of(deliveryInfoMock)
);

opfPickupInStoreHandlerServiceMock.getSingleProductDeliveryInfo.and.returnValue(
of(deliveryInfoMock)
);

const setApplePayRequestConfig = service['setApplePayRequestConfig'](
transactionInputMock
) as Observable<ApplePayJS.ApplePayPaymentRequest>;
setApplePayRequestConfig.subscribe((config) => {
expect(config.requiredShippingContactFields).toEqual([]);
expect(config.shippingType).toEqual(ApplePayShippingType.STORE_PICKUP);
expect(
opfPickupInStoreHandlerServiceMock.getSingleProductDeliveryInfo
).toHaveBeenCalled();
done();
});
});
});

it('should handle errors during Apple Pay session start', () => {
service = TestBed.inject(ApplePayService);

opfQuickBuyServiceMock.getQuickBuyDeliveryInfo.and.returnValue(
of({
type: OpfQuickBuyDeliveryType.SHIPPING,
})
);
cartHandlerServiceMock.loadOriginalCart.and.returnValue(of(true));
cartHandlerServiceMock.removeProductFromOriginalCart.and.returnValue(
of(true)
Expand Down
Loading

0 comments on commit 312ad0b

Please sign in to comment.