Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Nov 21, 2023
1 parent 78cb734 commit 8bdb152
Show file tree
Hide file tree
Showing 33 changed files with 126 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('AddToCartNotificationComponent', () => {
[DAFF_PRODUCT_STORE_FEATURE_KEY]: DaffProductReducersState<DaffProduct>;
}>;
const productFactory: DaffProductFactory = new DaffProductFactory();
const cartFactory: DaffCartFactory = new DaffCartFactory();
const cartFactory: DaffCartFactory = TestBed.inject(DaffCartFactory);

let addToCartNotification: AddToCartNotificationComponent;
let productAdded: MockProductAddedComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('Add To Cart Notification | Reducer', () => {
describe('when AddToCartSuccess action is triggered', () => {

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

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('CartItemsComponent', () => {
let cartItems;
let cart: CartItemsComponent;

const cartFactory = new DaffCartFactory();
const cartFactory = TestBed.inject(DaffCartFactory);
const mockCart = cartFactory.create({
items: new DaffCartItemFactory().createMany(2),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('CartSidebar', () => {

let summaryElement: DebugElement;

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

beforeEach(waitForAsync(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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 cartFactory = TestBed.inject(DaffCartFactory);
const cart = cartFactory.create();
const stubCartTitle = 'cartTitle';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('CartTotalsComponent', () => {
let cartTotalsComponent: CartTotalsComponent;
let cartTotalsItemComponent: any;
let currencyPipe;
const cartFactory = new DaffCartFactory();
const cartFactory = TestBed.inject(DaffCartFactory);
const cartItemFactory = new DaffCartItemFactory();

const mockCart = cartFactory.create({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('Cart', () => {
let cartItemCountElement: DebugElement;
let cartItemCountComponent: MockCartItemCountComponent;

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

beforeEach(waitForAsync(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {

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

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

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('CartResolverEffects', () => {
provideMockActions(() => actions$),
{
provide: DaffCartDriver,
useValue: new DaffTestingCartService(new DaffCartFactory()),
useValue: new DaffTestingCartService(TestBed.inject(DaffCartFactory)),
},
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('CheckoutEffects', () => {
let actions$: Observable<any>;
let effects: CheckoutEffects;
let router: Router;
const cartFactory: DaffCartFactory = new DaffCartFactory();
const cartFactory: DaffCartFactory = TestBed.inject(DaffCartFactory);
let stubCart: DaffCart;

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import * as fromDemoCheckout from '../../reducers/index';

const daffodilAddressFactory = new DaffAddressFactory();
const paymentFactory = new DaffPaymentFactory();
const cartFactory = new DaffCartFactory();
const cartFactory = TestBed.inject(DaffCartFactory);
const cartItemFactory = new DaffCartItemFactory();

const stubShippingAddress = daffodilAddressFactory.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {

import { ThankYouViewComponent } from './thank-you-view.component';

const cartFactory = new DaffCartFactory();
const cartFactory = TestBed.inject(DaffCartFactory);
const cartItemFactory = new DaffCartItemFactory();
const stubCart: DaffCart = cartFactory.create({ items: [cartItemFactory.create()]});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('DaffAuthorizeNetEffects', () => {
describe('updatePaymentSuccessSubstream$', () => {

it('should dispatch DaffAuthorizeNetUpdatePaymentSuccess when the cart payment method has been successfully updated', () => {
const stubCart = new DaffCartFactory().create();
const stubCart = TestBed.inject(DaffCartFactory).create();
const authorizeNetUpdatePayment = new DaffAuthorizeNetUpdatePayment(paymentTokenRequest, stubAddress);
const cartPaymentUpdateWithBillingSuccess = new DaffCartPaymentUpdateWithBillingSuccess(stubCart);
const authorizeNetPaymentUpdateSuccess = new DaffAuthorizeNetUpdatePaymentSuccess();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { TestBed } from '@angular/core/testing';

import {
DaffCart,
DaffCartTotalTypeEnum,
} from '@daffodil/cart';
import { DaffCartTotalTypeEnum } from '@daffodil/cart';
import { MagentoCart } from '@daffodil/cart/driver/magento';
import { MagentoCartFactory } from '@daffodil/cart/driver/magento/testing';
import { daffAdd } from '@daffodil/core';
Expand All @@ -12,12 +9,11 @@ import { transformCartTotals } from './cart-totals-transformer';

describe('transformCartTotals', () => {
let stubMagentoCart: MagentoCart;
let expectedTotals: {totals: DaffCart['totals']};

beforeEach(() => {
TestBed.configureTestingModule({});

stubMagentoCart = new MagentoCartFactory().create({
stubMagentoCart = TestBed.inject(MagentoCartFactory).create({
shipping_addresses: [
{
selected_shipping_method: {
Expand All @@ -28,97 +24,22 @@ describe('transformCartTotals', () => {
},
],
});
const totalTax = stubMagentoCart.prices.applied_taxes.reduce((acc, tax) => (daffAdd(acc, tax.amount.value)), 0);
expectedTotals = {
totals: [
{
name: DaffCartTotalTypeEnum.grandTotal,
label: 'Grand Total',
value: stubMagentoCart.prices.grand_total.value,
},
{
name: DaffCartTotalTypeEnum.subtotalExcludingTax,
label: 'Subtotal Excluding Tax',
value: stubMagentoCart.prices.subtotal_excluding_tax.value,
},
{
name: DaffCartTotalTypeEnum.subtotalIncludingTax,
label: 'Subtotal Including Tax',
value: stubMagentoCart.prices.subtotal_including_tax.value,
},
{
name: DaffCartTotalTypeEnum.subtotalWithDiscountExcludingTax,
label: 'Subtotal with Discount Excluding Tax',
value: stubMagentoCart.prices.subtotal_with_discount_excluding_tax.value,
},
{
name: DaffCartTotalTypeEnum.subtotalWithDiscountIncludingTax,
label: 'Subtotal with Discount Including Tax',
value: daffAdd(stubMagentoCart.prices.subtotal_with_discount_excluding_tax.value, totalTax),
},
{
name: DaffCartTotalTypeEnum.tax,
label: 'Tax',
value: totalTax,
},
...stubMagentoCart.prices.discounts.map(discount => ({
name: DaffCartTotalTypeEnum.discount,
label: discount.label,
value: discount.amount.value,
})),
{
name: DaffCartTotalTypeEnum.shipping,
label: 'Shipping',
value: stubMagentoCart.shipping_addresses[0].selected_shipping_method.amount.value,
},
],
};
});

it('should transform cart totals', () => {
expect(transformCartTotals(stubMagentoCart)).toEqual(expectedTotals);
const totalTax = stubMagentoCart.prices.applied_taxes.reduce((acc, tax) => (daffAdd(acc, tax.amount.value)), 0);
const transformedTotals = transformCartTotals(stubMagentoCart).totals;

expect(transformedTotals[DaffCartTotalTypeEnum.grandTotal].value).toEqual(stubMagentoCart.prices.grand_total.value);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalExcludingTax].value).toEqual(stubMagentoCart.prices.subtotal_excluding_tax.value);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalIncludingTax].value).toEqual(stubMagentoCart.prices.subtotal_including_tax.value);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalWithDiscountExcludingTax].value).toEqual(stubMagentoCart.prices.subtotal_with_discount_excluding_tax.value);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalWithDiscountIncludingTax].value).toEqual(daffAdd(stubMagentoCart.prices.subtotal_with_discount_excluding_tax.value, totalTax));
expect(transformedTotals[DaffCartTotalTypeEnum.tax].value).toEqual(totalTax);
expect(transformedTotals[DaffCartTotalTypeEnum.shipping].value).toEqual(stubMagentoCart.shipping_addresses[0].selected_shipping_method.amount.value);
});

it('should transform cart totals when magento gives null/empty values', () => {
expectedTotals = {
totals: [
{
name: DaffCartTotalTypeEnum.grandTotal,
label: 'Grand Total',
value: null,
},
{
name: DaffCartTotalTypeEnum.subtotalExcludingTax,
label: 'Subtotal Excluding Tax',
value: null,
},
{
name: DaffCartTotalTypeEnum.subtotalIncludingTax,
label: 'Subtotal Including Tax',
value: null,
},
{
name: DaffCartTotalTypeEnum.subtotalWithDiscountExcludingTax,
label: 'Subtotal with Discount Excluding Tax',
value: null,
},
{
name: DaffCartTotalTypeEnum.subtotalWithDiscountIncludingTax,
label: 'Subtotal with Discount Including Tax',
value: null,
},
{
name: DaffCartTotalTypeEnum.tax,
label: 'Tax',
value: 0,
},
{
name: DaffCartTotalTypeEnum.shipping,
label: 'Shipping',
value: null,
},
],
};
stubMagentoCart.prices.applied_taxes = [];
stubMagentoCart.prices.discounts = [];
stubMagentoCart.prices.grand_total.value = null;
Expand All @@ -127,49 +48,18 @@ describe('transformCartTotals', () => {
stubMagentoCart.prices.subtotal_with_discount_excluding_tax.value = null;
stubMagentoCart.shipping_addresses[0].selected_shipping_method.amount.value = null;

expect(transformCartTotals(stubMagentoCart)).toEqual(expectedTotals);
const transformedTotals = transformCartTotals(stubMagentoCart).totals;

expect(transformedTotals[DaffCartTotalTypeEnum.grandTotal].value).toEqual(null);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalExcludingTax].value).toEqual(null);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalIncludingTax].value).toEqual(null);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalWithDiscountExcludingTax].value).toEqual(null);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalWithDiscountIncludingTax].value).toEqual(null);
expect(transformedTotals[DaffCartTotalTypeEnum.tax].value).toEqual(0);
expect(transformedTotals[DaffCartTotalTypeEnum.shipping].value).toEqual(null);
});

it('should transform cart totals when magento gives empty shipping addresses', () => {
expectedTotals = {
totals: [
{
name: DaffCartTotalTypeEnum.grandTotal,
label: 'Grand Total',
value: null,
},
{
name: DaffCartTotalTypeEnum.subtotalExcludingTax,
label: 'Subtotal Excluding Tax',
value: null,
},
{
name: DaffCartTotalTypeEnum.subtotalIncludingTax,
label: 'Subtotal Including Tax',
value: null,
},
{
name: DaffCartTotalTypeEnum.subtotalWithDiscountExcludingTax,
label: 'Subtotal with Discount Excluding Tax',
value: null,
},
{
name: DaffCartTotalTypeEnum.subtotalWithDiscountIncludingTax,
label: 'Subtotal with Discount Including Tax',
value: null,
},
{
name: DaffCartTotalTypeEnum.tax,
label: 'Tax',
value: 0,
},
{
name: DaffCartTotalTypeEnum.shipping,
label: 'Shipping',
value: null,
},
],
};
stubMagentoCart.prices.applied_taxes = [];
stubMagentoCart.prices.discounts = [];
stubMagentoCart.prices.grand_total.value = null;
Expand All @@ -178,6 +68,14 @@ describe('transformCartTotals', () => {
stubMagentoCart.prices.subtotal_with_discount_excluding_tax.value = null;
stubMagentoCart.shipping_addresses = null;

expect(transformCartTotals(stubMagentoCart)).toEqual(expectedTotals);
const transformedTotals = transformCartTotals(stubMagentoCart).totals;

expect(transformedTotals[DaffCartTotalTypeEnum.grandTotal].value).toEqual(null);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalExcludingTax].value).toEqual(null);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalIncludingTax].value).toEqual(null);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalWithDiscountExcludingTax].value).toEqual(null);
expect(transformedTotals[DaffCartTotalTypeEnum.subtotalWithDiscountIncludingTax].value).toEqual(null);
expect(transformedTotals[DaffCartTotalTypeEnum.tax].value).toEqual(0);
expect(transformedTotals[DaffCartTotalTypeEnum.shipping].value).toEqual(null);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -181,48 +181,13 @@ describe('@daffodil/cart/driver/magento | MagentoCart', () => {

it('should return the expected cart totals', () => {
const totalTax = mockMagentoCart.prices.applied_taxes.reduce((acc, tax) => (daffAdd(acc, tax.amount.value)), 0);
expect(transformedCart.totals).toEqual([
{
name: DaffCartTotalTypeEnum.grandTotal,
label: 'Grand Total',
value: mockMagentoCart.prices.grand_total.value,
},
{
name: DaffCartTotalTypeEnum.subtotalExcludingTax,
label: 'Subtotal Excluding Tax',
value: mockMagentoCart.prices.subtotal_excluding_tax.value,
},
{
name: DaffCartTotalTypeEnum.subtotalIncludingTax,
label: 'Subtotal Including Tax',
value: mockMagentoCart.prices.subtotal_including_tax.value,
},
{
name: DaffCartTotalTypeEnum.subtotalWithDiscountExcludingTax,
label: 'Subtotal with Discount Excluding Tax',
value: mockMagentoCart.prices.subtotal_with_discount_excluding_tax.value,
},
{
name: DaffCartTotalTypeEnum.subtotalWithDiscountIncludingTax,
label: 'Subtotal with Discount Including Tax',
value: daffAdd(mockMagentoCart.prices.subtotal_with_discount_excluding_tax.value, totalTax),
},
{
name: DaffCartTotalTypeEnum.tax,
label: 'Tax',
value: totalTax,
},
...mockMagentoCart.prices.discounts.map(discount => ({
name: DaffCartTotalTypeEnum.discount,
label: discount.label,
value: discount.amount.value,
})),
{
name: DaffCartTotalTypeEnum.shipping,
label: 'Shipping',
value: mockMagentoCart.shipping_addresses[0].selected_shipping_method.amount.value,
},
]);
expect(transformedCart.totals[DaffCartTotalTypeEnum.grandTotal].value).toEqual(mockMagentoCart.prices.grand_total.value);
expect(transformedCart.totals[DaffCartTotalTypeEnum.subtotalExcludingTax].value).toEqual(mockMagentoCart.prices.subtotal_excluding_tax.value);
expect(transformedCart.totals[DaffCartTotalTypeEnum.subtotalIncludingTax].value).toEqual(mockMagentoCart.prices.subtotal_including_tax.value);
expect(transformedCart.totals[DaffCartTotalTypeEnum.subtotalWithDiscountExcludingTax].value).toEqual(mockMagentoCart.prices.subtotal_with_discount_excluding_tax.value);
expect(transformedCart.totals[DaffCartTotalTypeEnum.subtotalWithDiscountIncludingTax].value).toEqual(daffAdd(mockMagentoCart.prices.subtotal_with_discount_excluding_tax.value, totalTax));
expect(transformedCart.totals[DaffCartTotalTypeEnum.tax].value).toEqual(totalTax);
expect(transformedCart.totals[DaffCartTotalTypeEnum.shipping].value).toEqual(mockMagentoCart.shipping_addresses[0].selected_shipping_method.amount.value);
});

it('should call the shipping information transformer with the shipping method', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Cart | State | Guards | DaffBillingAddressGuard', () => {

describe('canActivate', () => {
it('should allow activation when there is a billing address', () => {
const cart: DaffCart = new DaffCartFactory().create({
const cart: DaffCart = TestBed.inject(DaffCartFactory).create({
billing_address: new DaffCartAddressFactory().create(),
});
store.dispatch(new DaffCartLoadSuccess(cart));
Expand All @@ -65,7 +65,7 @@ describe('Cart | State | Guards | DaffBillingAddressGuard', () => {

beforeEach(() => {
spyOn(router, 'navigateByUrl');
const cart: DaffCart = new DaffCartFactory().create({
const cart: DaffCart = TestBed.inject(DaffCartFactory).create({
billing_address: null,
});
store.dispatch(new DaffCartLoadSuccess(cart));
Expand Down
Loading

0 comments on commit 8bdb152

Please sign in to comment.