Skip to content

Commit

Permalink
feat: define cartQuickOrderRemoveListeningToFailEvent feature toggle (#…
Browse files Browse the repository at this point in the history
…18780)

Define cartQuickOrderRemoveListeningToFailEvent feature toggle to avoid showing an unnecessary duplicated error message on the failure of adding to cart
  • Loading branch information
Larisa-Staroverova committed May 7, 2024
1 parent 2be626f commit 4a9fe91
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@spartacus/cart/base/root';
import {
EventService,
FeatureConfigService,
GlobalMessageService,
GlobalMessageType,
I18nTestingModule,
Expand Down Expand Up @@ -100,6 +101,7 @@ describe('CartQuickOrderFormComponent', () => {
let activeCartService: ActiveCartFacade;
let eventService: EventService;
let globalMessageService: GlobalMessageService;
let featureConfigService: FeatureConfigService;

beforeEach(async () => {
await TestBed.configureTestingModule({
Expand All @@ -126,6 +128,7 @@ describe('CartQuickOrderFormComponent', () => {
activeCartService = TestBed.inject(ActiveCartFacade);
eventService = TestBed.inject(EventService);
globalMessageService = TestBed.inject(GlobalMessageService);
featureConfigService = TestBed.inject(FeatureConfigService);

fixture.detectChanges();
});
Expand Down Expand Up @@ -207,20 +210,33 @@ describe('CartQuickOrderFormComponent', () => {
});
});

it('should show global error message on add entry fail event', () => {
spyOn(globalMessageService, 'add').and.callThrough();
spyOn(eventService, 'get').and.callThrough();
describe('global error message', () => {
it('should not show global error message on add entry fail event in case cartQuickOrderRemoveListeningToFailEvent is enabled', () => {
spyOn(globalMessageService, 'add').and.callThrough();
component.ngOnInit();
component.quickOrderForm.controls['productCode'].setValue('test');
spyOn(featureConfigService, 'isEnabled').and.returnValue(true);

component.ngOnInit();
component.quickOrderForm.controls['productCode'].setValue('test');
component.applyQuickOrder();
addEntryCartEvent$.next(mockCartAddEntryFailEvent);

expect(globalMessageService.add).toHaveBeenCalledWith(
{
key: 'quickOrderCartForm.noResults',
},
GlobalMessageType.MSG_TYPE_ERROR
);
component.applyQuickOrder();
expect(globalMessageService.add).not.toHaveBeenCalled();
});

it('should show global error message on add entry fail event in case cartQuickOrderRemoveListeningToFailEvent is disabled', () => {
spyOn(globalMessageService, 'add').and.callThrough();
spyOn(eventService, 'get').and.callThrough();
component.ngOnInit();
component.quickOrderForm.controls['productCode'].setValue('test');
spyOn(featureConfigService, 'isEnabled').and.returnValue(false);

component.applyQuickOrder();
addEntryCartEvent$.next(mockCartAddEntryFailEvent);

expect(globalMessageService.add).toHaveBeenCalledWith(
{
key: 'quickOrderCartForm.noResults',
},
GlobalMessageType.MSG_TYPE_ERROR
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import {
ChangeDetectionStrategy,
Component,
inject,
OnDestroy,
OnInit,
} from '@angular/core';
Expand All @@ -23,6 +24,7 @@ import {
} from '@spartacus/cart/base/root';
import {
EventService,
FeatureConfigService,
GlobalMessageService,
GlobalMessageType,
} from '@spartacus/core';
Expand All @@ -35,6 +37,8 @@ import { first, map } from 'rxjs/operators';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CartQuickOrderFormComponent implements OnInit, OnDestroy {
private featureConfig = inject(FeatureConfigService);

quickOrderForm: UntypedFormGroup;
cartIsLoading$: Observable<boolean> = this.activeCartService
.isStable()
Expand Down Expand Up @@ -73,7 +77,11 @@ export class CartQuickOrderFormComponent implements OnInit, OnDestroy {
const quantity = this.quickOrderForm.get('quantity')?.value;

this.watchAddEntrySuccessEvent();
this.watchAddEntryFailEvent();
if (
!this.featureConfig.isEnabled('cartQuickOrderRemoveListeningToFailEvent')
) {
this.watchAddEntryFailEvent();
}

if (productCode && quantity) {
this.activeCartService.addEntry(productCode, quantity);
Expand Down Expand Up @@ -141,6 +149,16 @@ export class CartQuickOrderFormComponent implements OnInit, OnDestroy {
);
}

/**
* @deprecated since 2211.24
*
* This method is no longer needed since BadRequestHandler.handleUnknownIdentifierError was introduced.
* If this method is used an unnecessary duplicated error message will appear in the UI.
* Therefore this method will be removed.
*
* You can enable the Feature Toggle 'cartQuickOrderRemoveListenToFailEvent'
* to stop calling this method by default.
*/
protected watchAddEntryFailEvent(): void {
this.cartEventsSubscription.add(
this.eventService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ export interface FeatureTogglesInterface {
* Determines whether the controls in the `CarouselComponent` are focusable and accessible from the keyboard.
*/
a11yFocusableCarouselControls?: boolean;

/**
* In `CartQuickOrderFormComponent` it stops calling the deprecated method
* `watchAddEntryFailEvent()`, which listens to the `CartAddEntryFailEvent`.
*
* It avoids showing an unnecessary duplicated error message on the failure
* of adding to the cart.
*/
cartQuickOrderRemoveListeningToFailEvent?: boolean;
}

export const defaultFeatureToggles: Required<FeatureTogglesInterface> = {
Expand Down Expand Up @@ -203,4 +212,5 @@ export const defaultFeatureToggles: Required<FeatureTogglesInterface> = {
a11yUnitsListKeyboardControls: false,
a11yCartItemsLinksStyles: false,
a11yFocusableCarouselControls: false,
cartQuickOrderRemoveListeningToFailEvent: false,
};
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ if (environment.requestedDeliveryDate) {
a11yUnitsListKeyboardControls: true,
a11yCartItemsLinksStyles: true,
a11yFocusableCarouselControls: true,
cartQuickOrderRemoveListeningToFailEvent: true,
};
return appFeatureToggles;
}),
Expand Down

0 comments on commit 4a9fe91

Please sign in to comment.