Skip to content

Commit

Permalink
test(cart): fix factory partial types (#1834)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Nov 24, 2023
1 parent 2bafc50 commit ef17259
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { faker } from '@faker-js/faker';

import { MockCart } from '@daffodil/cart/testing';
import { DaffCartTotalFactory } from '@daffodil/cart/testing';
import { DaffCartShippingInformationFactory } from '@daffodil/cart/testing';
import { DaffCartWithStoreCredit } from '@daffodil/cart-store-credit';
import { DaffModelFactory } from '@daffodil/core/testing';

Expand All @@ -22,7 +23,8 @@ export class MockDaffCartWithStoreCredit extends MockCart implements DaffCartWit
export class DaffCartWithStoreCreditFactory extends DaffModelFactory<DaffCartWithStoreCredit>{
constructor(
totalFactory: DaffCartTotalFactory,
shippingInformationFactory: DaffCartShippingInformationFactory,
) {
super(MockDaffCartWithStoreCredit, totalFactory);
super(MockDaffCartWithStoreCredit, totalFactory, shippingInformationFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@ import { TestBed } from '@angular/core/testing';

import { DaffCartTotalTypeEnum } from '@daffodil/cart';
import { MagentoCart } from '@daffodil/cart/driver/magento';
import { MagentoCartFactory } from '@daffodil/cart/driver/magento/testing';
import {
MagentoCartFactory,
MagentoCartShippingMethodFactory,
MagentoShippingAddressFactory,
} from '@daffodil/cart/driver/magento/testing';
import { daffAdd } from '@daffodil/core';

import { transformCartTotals } from './cart-totals-transformer';

describe('transformCartTotals', () => {
let magentoShippingAddressFactory: MagentoShippingAddressFactory;
let magentoShippingMethodFactory: MagentoCartShippingMethodFactory;

let stubMagentoCart: MagentoCart;

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

magentoShippingAddressFactory = TestBed.inject(MagentoShippingAddressFactory);
magentoShippingMethodFactory = TestBed.inject(MagentoCartShippingMethodFactory);
stubMagentoCart = TestBed.inject(MagentoCartFactory).create({
shipping_addresses: [
{
selected_shipping_method: {
magentoShippingAddressFactory.create({
selected_shipping_method: magentoShippingMethodFactory.create({
amount: {
value: 100,
currency: 'USD',
},
},
},
}),
}),
],
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ describe('@daffodil/cart/driver/magento | MagentoCart', () => {
mockDaffCart = daffCartFactory.create();
mockMagentoCart = magentoCartFactory.create({
shipping_addresses: [
{
selected_shipping_method: {
magentoShippingAddressFactory.create({
selected_shipping_method: magentoShippingMethodFactory.create({
amount: {
value: 100,
currency: 'USD',
},
},
},
}),
}),
],
});
mockShippingAddress = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ describe('Cart | State | Guards | DaffShippingMethodGuard', () => {

describe('canActivate', () => {
it('should allow activation when there is a shipping method', () => {
const cart: DaffCart = TestBed.inject(DaffCartFactory).create({
shipping_information: new DaffCartShippingRateFactory().create(),
});
const cart: DaffCart = TestBed.inject(DaffCartFactory).create();
store.dispatch(new DaffCartLoadSuccess(cart));
const expected = cold('(a|)', { a: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('DaffEmptyCartResolver', () => {
done();
});

stubCart = cartFactory.create({ items: cartItemFactory.create() });
stubCart = cartFactory.create({ items: cartItemFactory.createMany(1) });
cartResolverSubject.next(stubCart);
});
});
Expand Down
10 changes: 8 additions & 2 deletions libs/cart/state/src/facades/cart/cart.facade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,10 @@ describe('DaffCartFacade', () => {
describe('when all the shipping method is present', () => {
beforeEach(() => {
const cart: DaffCart = cartFactory.create({
shipping_information: shippingMethodFactory.create(),
shipping_information: {
address_id: 'id',
...shippingMethodFactory.create(),
},
});
facade.dispatch(new DaffCartLoadSuccess(cart));
});
Expand Down Expand Up @@ -1660,7 +1663,10 @@ describe('DaffCartFacade', () => {
shipping_address: cartAddressFactory.create(),
billing_address: cartAddressFactory.create(),
payment: paymentFactory.create(),
shipping_information: shippingMethodFactory.create(),
shipping_information: {
address_id: 'id',
...shippingMethodFactory.create(),
},
});
facade.dispatch(new DaffCartLoadSuccess(cart));
});
Expand Down
7 changes: 2 additions & 5 deletions libs/cart/state/src/selectors/cart/cart.selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import {
DaffCartItemFactory,
DaffCartAddressFactory,
DaffCartPaymentFactory,
DaffCartShippingRateFactory,
} from '@daffodil/cart/testing';
import {
DaffState,
Expand All @@ -57,14 +56,13 @@ import {

import { getCartSelectors } from './cart.selector';

describe('Cart | Selector | Cart', () => {
describe('@daffodil/cart/state | getCartSelectors', () => {
let store: Store<DaffCartStateRootSlice>;

let cartFactory: DaffCartFactory;
let cartItemFactory: DaffCartItemFactory;
let cartAddressFactory: DaffCartAddressFactory;
let paymentFactory: DaffCartPaymentFactory;
let shippingMethodFactory: DaffCartShippingRateFactory;

let orderId: string;
let cart: DaffCart;
Expand Down Expand Up @@ -163,7 +161,6 @@ describe('Cart | Selector | Cart', () => {
cartItemFactory = TestBed.inject(DaffCartItemFactory);
cartAddressFactory = TestBed.inject(DaffCartAddressFactory);
paymentFactory = TestBed.inject(DaffCartPaymentFactory);
shippingMethodFactory = TestBed.inject(DaffCartShippingRateFactory);

orderId = 'id';
error = {
Expand All @@ -175,8 +172,8 @@ describe('Cart | Selector | Cart', () => {
shipping_address: cartAddressFactory.create(),
billing_address: cartAddressFactory.create(),
payment: paymentFactory.create(),
shipping_information: shippingMethodFactory.create(),
});
cart.shipping_information.address_id = String(cart.shipping_address.id);
loading = {
[DaffCartOperationType.Cart]: DaffState.Complete,
[DaffCartOperationType.Item]: DaffState.Complete,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { TestBed } from '@angular/core/testing';

import { DaffCartShippingRate } from '@daffodil/cart';

import { DaffCartShippingRateFactory } from './cart-shipping-rate.factory';

describe('Cart | Testing | Factories | CartShippingRateFactory', () => {

let cartShippingRateFactory: DaffCartShippingRateFactory;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [DaffCartShippingRateFactory],
});

cartShippingRateFactory = TestBed.inject(DaffCartShippingRateFactory);
});

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

describe('create', () => {

let result: DaffCartShippingRate;

beforeEach(() => {
result = cartShippingRateFactory.create();
});

xit('should return a CartShippingRate with all required fields defined', () => {

});
});

describe('createMany', () => {
let result: DaffCartShippingRate[];

it('should create as many cart shipping rates as desired', () => {
result = cartShippingRateFactory.createMany(2);
expect(result.length).toEqual(2);

result = cartShippingRateFactory.createMany(3);
expect(result.length).toEqual(3);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@angular/core';
import { faker } from '@faker-js/faker/locale/en_US';

import { DaffCartShippingInformation } from '@daffodil/cart';
import { DaffModelFactory } from '@daffodil/core/testing';

import { MockCartShippingRate } from './cart-shipping-rate.factory';

export class MockCartShippingInformation extends MockCartShippingRate implements DaffCartShippingInformation {
address_id = faker.datatype.uuid();
}

@Injectable({
providedIn: 'root',
})
export class DaffCartShippingInformationFactory extends DaffModelFactory<DaffCartShippingInformation>{
constructor(){
super(MockCartShippingInformation);
}
}
26 changes: 13 additions & 13 deletions libs/cart/testing/src/factories/cart-shipping-rate.factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
import { TestBed } from '@angular/core/testing';

import { DaffCartShippingRate } from '@daffodil/cart';
import { DaffCartShippingInformation } from '@daffodil/cart';

import { DaffCartShippingRateFactory } from './cart-shipping-rate.factory';
import { DaffCartShippingInformationFactory } from './cart-shipping-information.factory';

describe('Cart | Testing | Factories | CartShippingRateFactory', () => {
describe('@daffodil/cart/testing | DaffCartShippingInformationFactory', () => {

let cartShippingRateFactory: DaffCartShippingRateFactory;
let cartShippingInformationFactory: DaffCartShippingInformationFactory;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [DaffCartShippingRateFactory],
providers: [DaffCartShippingInformationFactory],
});

cartShippingRateFactory = TestBed.inject(DaffCartShippingRateFactory);
cartShippingInformationFactory = TestBed.inject(DaffCartShippingInformationFactory);
});

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

describe('create', () => {

let result: DaffCartShippingRate;
let result: DaffCartShippingInformation;

beforeEach(() => {
result = cartShippingRateFactory.create();
result = cartShippingInformationFactory.create();
});

xit('should return a CartShippingRate with all required fields defined', () => {
xit('should return a CartShippingInformation with all required fields defined', () => {

});
});

describe('createMany', () => {
let result: DaffCartShippingRate[];
let result: DaffCartShippingInformation[];

it('should create as many cart shipping rates as desired', () => {
result = cartShippingRateFactory.createMany(2);
result = cartShippingInformationFactory.createMany(2);
expect(result.length).toEqual(2);

result = cartShippingRateFactory.createMany(3);
result = cartShippingInformationFactory.createMany(3);
expect(result.length).toEqual(3);
});
});
Expand Down
9 changes: 6 additions & 3 deletions libs/cart/testing/src/factories/cart.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { faker } from '@faker-js/faker/locale/en_US';
import { DaffCart } from '@daffodil/cart';
import { DaffModelFactory } from '@daffodil/core/testing';

import { DaffCartShippingInformationFactory } from './cart-shipping-information.factory';
import { DaffCartTotalFactory } from './cart-total.factory';

export class MockCart implements DaffCart {
Expand All @@ -14,15 +15,16 @@ export class MockCart implements DaffCart {
items = [];
billing_address = null;
shipping_address = null;
shipping_information = null;
shipping_information = this.shippingInformationFactory.create();
totals = this.totalFactory.createAllTotals();
payment = null;
available_shipping_methods = [];
available_payment_methods = [];
extra_attributes = {};

constructor(
private totalFactory: DaffCartTotalFactory,
protected totalFactory: DaffCartTotalFactory,
protected shippingInformationFactory: DaffCartShippingInformationFactory,
) {}
};

Expand All @@ -32,7 +34,8 @@ export class MockCart implements DaffCart {
export class DaffCartFactory extends DaffModelFactory<DaffCart>{
constructor(
totalFactory: DaffCartTotalFactory,
shippingInformationFactory: DaffCartShippingInformationFactory,
) {
super(MockCart, totalFactory);
super(MockCart, totalFactory, shippingInformationFactory);
}
}
1 change: 1 addition & 0 deletions libs/cart/testing/src/factories/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export * from './cart-item/composite-cart-item.factory';
export * from './cart-address.factory';
export * from './cart-payment.factory';
export * from './cart-shipping-rate.factory';
export * from './cart-shipping-information.factory';
export * from './cart-coupon.factory';
export * from './cart-total.factory';

0 comments on commit ef17259

Please sign in to comment.