Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cart)!: change cart totals to dict #2608

Merged
merged 6 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '@ngrx/store';
import { cold } from 'jasmine-marbles';

import { DaffCart } from '@daffodil/cart';
import {
DaffAddToCart,
DaffAddToCartSuccess,
Expand Down Expand Up @@ -60,11 +61,14 @@ describe('AddToCartNotificationComponent', () => {
cart: DaffCartReducersState;
[DAFF_PRODUCT_STORE_FEATURE_KEY]: DaffProductReducersState<DaffProduct>;
}>;
const productFactory: DaffProductFactory = new DaffProductFactory();
const cartFactory: DaffCartFactory = new DaffCartFactory();
let productFactory: DaffProductFactory;
let cartFactory: DaffCartFactory;

let addToCartNotification: AddToCartNotificationComponent;
let productAdded: MockProductAddedComponent;
let stubProduct: DaffProduct;
let productAddPayload;
let stubCart: DaffCart;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -91,6 +95,13 @@ describe('AddToCartNotificationComponent', () => {
}));

beforeEach(() => {
cartFactory = TestBed.inject(DaffCartFactory);
productFactory = TestBed.inject(DaffProductFactory);

stubProduct = productFactory.create();
productAddPayload = { productId: stubProduct.id, qty: 1 };
stubCart = cartFactory.create();

fixture = TestBed.createComponent(WrapperComponent);
wrapper = fixture.componentInstance;
store = TestBed.inject(Store);
Expand All @@ -106,9 +117,7 @@ describe('AddToCartNotificationComponent', () => {
});

describe('on demo-product-added', () => {
const stubProduct = productFactory.create();
const productAddPayload = { productId: stubProduct.id, qty: 1 };
const stubCart = cartFactory.create();


beforeEach(() => {
store.dispatch(new DaffProductLoadSuccess({
Expand All @@ -133,7 +142,6 @@ describe('AddToCartNotificationComponent', () => {
});

describe('ngOnInit', () => {

beforeEach(() => {
addToCartNotification.ngOnInit();
});
Expand Down Expand Up @@ -172,10 +180,6 @@ describe('AddToCartNotificationComponent', () => {
});

describe('when loading$ is false', () => {
const stubProduct = productFactory.create();
const productAddPayload = { productId: stubProduct.id, qty: 1 };
const stubCart = cartFactory.create();

beforeEach(() => {
store.dispatch(new OpenAddToCartNotification());
store.dispatch(new DaffAddToCart(productAddPayload));
Expand Down Expand Up @@ -214,9 +218,6 @@ describe('AddToCartNotificationComponent', () => {
});

describe('when loading$ is true', () => {
const stubProduct = productFactory.create();
const productAddPayload = { productId: stubProduct.id, qty: 1 };

beforeEach(() => {
store.dispatch(new OpenAddToCartNotification());
store.dispatch(new DaffAddToCart(productAddPayload));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TestBed } from '@angular/core/testing';

import { DaffCart } from '@daffodil/cart';
import {
DaffAddToCart,
Expand All @@ -16,9 +18,17 @@ import {
getProductQty,
getLoading,
getProductId,
State,
} from '../reducers/add-to-cart-notification.reducer';

describe('Add To Cart Notification | Reducer', () => {
let cartFactory: DaffCartFactory;
let stubCart: DaffCart;

beforeEach(() => {
cartFactory = TestBed.inject(DaffCartFactory);
stubCart = cartFactory.create();
});

describe('initialState', () => {

Expand All @@ -40,7 +50,7 @@ describe('Add To Cart Notification | Reducer', () => {

describe('when OpenAddToCartNotification action is triggered', () => {

let result;
let result: State;

beforeEach(() => {
const openAddToCartNotificationAction = new OpenAddToCartNotification();
Expand All @@ -55,7 +65,7 @@ describe('Add To Cart Notification | Reducer', () => {

describe('when CloseAddToCartNotification action is triggered', () => {

let result;
let result: State;

beforeEach(() => {
const closeAddToCartNotificationAction = new CloseAddToCartNotification();
Expand All @@ -70,7 +80,7 @@ describe('Add To Cart Notification | Reducer', () => {

describe('when AddToCart action is triggered', () => {

let result;
let result: State;
let stubQty;
let stubId;

Expand All @@ -96,10 +106,7 @@ describe('Add To Cart Notification | Reducer', () => {
});

describe('when AddToCartSuccess action is triggered', () => {

let result;
const cartFactory = new DaffCartFactory();
const stubCart: DaffCart = cartFactory.create();
let result: State;

beforeEach(() => {
const addToCartSuccessAction = new DaffAddToCartSuccess(stubCart);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ describe('CartItemsComponent', () => {
let cartItems;
let cart: CartItemsComponent;

const cartFactory = new DaffCartFactory();
const mockCart = cartFactory.create({
items: new DaffCartItemFactory().createMany(2),
});
let cartFactory: DaffCartFactory;
let mockCart: DaffCart;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -53,6 +51,12 @@ describe('CartItemsComponent', () => {
}));

beforeEach(() => {
cartFactory = TestBed.inject(DaffCartFactory);

mockCart = cartFactory.create({
items: new DaffCartItemFactory().createMany(2),
});

fixture = TestBed.createComponent(WrapperComponent);
wrapper = fixture.componentInstance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ describe('CartSidebar', () => {

let summaryElement: DebugElement;

const cartFactory = new DaffCartFactory();
const cart = cartFactory.create();
let cartFactory: DaffCartFactory;
let cart: DaffCart;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -68,6 +68,10 @@ describe('CartSidebar', () => {
}));

beforeEach(() => {
cartFactory = TestBed.inject(DaffCartFactory);

cart = cartFactory.create();

fixture = TestBed.createComponent(WrapperComponent);
wrapper = fixture.componentInstance;
wrapper.cartValue = cart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { DaffLoadingIconModule } from '@daffodil/design';
import { CartSummaryWrapperComponent } from './cart-summary-wrapper.component';
import { CartSummaryComponent } from '../cart-summary/cart-summary.component';

const cartFactory = new DaffCartFactory();
const cart = cartFactory.create();
let cartFactory: DaffCartFactory;
let cart: DaffCart;
const stubCartTitle = 'cartTitle';

@Component({ template: '<demo-cart-summary-wrapper [cartTitle]="cartTitleValue" [cart]="cartValue$ | async" [loading]="loadingValue$ | async"><div class="transcluded-content"></div></demo-cart-summary-wrapper>' })
Expand Down Expand Up @@ -83,6 +83,10 @@ describe('CartSummaryWrapper', () => {
}));

beforeEach(() => {
cartFactory = TestBed.inject(DaffCartFactory);

cart = cartFactory.create();

fixture = TestBed.createComponent(WrapperComponent);
router = TestBed.inject(Router);
spyOn(router, 'navigateByUrl');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ describe('CartTotalsComponent', () => {
let cartTotalsComponent: CartTotalsComponent;
let cartTotalsItemComponent: any;
let currencyPipe;
const cartFactory = new DaffCartFactory();
const cartItemFactory = new DaffCartItemFactory();
let cartFactory: DaffCartFactory;
let cartItemFactory: DaffCartItemFactory;

const mockCart = cartFactory.create({
items: cartItemFactory.createMany(2),
});
let mockCart: DaffCart;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -52,6 +50,12 @@ describe('CartTotalsComponent', () => {
}));

beforeEach(() => {
cartFactory = TestBed.inject(DaffCartFactory);
cartItemFactory = TestBed.inject(DaffCartItemFactory);

mockCart = cartFactory.create({
items: cartItemFactory.createMany(2),
});
fixture = TestBed.createComponent(WrapperComponent);
wrapper = fixture.componentInstance;
currencyPipe = spyOn(DaffMockCurrencyPipe.prototype, 'transform');
Expand Down
10 changes: 7 additions & 3 deletions apps/demo/src/app/cart/components/cart/cart.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ describe('Cart', () => {
let cartItemCountElement: DebugElement;
let cartItemCountComponent: MockCartItemCountComponent;

const cartFactory = new DaffCartFactory();
const cart = cartFactory.create();
let cartFactory: DaffCartFactory;
let cart: DaffCart;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -89,9 +89,13 @@ describe('Cart', () => {
}));

beforeEach(() => {
daffCartFacade = TestBed.inject(MockDaffCartFacade);
cartFactory = TestBed.inject(DaffCartFactory);

cart = cartFactory.create();

fixture = TestBed.createComponent(WrapperComponent);
wrapper = fixture.componentInstance;
daffCartFacade = TestBed.inject(MockDaffCartFacade);
daffCartFacade.items$ = new BehaviorSubject(cart.items);
daffCartFacade.isCartEmpty$ = new BehaviorSubject(true);
wrapper.cartValue = cart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ import {

import { DemoCartViewComponent } from './cart-view.component';

const cartFactory = new DaffCartFactory();
const cart = cartFactory.create();

@Component({
selector: 'demo-cart',
template: '',
Expand All @@ -38,6 +35,8 @@ describe('DemoCartViewComponent', () => {
let component: DemoCartViewComponent;
let fixture: ComponentFixture<DemoCartViewComponent>;
let cartFacade: MockDaffCartFacade;
let cartFactory: DaffCartFactory;
let cart: DaffCart;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -55,6 +54,10 @@ describe('DemoCartViewComponent', () => {
}));

beforeEach(() => {
cartFactory = TestBed.inject(DaffCartFactory);

cart = cartFactory.create();

fixture = TestBed.createComponent(DemoCartViewComponent);
component = fixture.componentInstance;
cartFacade = TestBed.inject(MockDaffCartFacade);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import {
DaffCartServiceInterface,
DaffCartDriver,
} from '@daffodil/cart/driver';
import { DaffTestingCartService } from '@daffodil/cart/driver/testing';
import { daffCartReducers } from '@daffodil/cart/state';
import { DaffTestingCartDriverModule } from '@daffodil/cart/driver/testing';
import {
DaffCartStateTestingModule,
MockDaffCartFacade,
} from '@daffodil/cart/state/testing';
import { DaffCartFactory } from '@daffodil/cart/testing';

import { CartResolverEffects } from './cart-resolver.effects';
Expand All @@ -35,44 +38,39 @@ describe('CartResolverEffects', () => {
let cartFactory: DaffCartFactory;
let stubCart: DaffCart;
let driver: DaffCartServiceInterface<DaffCart>;
let cartFacade: MockDaffCartFacade;

beforeEach(() => {

TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({
carts: combineReducers(daffCartReducers),
}),
DaffTestingCartDriverModule.forRoot(),
DaffCartStateTestingModule,
],
providers: [
CartResolverEffects,
provideMockActions(() => actions$),
{
provide: DaffCartDriver,
useValue: new DaffTestingCartService(new DaffCartFactory()),
},
],
});

cartFacade = TestBed.inject(MockDaffCartFacade);
effects = TestBed.inject(CartResolverEffects);
cartFactory = TestBed.inject(DaffCartFactory);
stubCart = cartFactory.create();
driver = TestBed.inject(DaffCartDriver);

stubCart = cartFactory.create();
});

it('should be created', () => {
expect(effects).toBeTruthy();
});

describe('onResolveCart$', () => {

let expected;
const resolveCartAction = new ResolveCart();

describe('when cart in redux state is defined', () => {

beforeEach(() => {
spyOn(effects, 'selectStoreCart').and.returnValue(of(stubCart));
cartFacade.cart$.next(stubCart);
});

it('should dispatch a ResolveCartSuccess action', () => {
Expand All @@ -85,13 +83,11 @@ describe('CartResolverEffects', () => {
});

describe('when cart in redux state is null', () => {

beforeEach(() => {
spyOn(effects, 'selectStoreCart').and.returnValue(of(null));
cartFacade.cart$.next(null);
});

describe('and service call to cartService.get is successful', () => {

beforeEach(() => {
spyOn(driver, 'get').and.returnValue(of({
response: stubCart,
Expand All @@ -109,7 +105,6 @@ describe('CartResolverEffects', () => {
});

describe('and service call to cartService.get fails', () => {

beforeEach(() => {
const response = cold('#', {});
spyOn(driver, 'get').and.returnValue(response);
Expand Down
Loading