Skip to content

Commit

Permalink
chore: fix errors in cart lib unit tests (#18042)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmch91 authored Nov 7, 2023
1 parent 1b259fc commit c266735
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import {
CmsComponentData,
CurrentProductService,
OutletModule,
ProductListItemContext,
SpinnerModule,
} from '@spartacus/storefront';
Expand Down Expand Up @@ -132,6 +133,7 @@ describe('AddToCartComponent', () => {
SpinnerModule,
I18nTestingModule,
ReactiveFormsModule,
OutletModule,
],
declarations: [AddToCartComponent, MockItemCounterComponent],
providers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from '@spartacus/core';
import {
ICON_TYPE,
KeyboardFocusTestingModule,
LaunchDialogService,
PromotionsModule,
SpinnerModule,
Expand Down Expand Up @@ -144,6 +145,7 @@ describe('AddedToCartDialogComponent', () => {
I18nTestingModule,
PromotionsModule,
FeaturesConfigModule,
KeyboardFocusTestingModule,
],
declarations: [
AddedToCartDialogComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class MockCartCouponComponent {
cartIsLoading = false;
}

@Component({
selector: 'cx-cart-validation-warnings',
template: '',
})
class MockCartValidationWarningsComponent {}

describe('CartDetailsComponent', () => {
let component: CartDetailsComponent;
let fixture: ComponentFixture<CartDetailsComponent>;
Expand Down Expand Up @@ -96,6 +102,7 @@ describe('CartDetailsComponent', () => {
CartDetailsComponent,
MockCartItemListComponent,
MockCartCouponComponent,
MockCartValidationWarningsComponent,
],
providers: [
{ provide: SelectiveCartFacade, useValue: mockSelectiveCartFacade },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import {
ControlContainer,
UntypedFormControl,
ReactiveFormsModule,
UntypedFormControl,
} from '@angular/forms';
import { By } from '@angular/platform-browser';
import { RouterTestingModule } from '@angular/router/testing';
Expand Down Expand Up @@ -87,6 +87,21 @@ const mockProduct = {
},
};

@Component({
selector: 'cx-cart-item-validation-warning',
template: '',
})
class MockCartItemValidationWarningComponent {
@Input() code: string;
}

@Directive({
selector: '[cxAtMessage]',
})
class MockAtMessageDirective {
@Input() cxAtMessage: string | string[] | undefined;
}

describe('CartItemComponent', () => {
let cartItemComponent: CartItemComponent;
let componentInjector: Injector;
Expand Down Expand Up @@ -116,6 +131,8 @@ describe('CartItemComponent', () => {
MockUrlPipe,
MockFeatureLevelDirective,
MockOutletDirective,
MockCartItemValidationWarningComponent,
MockAtMessageDirective,
],
providers: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Component, Input } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { ActiveCartFacade, Cart } from '@spartacus/cart/base/root';
import { Observable, of } from 'rxjs';
Expand All @@ -13,14 +14,22 @@ class MockActiveCartService {
}
}

@Component({
selector: 'cx-order-summary',
template: '',
})
class MockOrderSummaryComponent {
@Input() cart: Cart;
}

describe('CartTotalsComponent', () => {
let component: CartTotalsComponent;
let fixture: ComponentFixture<CartTotalsComponent>;

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [CartTotalsComponent],
declarations: [CartTotalsComponent, MockOrderSummaryComponent],
providers: [
{
provide: ActiveCartFacade,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Cart,
CartItemComponentOptions,
OrderEntry,
PromotionLocation,
SelectiveCartFacade,
} from '@spartacus/cart/base/root';
import {
Expand All @@ -23,6 +24,7 @@ class MockCartItemListComponent {
@Input() readonly = false;
@Input() items: OrderEntry[];
@Input() cartIsLoading: Observable<boolean>;
@Input() promotionLocation: PromotionLocation;
@Input() options: CartItemComponentOptions = {
isSaveForLater: false,
optionalBtn: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ const MockCmsComponentData = <CmsComponentData<any>>{
})
class MockQuickOrderFormComponent {
@Input() isLoading: boolean;
@Input() limit: number;
}

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class MockItemCounterComponent {
})
class MockMediaComponent {
@Input() container;
@Input() alt: string;
}

describe('QuickOrderItemComponent', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractType } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import {
ActiveCartFacade,
CartAddEntrySuccessEvent,
Expand All @@ -14,7 +14,7 @@ import {
SearchConfig,
} from '@spartacus/core';
import { Observable, of, queueScheduler } from 'rxjs';
import { delay, observeOn, switchMap, take, tap } from 'rxjs/operators';
import { observeOn, take, tap } from 'rxjs/operators';
import { QuickOrderService } from './quick-order.service';

const mockProduct1Code: string = 'mockCode1';
Expand Down Expand Up @@ -325,24 +325,32 @@ describe('QuickOrderService', () => {
});
});

it('should add deleted entry and after 7s delete it', (done) => {
it('should add deleted entry and after 7s delete it', fakeAsync(() => {
service.loadEntries(mockEntries);
service.softDeleteEntry(0);

let softDeletedEntries: any;
let shouldCheck = true;

service
.getSoftDeletedEntries()
.pipe(
tap((softDeletedEntries) => {
expect(softDeletedEntries).toEqual({ mockCode1: mockEntry1 });
}),
delay(7000),
switchMap(() => service.getSoftDeletedEntries())
tap((entries) => {
if (shouldCheck) {
expect(entries).toEqual({ mockCode1: mockEntry1 });

shouldCheck = false;
}
})
)
.subscribe((result) => {
expect(result).toEqual({});
softDeletedEntries = result;
});
done();
});

tick(7000);

expect(softDeletedEntries).toEqual({});
}));

it('should not add deleted entry', (done) => {
service.loadEntries([mockEmptyEntry]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
RoutingService,
Translatable,
} from '@spartacus/core';
import { OutletModule } from '@spartacus/storefront';
import { BehaviorSubject, EMPTY, Observable, of } from 'rxjs';
import { SavedCartDetailsService } from '../saved-cart-details.service';
import { SavedCartDetailsItemsComponent } from './saved-cart-details-items.component';
Expand Down Expand Up @@ -81,7 +82,7 @@ describe('SavedCartDetailsItemsComponent', () => {
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [StoreModule.forRoot({}), I18nTestingModule],
imports: [StoreModule.forRoot({}), I18nTestingModule, OutletModule],
declarations: [SavedCartDetailsItemsComponent],
providers: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ChangeDetectionStrategy,
Component,
DebugElement,
Directive,
Input,
Pipe,
PipeTransform,
Expand Down Expand Up @@ -91,6 +92,13 @@ class MockUrlPipe implements PipeTransform {
transform(): any {}
}

@Directive({
selector: '[cxAtMessage]',
})
class MockAtMessageDirective {
@Input() cxAtMessage: string | string[] | undefined;
}

describe('AddToWishListComponent', () => {
let component: AddToWishListComponent;
let fixture: ComponentFixture<AddToWishListComponent>;
Expand All @@ -101,7 +109,12 @@ describe('AddToWishListComponent', () => {
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [I18nTestingModule, RouterTestingModule],
declarations: [AddToWishListComponent, MockIconComponent, MockUrlPipe],
declarations: [
AddToWishListComponent,
MockIconComponent,
MockUrlPipe,
MockAtMessageDirective,
],
providers: [
{ provide: AuthService, useClass: MockAuthService },
{ provide: WishListFacade, useClass: MockWishListService },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ChangeDetectionStrategy,
Component,
DebugElement,
Directive,
Injector,
Input,
Pipe,
Expand Down Expand Up @@ -71,6 +72,13 @@ const mockCartEntry: OrderEntry = {
},
};

@Directive({
selector: '[cxAtMessage]',
})
class MockAtMessageDirective {
@Input() cxAtMessage: string | string[] | undefined;
}

describe('WishListItemComponent', () => {
let component: WishListItemComponent;
let fixture: ComponentFixture<WishListItemComponent>;
Expand All @@ -86,6 +94,7 @@ describe('WishListItemComponent', () => {
MockPictureComponent,
MockAddToCartComponent,
MockUrlPipe,
MockAtMessageDirective,
],
})
.overrideComponent(WishListItemComponent, {
Expand Down

0 comments on commit c266735

Please sign in to comment.