diff --git a/feature-libs/pickup-in-store/components/container/pdp-pickup-options-container/pdp-pickup-options-container.component.spec.ts b/feature-libs/pickup-in-store/components/container/pdp-pickup-options-container/pdp-pickup-options-container.component.spec.ts index 682bcba5ebc..4acea335894 100644 --- a/feature-libs/pickup-in-store/components/container/pdp-pickup-options-container/pdp-pickup-options-container.component.spec.ts +++ b/feature-libs/pickup-in-store/components/container/pdp-pickup-options-container/pdp-pickup-options-container.component.spec.ts @@ -20,7 +20,7 @@ import { LAUNCH_CALLER, LaunchDialogService, } from '@spartacus/storefront'; -import { Observable, of, Subscription } from 'rxjs'; +import { firstValueFrom, Observable, of, Subscription } from 'rxjs'; import { PdpPickupOptionsContainerComponent } from './pdp-pickup-options-container.component'; import { MockIntendedPickupLocationService } from '../../../core/facade/intended-pickup-location.service.spec'; @@ -53,7 +53,7 @@ class MockPickupLocationsSearchFacade implements PickupLocationsSearchFacade { getStockLevelAtStore = createSpy().and.returnValue( of({ stockLevel: { displayName: 'London School' } }) ); - getStoreDetails = createSpy(); + getStoreDetails = createSpy().and.returnValue(of({ name: 'London School' })); loadStoreDetails = createSpy(); } @@ -220,6 +220,34 @@ describe('PdpPickupOptionsComponent', () => { expect(component.openDialog).not.toHaveBeenCalled(); }); + it('should return undefined if intendedLocation.displayName is not defined', async () => { + spyOn( + intendedPickupLocationService, + 'getIntendedLocation' + ).and.returnValue(of({ pickupOption: 'pickup', displayName: undefined })); + spyOn(component, 'setIntendedPickupLocation'); + const displayLocation = await firstValueFrom( + component.displayPickupLocation$ + ); + expect(displayLocation).toEqual(undefined); + }); + + it('setIntendedPickupLocation should set pickupOption as delivery', async () => { + spyOn( + preferredStoreFacade, + 'getPreferredStoreWithProductInStock' + ).and.returnValue( + of({ name: 'London School', displayName: 'London School' }) + ); + component.setIntendedPickupLocation('productCode'); + expect( + intendedPickupLocationService.setIntendedLocation + ).toHaveBeenCalledWith('productCode', { + name: 'London School', + pickupOption: 'delivery', + }); + }); + it('should open dialog if displayName is not set and a11yPickupOptionsTabs disabled', () => { spyOn(featureConfigService, 'isEnabled').and.returnValue(false); spyOn(component, 'openDialog'); diff --git a/feature-libs/pickup-in-store/components/container/pdp-pickup-options-container/pdp-pickup-options-container.component.ts b/feature-libs/pickup-in-store/components/container/pdp-pickup-options-container/pdp-pickup-options-container.component.ts index 930a66c4458..b416ba30bf1 100644 --- a/feature-libs/pickup-in-store/components/container/pdp-pickup-options-container/pdp-pickup-options-container.component.ts +++ b/feature-libs/pickup-in-store/components/container/pdp-pickup-options-container/pdp-pickup-options-container.component.ts @@ -32,7 +32,7 @@ import { LAUNCH_CALLER, LaunchDialogService, } from '@spartacus/storefront'; -import { combineLatest, iif, Observable, of, Subscription } from 'rxjs'; +import { combineLatest, Observable, of, Subscription } from 'rxjs'; import { concatMap, filter, @@ -114,34 +114,15 @@ export class PdpPickupOptionsContainerComponent implements OnInit, OnDestroy { .getIntendedLocation(productCode) .pipe(map((intendedLocation) => ({ intendedLocation, productCode }))) ), - switchMap(({ intendedLocation, productCode }) => - iif( - () => !!intendedLocation && !!intendedLocation.displayName, - of(getProperty(intendedLocation, 'displayName')), - this.preferredStoreFacade - .getPreferredStoreWithProductInStock(productCode) - .pipe( - map(({ name }) => name), - tap((storeName) => - this.pickupLocationsSearchService.loadStoreDetails(storeName) - ), - concatMap((storeName: string) => - this.pickupLocationsSearchService.getStoreDetails(storeName) - ), - filter((storeDetails) => !!storeDetails), - tap((storeDetails) => { - this.intendedPickupLocationService.setIntendedLocation( - productCode, - { - ...storeDetails, - pickupOption: 'delivery', - } - ); - }) - ) - ) - ), - tap(() => (this.displayNameIsSet = true)) + switchMap(({ intendedLocation, productCode }) => { + if (intendedLocation?.displayName) { + this.displayNameIsSet = true; + return of(getProperty(intendedLocation, 'displayName')); + } + + this.setIntendedPickupLocation(productCode); + return of(undefined); + }) ); this.intendedPickupLocation$ = this.currentProductService.getProduct().pipe( @@ -177,6 +158,29 @@ export class PdpPickupOptionsContainerComponent implements OnInit, OnDestroy { this.subscription.unsubscribe(); } + setIntendedPickupLocation(productCode: string) { + this.subscription.add( + this.preferredStoreFacade + .getPreferredStoreWithProductInStock(productCode) + .pipe( + map(({ name }) => name), + tap((storeName) => + this.pickupLocationsSearchService.loadStoreDetails(storeName) + ), + concatMap((storeName: string) => + this.pickupLocationsSearchService.getStoreDetails(storeName) + ), + filter((storeDetails) => !!storeDetails) + ) + .subscribe((storeDetails) => { + this.intendedPickupLocationService.setIntendedLocation(productCode, { + ...storeDetails, + pickupOption: 'delivery', + }); + }) + ); + } + // TODO: Make argument required once 'a11yDialogTriggerRefocus' feature flag is removed. /** * @deprecated since 2211.28.0 - The use of TriggerElement param will become mandatory. diff --git a/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.component.ts b/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.component.ts index 4a9a126017c..9ca1ec5fb60 100644 --- a/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.component.ts +++ b/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.component.ts @@ -18,6 +18,7 @@ import { ViewChild, TemplateRef, Optional, + ChangeDetectionStrategy, } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; import { FeatureConfigService, useFeatureStyles } from '@spartacus/core'; @@ -32,6 +33,7 @@ import { PickupOptionsTabs } from './pickup-options.model'; @Component({ selector: 'cx-pickup-options', templateUrl: './pickup-options.component.html', + changeDetection: ChangeDetectionStrategy.OnPush, }) export class PickupOptionsComponent implements OnChanges, AfterViewInit, OnDestroy