Skip to content

Commit

Permalink
fix: CXSPA-8566 pickup bug fix (#19779)
Browse files Browse the repository at this point in the history
  • Loading branch information
suprishi authored Jan 7, 2025
1 parent a8658d2 commit 789b4bc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ViewChild,
TemplateRef,
Optional,
ChangeDetectionStrategy,
} from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { FeatureConfigService, useFeatureStyles } from '@spartacus/core';
Expand All @@ -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
Expand Down

0 comments on commit 789b4bc

Please sign in to comment.