diff --git a/apps/demo/src/app/drivers/in-memory/backend/backend.service.spec.ts b/apps/demo/src/app/drivers/in-memory/backend/backend.service.spec.ts index eb27722c48..68b7c3c9e5 100644 --- a/apps/demo/src/app/drivers/in-memory/backend/backend.service.spec.ts +++ b/apps/demo/src/app/drivers/in-memory/backend/backend.service.spec.ts @@ -49,7 +49,7 @@ describe('@daffodil/demo | DemoInMemoryBackendService', () => { returnedValue, ); reqInfo = { - collectionName: DaffInMemoryBackendCartRootService.COLLECTION_NAMES[0], + collectionName: 'cart', }; result = service.post(reqInfo); @@ -160,7 +160,7 @@ describe('@daffodil/demo | DemoInMemoryBackendService', () => { returnedValue = 'returnedValue'; spyOn(service['cartTestingService'], 'get').and.returnValue(returnedValue); reqInfo = { - collectionName: DaffInMemoryBackendCartRootService.COLLECTION_NAMES[0], + collectionName: 'cart', }; result = service.get(reqInfo); @@ -195,7 +195,7 @@ describe('@daffodil/demo | DemoInMemoryBackendService', () => { returnedValue, ); reqInfo = { - collectionName: DaffInMemoryBackendCartRootService.COLLECTION_NAMES[0], + collectionName: 'cart', }; result = service.put(reqInfo); @@ -231,7 +231,7 @@ describe('@daffodil/demo | DemoInMemoryBackendService', () => { returnedValue, ); reqInfo = { - collectionName: DaffInMemoryBackendCartRootService.COLLECTION_NAMES[0], + collectionName: 'cart', }; result = service.delete(reqInfo); diff --git a/apps/demo/src/app/drivers/in-memory/backend/backend.service.ts b/apps/demo/src/app/drivers/in-memory/backend/backend.service.ts index 7bd810c912..b48a930c61 100644 --- a/apps/demo/src/app/drivers/in-memory/backend/backend.service.ts +++ b/apps/demo/src/app/drivers/in-memory/backend/backend.service.ts @@ -36,7 +36,7 @@ export class DemoInMemoryBackendService implements InMemoryDbService { post(reqInfo: any) { const collectionName = reqInfo.collectionName; - if (DaffInMemoryBackendCartRootService.COLLECTION_NAMES.indexOf(collectionName) > -1) { + if (this.cartTestingService.canHandle(collectionName)) { return this.cartTestingService.post(reqInfo); } else if (collectionName === 'auth') { return this.authTestingService.post(reqInfo); @@ -51,7 +51,7 @@ export class DemoInMemoryBackendService implements InMemoryDbService { return this.productTestingService.get(reqInfo); } else if (collectionName === 'navigation') { return this.navigationTestingService.get(reqInfo); - } else if (DaffInMemoryBackendCartRootService.COLLECTION_NAMES.indexOf(collectionName) > -1) { + } else if (this.cartTestingService.canHandle(collectionName)) { return this.cartTestingService.get(reqInfo); } else if (collectionName === 'countries') { return this.geographyTestingService.get(reqInfo); @@ -62,14 +62,14 @@ export class DemoInMemoryBackendService implements InMemoryDbService { put(reqInfo: any) { const collectionName = reqInfo.collectionName; - if(DaffInMemoryBackendCartRootService.COLLECTION_NAMES.indexOf(collectionName) > -1) { + if(this.cartTestingService.canHandle(collectionName)) { return this.cartTestingService.put(reqInfo); } } delete(reqInfo: any) { const collectionName = reqInfo.collectionName; - if(DaffInMemoryBackendCartRootService.COLLECTION_NAMES.indexOf(collectionName) > -1) { + if(this.cartTestingService.canHandle(collectionName)) { return this.cartTestingService.delete(reqInfo); } } diff --git a/libs/cart/driver/in-memory/src/backend/cart-address/cart-address.service.ts b/libs/cart/driver/in-memory/src/backend/cart-address/cart-address.service.ts index a827865240..e8bd43614a 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-address/cart-address.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-address/cart-address.service.ts @@ -8,8 +8,9 @@ import { } from 'angular-in-memory-web-api'; import { DaffCart } from '@daffodil/cart'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory'; +import { DAFF_CART_IN_MEMORY_CART_ADDRESS_COLLECTION_NAME } from '../../collection-names'; import { DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK, DaffCartInMemoryExtraAttributesHook, @@ -21,7 +22,9 @@ import { @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartAddressService implements DaffInMemoryDataServiceInterface { +export class DaffInMemoryBackendCartAddressService implements DaffInMemorySingleRouteableBackend { + readonly collectionName = DAFF_CART_IN_MEMORY_CART_ADDRESS_COLLECTION_NAME; + constructor( @Inject(DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK) private extraFieldsHook: DaffCartInMemoryExtraAttributesHook, ) {} diff --git a/libs/cart/driver/in-memory/src/backend/cart-billing-address/cart-billing-address.service.ts b/libs/cart/driver/in-memory/src/backend/cart-billing-address/cart-billing-address.service.ts index 3ebfa68261..0cca5a4110 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-billing-address/cart-billing-address.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-billing-address/cart-billing-address.service.ts @@ -11,8 +11,9 @@ import { DaffCart, DaffCartAddress, } from '@daffodil/cart'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory'; +import { DAFF_CART_IN_MEMORY_CART_BILLING_ADDRESS_COLLECTION_NAME } from '../../collection-names'; import { DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK, DaffCartInMemoryExtraAttributesHook, @@ -24,7 +25,9 @@ import { @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartBillingAddressService implements DaffInMemoryDataServiceInterface { +export class DaffInMemoryBackendCartBillingAddressService implements DaffInMemorySingleRouteableBackend { + readonly collectionName = DAFF_CART_IN_MEMORY_CART_BILLING_ADDRESS_COLLECTION_NAME; + constructor( @Inject(DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK) private extraFieldsHook: DaffCartInMemoryExtraAttributesHook, ) {} diff --git a/libs/cart/driver/in-memory/src/backend/cart-coupon/cart-coupon.service.ts b/libs/cart/driver/in-memory/src/backend/cart-coupon/cart-coupon.service.ts index 1e7f7acec9..0d131ae151 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-coupon/cart-coupon.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-coupon/cart-coupon.service.ts @@ -11,8 +11,9 @@ import { DaffCart, DaffCartCoupon, } from '@daffodil/cart'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory'; +import { DAFF_CART_IN_MEMORY_CART_COUPON_COLLECTION_NAME } from '../../collection-names'; import { DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK, DaffCartInMemoryExtraAttributesHook, @@ -24,7 +25,9 @@ import { @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartCouponService implements DaffInMemoryDataServiceInterface { +export class DaffInMemoryBackendCartCouponService implements DaffInMemorySingleRouteableBackend { + readonly collectionName = DAFF_CART_IN_MEMORY_CART_COUPON_COLLECTION_NAME; + constructor( @Inject(DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK) private extraFieldsHook: DaffCartInMemoryExtraAttributesHook, ) {} diff --git a/libs/cart/driver/in-memory/src/backend/cart-items/cart-items.service.ts b/libs/cart/driver/in-memory/src/backend/cart-items/cart-items.service.ts index 9c859299b7..85eb42988e 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-items/cart-items.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-items/cart-items.service.ts @@ -13,9 +13,10 @@ import { DaffCartItemInput, } from '@daffodil/cart'; import { DaffCartItemFactory } from '@daffodil/cart/testing'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory'; import { DaffInMemoryBackendProductService } from '@daffodil/product/driver/in-memory'; +import { DAFF_CART_IN_MEMORY_CART_ITEMS_COLLECTION_NAME } from '../../collection-names'; import { daffCartInMemoryComputeCartItemPrices } from '../../helpers/compute-cart-item-prices'; import { daffCartInMemoryComputeCartTotals } from '../../helpers/compute-cart-totals'; import { @@ -29,7 +30,9 @@ import { @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartItemsService implements DaffInMemoryDataServiceInterface { +export class DaffInMemoryBackendCartItemsService implements DaffInMemorySingleRouteableBackend { + readonly collectionName = DAFF_CART_IN_MEMORY_CART_ITEMS_COLLECTION_NAME; + constructor( private cartItemFactory: DaffCartItemFactory, @Inject(DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK) private extraFieldsHook: DaffCartInMemoryExtraAttributesHook, diff --git a/libs/cart/driver/in-memory/src/backend/cart-order/cart-order.service.ts b/libs/cart/driver/in-memory/src/backend/cart-order/cart-order.service.ts index 375119024c..48c9f9d5d0 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-order/cart-order.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-order/cart-order.service.ts @@ -5,7 +5,9 @@ import { } from 'angular-in-memory-web-api'; import { DaffCartOrderResult } from '@daffodil/cart'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_ORDER_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -13,7 +15,9 @@ import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartOrderService implements DaffInMemoryDataServiceInterface { +export class DaffInMemoryBackendCartOrderService implements DaffInMemorySingleRouteableBackend { + readonly collectionName = DAFF_CART_IN_MEMORY_CART_ORDER_COLLECTION_NAME; + post(reqInfo: RequestInfo) { return reqInfo.utils.createResponse$(() => ({ body: this.placeOrder(reqInfo), diff --git a/libs/cart/driver/in-memory/src/backend/cart-payment-methods/cart-payment-methods.service.ts b/libs/cart/driver/in-memory/src/backend/cart-payment-methods/cart-payment-methods.service.ts index a3f82fb56c..f41efaa75c 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-payment-methods/cart-payment-methods.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-payment-methods/cart-payment-methods.service.ts @@ -8,7 +8,9 @@ import { DaffCart, DaffCartPaymentMethod, } from '@daffodil/cart'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_PAYMENT_METHODS_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -16,7 +18,9 @@ import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartPaymentMethodsService implements DaffInMemoryDataServiceInterface { +export class DaffInMemoryBackendCartPaymentMethodsService implements DaffInMemorySingleRouteableBackend { + readonly collectionName = DAFF_CART_IN_MEMORY_CART_PAYMENT_METHODS_COLLECTION_NAME; + get(reqInfo: RequestInfo) { return reqInfo.utils.createResponse$(() => ({ body: this.listPaymentMethods(reqInfo), diff --git a/libs/cart/driver/in-memory/src/backend/cart-payment/cart-payment.service.ts b/libs/cart/driver/in-memory/src/backend/cart-payment/cart-payment.service.ts index ba56b4de18..9c7c135d43 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-payment/cart-payment.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-payment/cart-payment.service.ts @@ -11,8 +11,9 @@ import { DaffCart, DaffCartPaymentMethod, } from '@daffodil/cart'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory'; +import { DAFF_CART_IN_MEMORY_CART_PAYMENT_COLLECTION_NAME } from '../../collection-names'; import { DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK, DaffCartInMemoryExtraAttributesHook, @@ -24,7 +25,9 @@ import { @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartPaymentService implements DaffInMemoryDataServiceInterface { +export class DaffInMemoryBackendCartPaymentService implements DaffInMemorySingleRouteableBackend { + readonly collectionName = DAFF_CART_IN_MEMORY_CART_PAYMENT_COLLECTION_NAME; + constructor( @Inject(DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK) private extraFieldsHook: DaffCartInMemoryExtraAttributesHook, ) {} diff --git a/libs/cart/driver/in-memory/src/backend/cart-root.service.ts b/libs/cart/driver/in-memory/src/backend/cart-root.service.ts index ef598d5a27..f706e09343 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-root.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-root.service.ts @@ -2,11 +2,17 @@ import { Injectable } from '@angular/core'; import { InMemoryDbService, RequestInfo, - STATUS, } from 'angular-in-memory-web-api'; +import { + Observable, + of, +} from 'rxjs'; import { DaffCart } from '@daffodil/cart'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { + DaffInMemoryBackendDelegate, + DaffInMemoryMultiRouteableBackend, +} from '@daffodil/driver/in-memory'; import { DaffInMemoryBackendCartService } from './cart/cart.service'; import { DaffInMemoryBackendCartAddressService } from './cart-address/cart-address.service'; @@ -20,6 +26,24 @@ import { DaffInMemoryBackendCartShippingAddressService } from './cart-shipping-a import { DaffInMemoryBackendCartShippingInformationService } from './cart-shipping-information/cart-shipping-information.service'; import { DaffInMemoryBackendCartShippingMethodsService } from './cart-shipping-methods/cart-shipping-methods.service'; +/** + * The collections that the root service manages. + * Useful for a higher-level backend that delegates to this one based on collection name. + */ +const COLLECTION_NAMES = [ + 'cart', + 'cart-items', + 'cart-order', + 'cart-coupon', + 'cart-address', + 'cart-shipping-address', + 'cart-billing-address', + 'cart-payment-methods', + 'cart-shipping-methods', + 'cart-payment', + 'cart-shipping-information', +]; + /** * The root cart in-memory backend. * Creates the database and delegates requests to child backends. @@ -27,43 +51,50 @@ import { DaffInMemoryBackendCartShippingMethodsService } from './cart-shipping-m @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartRootService implements InMemoryDbService, DaffInMemoryDataServiceInterface { - /** - * The collections that the root service manages. - * Useful for a higher-level backend that delegates to this one based on collection name. - */ - public static readonly COLLECTION_NAMES = [ - 'cart', - 'cart-items', - 'cart-order', - 'cart-coupon', - 'cart-address', - 'cart-shipping-address', - 'cart-billing-address', - 'cart-payment-methods', - 'cart-shipping-methods', - 'cart-payment', - 'cart-shipping-information', - ]; - +export class DaffInMemoryBackendCartRootService extends DaffInMemoryBackendDelegate implements InMemoryDbService, DaffInMemoryMultiRouteableBackend { /** * The collection of carts in the backend. */ public carts: DaffCart[] = []; constructor( - private cartService: DaffInMemoryBackendCartService, - private cartItemsService: DaffInMemoryBackendCartItemsService, - private cartOrderService: DaffInMemoryBackendCartOrderService, - private cartCouponService: DaffInMemoryBackendCartCouponService, - private cartAddressService: DaffInMemoryBackendCartAddressService, - private cartShippingAddressService: DaffInMemoryBackendCartShippingAddressService, - private cartBillingAddressService: DaffInMemoryBackendCartBillingAddressService, - private cartPaymentMethodsService: DaffInMemoryBackendCartPaymentMethodsService, - private cartShippingMethodsService: DaffInMemoryBackendCartShippingMethodsService, - private cartPaymentService: DaffInMemoryBackendCartPaymentService, - private cartShippingInformationService: DaffInMemoryBackendCartShippingInformationService, - ) {} + cartService: DaffInMemoryBackendCartService, + cartItemsService: DaffInMemoryBackendCartItemsService, + cartOrderService: DaffInMemoryBackendCartOrderService, + cartCouponService: DaffInMemoryBackendCartCouponService, + cartAddressService: DaffInMemoryBackendCartAddressService, + cartShippingAddressService: DaffInMemoryBackendCartShippingAddressService, + cartBillingAddressService: DaffInMemoryBackendCartBillingAddressService, + cartPaymentMethodsService: DaffInMemoryBackendCartPaymentMethodsService, + cartShippingMethodsService: DaffInMemoryBackendCartShippingMethodsService, + cartPaymentService: DaffInMemoryBackendCartPaymentService, + cartShippingInformationService: DaffInMemoryBackendCartShippingInformationService, + ) { + super([ + cartService, + cartItemsService, + cartOrderService, + cartCouponService, + cartAddressService, + cartShippingAddressService, + cartBillingAddressService, + cartPaymentMethodsService, + cartShippingMethodsService, + cartPaymentService, + cartShippingInformationService, + ]); + } + + protected override delegateRequest(reqInfo: RequestInfo, method): Observable { + return super.delegateRequest({ + ...reqInfo, + collection: this.carts, + }, method); + } + + canHandle(collectionName: string): boolean { + return COLLECTION_NAMES.includes(collectionName); + } createDb(reqInfo: RequestInfo) { if (reqInfo) { @@ -73,69 +104,8 @@ export class DaffInMemoryBackendCartRootService implements InMemoryDbService, Da } } - return { + return of({ cart: this.carts, - }; - } - - get(reqInfo: RequestInfo) { - return this.delegateRequest(reqInfo); - } - - post(reqInfo: RequestInfo) { - return this.delegateRequest(reqInfo); - } - - put(reqInfo: RequestInfo) { - return this.delegateRequest(reqInfo); - } - - delete(reqInfo: RequestInfo) { - return this.delegateRequest(reqInfo); - } - - private delegateRequest(reqInfo: RequestInfo) { - reqInfo.collection = this.carts; - - switch (reqInfo.collectionName) { - case 'cart': - return this.cartService[reqInfo.method](reqInfo); - - case 'cart-items': - return this.cartItemsService[reqInfo.method](reqInfo); - - case 'cart-order': - return this.cartOrderService[reqInfo.method](reqInfo); - - case 'cart-coupon': - return this.cartCouponService[reqInfo.method](reqInfo); - - case 'cart-address': - return this.cartAddressService[reqInfo.method](reqInfo); - - case 'cart-shipping-address': - return this.cartShippingAddressService[reqInfo.method](reqInfo); - - case 'cart-billing-address': - return this.cartBillingAddressService[reqInfo.method](reqInfo); - - case 'cart-payment-methods': - return this.cartPaymentMethodsService[reqInfo.method](reqInfo); - - case 'cart-shipping-methods': - return this.cartShippingMethodsService[reqInfo.method](reqInfo); - - case 'cart-payment': - return this.cartPaymentService[reqInfo.method](reqInfo); - - case 'cart-shipping-information': - return this.cartShippingInformationService[reqInfo.method](reqInfo); - - default: - return reqInfo.utils.createResponse$(() => ({ - body: {}, - status: STATUS.OK, - })); - } + }); } } diff --git a/libs/cart/driver/in-memory/src/backend/cart-root.service.unit.spec.ts b/libs/cart/driver/in-memory/src/backend/cart-root.service.unit.spec.ts index ce86d716de..c31f7eb720 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-root.service.unit.spec.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-root.service.unit.spec.ts @@ -162,15 +162,18 @@ describe('DaffInMemoryBackendCartRootService | Unit', () => { }); describe('after initialization', () => { - let result; + let result: ReturnType; beforeEach(() => { service.carts = []; result = service.createDb(reqInfoStub); }); - it('should have an empty array in DB', () => { - expect(result.cart).toEqual([]); + it('should have an empty array in DB', (done) => { + result.subscribe((res) => { + expect(res.cart).toEqual([]); + done(); + }); }); }); diff --git a/libs/cart/driver/in-memory/src/backend/cart-shipping-address/cart-shipping-address.service.ts b/libs/cart/driver/in-memory/src/backend/cart-shipping-address/cart-shipping-address.service.ts index 599dbf2518..f6cc542ab9 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-shipping-address/cart-shipping-address.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-shipping-address/cart-shipping-address.service.ts @@ -11,8 +11,9 @@ import { DaffCart, DaffCartAddress, } from '@daffodil/cart'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory'; +import { DAFF_CART_IN_MEMORY_CART_SHIPPING_ADDRESS_COLLECTION_NAME } from '../../collection-names'; import { DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK, DaffCartInMemoryExtraAttributesHook, @@ -24,7 +25,9 @@ import { @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartShippingAddressService implements DaffInMemoryDataServiceInterface { +export class DaffInMemoryBackendCartShippingAddressService implements DaffInMemorySingleRouteableBackend { + readonly collectionName = DAFF_CART_IN_MEMORY_CART_SHIPPING_ADDRESS_COLLECTION_NAME; + constructor( @Inject(DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK) private extraFieldsHook: DaffCartInMemoryExtraAttributesHook, ) {} diff --git a/libs/cart/driver/in-memory/src/backend/cart-shipping-information/cart-shipping-information.service.ts b/libs/cart/driver/in-memory/src/backend/cart-shipping-information/cart-shipping-information.service.ts index cd4de5cacc..af9a88a6c9 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-shipping-information/cart-shipping-information.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-shipping-information/cart-shipping-information.service.ts @@ -11,9 +11,10 @@ import { DaffCart, DaffCartShippingRate, } from '@daffodil/cart'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory'; import { DaffInMemoryBackendProductService } from '@daffodil/product/driver/in-memory'; +import { DAFF_CART_IN_MEMORY_CART_SHIPPING_COLLECTION_NAME } from '../../collection-names'; import { daffCartInMemoryComputeCartTotals } from '../../helpers/compute-cart-totals'; import { DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK, @@ -26,7 +27,9 @@ import { @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartShippingInformationService implements DaffInMemoryDataServiceInterface { +export class DaffInMemoryBackendCartShippingInformationService implements DaffInMemorySingleRouteableBackend { + readonly collectionName = DAFF_CART_IN_MEMORY_CART_SHIPPING_COLLECTION_NAME; + constructor( @Inject(DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK) private extraFieldsHook: DaffCartInMemoryExtraAttributesHook, private productBackend: DaffInMemoryBackendProductService, diff --git a/libs/cart/driver/in-memory/src/backend/cart-shipping-methods/cart-shipping-methods.service.ts b/libs/cart/driver/in-memory/src/backend/cart-shipping-methods/cart-shipping-methods.service.ts index 39d5f22681..4dfc294963 100644 --- a/libs/cart/driver/in-memory/src/backend/cart-shipping-methods/cart-shipping-methods.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart-shipping-methods/cart-shipping-methods.service.ts @@ -9,7 +9,9 @@ import { DaffCartShippingRate, } from '@daffodil/cart'; import { DaffCartShippingRateFactory } from '@daffodil/cart/testing'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_SHIPPING_METHODS_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -17,7 +19,9 @@ import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartShippingMethodsService implements DaffInMemoryDataServiceInterface { +export class DaffInMemoryBackendCartShippingMethodsService implements DaffInMemorySingleRouteableBackend { + readonly collectionName = DAFF_CART_IN_MEMORY_CART_SHIPPING_METHODS_COLLECTION_NAME; + constructor( private shippingMethodFactory: DaffCartShippingRateFactory, ) {} diff --git a/libs/cart/driver/in-memory/src/backend/cart/cart.service.ts b/libs/cart/driver/in-memory/src/backend/cart/cart.service.ts index b37a2ddc32..e5ef73ea36 100644 --- a/libs/cart/driver/in-memory/src/backend/cart/cart.service.ts +++ b/libs/cart/driver/in-memory/src/backend/cart/cart.service.ts @@ -11,9 +11,10 @@ import { import { DaffCart } from '@daffodil/cart'; import { DaffCartFactory } from '@daffodil/cart/testing'; -import { DaffInMemoryDataServiceInterface } from '@daffodil/driver/in-memory'; +import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory'; import { DaffInMemoryBackendProductService } from '@daffodil/product/driver/in-memory'; +import { DAFF_CART_IN_MEMORY_CART_COLLECTION_NAME } from '../../collection-names'; import { daffCartInMemoryComputeCartTotals } from '../../helpers/compute-cart-totals'; import { DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK, @@ -26,7 +27,9 @@ import { @Injectable({ providedIn: 'root', }) -export class DaffInMemoryBackendCartService implements DaffInMemoryDataServiceInterface { +export class DaffInMemoryBackendCartService implements DaffInMemorySingleRouteableBackend { + readonly collectionName = DAFF_CART_IN_MEMORY_CART_COLLECTION_NAME; + constructor( private cartFactory: DaffCartFactory, @Inject(DAFF_CART_IN_MEMORY_EXTRA_ATTRIBUTES_HOOK) private extraFieldsHook: DaffCartInMemoryExtraAttributesHook, diff --git a/libs/cart/driver/in-memory/src/collection-names.ts b/libs/cart/driver/in-memory/src/collection-names.ts new file mode 100644 index 0000000000..395e50559a --- /dev/null +++ b/libs/cart/driver/in-memory/src/collection-names.ts @@ -0,0 +1,11 @@ +export const DAFF_CART_IN_MEMORY_CART_COLLECTION_NAME = 'cart'; +export const DAFF_CART_IN_MEMORY_CART_ITEMS_COLLECTION_NAME = 'cart-items'; +export const DAFF_CART_IN_MEMORY_CART_ORDER_COLLECTION_NAME = 'cart-order'; +export const DAFF_CART_IN_MEMORY_CART_COUPON_COLLECTION_NAME = 'cart-coupon'; +export const DAFF_CART_IN_MEMORY_CART_ADDRESS_COLLECTION_NAME = 'cart-address'; +export const DAFF_CART_IN_MEMORY_CART_SHIPPING_ADDRESS_COLLECTION_NAME = 'cart-shipping-address'; +export const DAFF_CART_IN_MEMORY_CART_BILLING_ADDRESS_COLLECTION_NAME = 'cart-billing-address'; +export const DAFF_CART_IN_MEMORY_CART_PAYMENT_METHODS_COLLECTION_NAME = 'cart-payment-methods'; +export const DAFF_CART_IN_MEMORY_CART_SHIPPING_METHODS_COLLECTION_NAME = 'cart-shipping-methods'; +export const DAFF_CART_IN_MEMORY_CART_PAYMENT_COLLECTION_NAME = 'cart-payment'; +export const DAFF_CART_IN_MEMORY_CART_SHIPPING_COLLECTION_NAME = 'cart-shipping-information'; diff --git a/libs/cart/driver/in-memory/src/drivers/cart-address/cart-address.service.spec.ts b/libs/cart/driver/in-memory/src/drivers/cart-address/cart-address.service.spec.ts index c7a5a1a19a..9cf2ac4dcf 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-address/cart-address.service.spec.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-address/cart-address.service.spec.ts @@ -7,6 +7,7 @@ import { provideHttpClientTesting, } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { DaffCart, @@ -19,8 +20,8 @@ import { import { DaffInMemoryCartAddressService } from './cart-address.service'; -describe('Driver | In Memory | Cart | CartAddressService', () => { - let cartBillingAddressService: DaffInMemoryCartAddressService; +describe('@daffodil/cart/driver/in-memory | CartAddressService', () => { + let service: DaffInMemoryCartAddressService; let httpMock: HttpTestingController; let cartFactory: DaffCartFactory; let cartAddressFactory: DaffCartAddressFactory; @@ -34,6 +35,12 @@ describe('Driver | In Memory | Cart | CartAddressService', () => { imports: [], providers: [ DaffInMemoryCartAddressService, + { + provide: InMemoryBackendConfig, + useValue: { + apiBase: 'api', + }, + }, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting(), ], @@ -42,7 +49,7 @@ describe('Driver | In Memory | Cart | CartAddressService', () => { httpMock = TestBed.inject(HttpTestingController); cartFactory = TestBed.inject(DaffCartFactory); cartAddressFactory = TestBed.inject(DaffCartAddressFactory); - cartBillingAddressService = TestBed.inject(DaffInMemoryCartAddressService); + service = TestBed.inject(DaffInMemoryCartAddressService); mockCart = cartFactory.create(); mockCartAddress = cartAddressFactory.create(); @@ -56,7 +63,7 @@ describe('Driver | In Memory | Cart | CartAddressService', () => { }); it('should be created', () => { - expect(cartBillingAddressService).toBeTruthy(); + expect(service).toBeTruthy(); }); describe('update', () => { @@ -69,11 +76,11 @@ describe('Driver | In Memory | Cart | CartAddressService', () => { }); it('should send a put request', done => { - cartBillingAddressService.update(cartId, mockCartAddressUpdate).subscribe(cart => { + service.update(cartId, mockCartAddressUpdate).subscribe(cart => { done(); }); - const req = httpMock.expectOne(`${cartBillingAddressService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('PUT'); expect(req.request.body).toEqual(mockCartAddressUpdate); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-address/cart-address.service.ts b/libs/cart/driver/in-memory/src/drivers/cart-address/cart-address.service.ts index 6db532b418..fdd447dce9 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-address/cart-address.service.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-address/cart-address.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { Observable } from 'rxjs'; import { @@ -7,6 +8,9 @@ import { DaffCartAddress, } from '@daffodil/cart'; import { DaffCartAddressServiceInterface } from '@daffodil/cart/driver'; +import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_ADDRESS_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -14,13 +18,13 @@ import { DaffCartAddressServiceInterface } from '@daffodil/cart/driver'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryCartAddressService implements DaffCartAddressServiceInterface { - /** - * The URL with which the driver makes calls to the backend. - */ - readonly url = '/api/cart-address'; - - constructor(private http: HttpClient) {} +export class DaffInMemoryCartAddressService extends DaffInMemoryDriverBase implements DaffCartAddressServiceInterface { + constructor( + private http: HttpClient, + config: InMemoryBackendConfig, + ) { + super(config, DAFF_CART_IN_MEMORY_CART_ADDRESS_COLLECTION_NAME); + } update(cartId: DaffCart['id'], address: DaffCartAddress): Observable> { return this.http.put>(`${this.url}/${cartId}`, address); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-billing-address/cart-billing-address.service.spec.ts b/libs/cart/driver/in-memory/src/drivers/cart-billing-address/cart-billing-address.service.spec.ts index 3b3f5b5a86..352233fe3f 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-billing-address/cart-billing-address.service.spec.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-billing-address/cart-billing-address.service.spec.ts @@ -7,6 +7,7 @@ import { provideHttpClientTesting, } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { DaffCart, @@ -19,8 +20,8 @@ import { import { DaffInMemoryCartBillingAddressService } from './cart-billing-address.service'; -describe('Driver | In Memory | Cart | CartBillingAddressService', () => { - let cartBillingAddressService: DaffInMemoryCartBillingAddressService; +describe('@daffodil/cart/driver/in-memory | CartBillingAddressService', () => { + let service: DaffInMemoryCartBillingAddressService; let httpMock: HttpTestingController; let cartFactory: DaffCartFactory; let cartAddressFactory: DaffCartAddressFactory; @@ -34,6 +35,12 @@ describe('Driver | In Memory | Cart | CartBillingAddressService', () => { imports: [], providers: [ DaffInMemoryCartBillingAddressService, + { + provide: InMemoryBackendConfig, + useValue: { + apiBase: 'api', + }, + }, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting(), ], @@ -42,7 +49,7 @@ describe('Driver | In Memory | Cart | CartBillingAddressService', () => { httpMock = TestBed.inject(HttpTestingController); cartFactory = TestBed.inject(DaffCartFactory); cartAddressFactory = TestBed.inject(DaffCartAddressFactory); - cartBillingAddressService = TestBed.inject(DaffInMemoryCartBillingAddressService); + service = TestBed.inject(DaffInMemoryCartBillingAddressService); mockCart = cartFactory.create(); mockCartAddress = cartAddressFactory.create(); @@ -55,16 +62,16 @@ describe('Driver | In Memory | Cart | CartBillingAddressService', () => { }); it('should be created', () => { - expect(cartBillingAddressService).toBeTruthy(); + expect(service).toBeTruthy(); }); describe('get | getting a cart billing address', () => { it('should send a get request', done => { - cartBillingAddressService.get(cartId).subscribe(res => { + service.get(cartId).subscribe(res => { done(); }); - const req = httpMock.expectOne(`${cartBillingAddressService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('GET'); req.flush(mockCartAddress); @@ -80,11 +87,11 @@ describe('Driver | In Memory | Cart | CartBillingAddressService', () => { }); it('should send a put request', done => { - cartBillingAddressService.update(cartId, mockCartAddressUpdate).subscribe(cart => { + service.update(cartId, mockCartAddressUpdate).subscribe(cart => { done(); }); - const req = httpMock.expectOne(`${cartBillingAddressService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('PUT'); expect(req.request.body).toEqual(mockCartAddressUpdate); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-billing-address/cart-billing-address.service.ts b/libs/cart/driver/in-memory/src/drivers/cart-billing-address/cart-billing-address.service.ts index 21b723f432..30601c57e8 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-billing-address/cart-billing-address.service.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-billing-address/cart-billing-address.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { Observable } from 'rxjs'; import { @@ -7,6 +8,9 @@ import { DaffCart, } from '@daffodil/cart'; import { DaffCartBillingAddressServiceInterface } from '@daffodil/cart/driver'; +import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_BILLING_ADDRESS_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -14,13 +18,13 @@ import { DaffCartBillingAddressServiceInterface } from '@daffodil/cart/driver'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryCartBillingAddressService implements DaffCartBillingAddressServiceInterface { - /** - * The URL with which the driver makes calls to the backend. - */ - readonly url = '/api/cart-billing-address'; - - constructor(private http: HttpClient) {} +export class DaffInMemoryCartBillingAddressService extends DaffInMemoryDriverBase implements DaffCartBillingAddressServiceInterface { + constructor( + private http: HttpClient, + config: InMemoryBackendConfig, + ) { + super(config, DAFF_CART_IN_MEMORY_CART_BILLING_ADDRESS_COLLECTION_NAME); + } get(cartId: DaffCart['id']): Observable { return this.http.get(`${this.url}/${cartId}`); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-coupon/cart-coupon.service.spec.ts b/libs/cart/driver/in-memory/src/drivers/cart-coupon/cart-coupon.service.spec.ts index 86bc77c2d8..39931bd9eb 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-coupon/cart-coupon.service.spec.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-coupon/cart-coupon.service.spec.ts @@ -7,6 +7,7 @@ import { provideHttpClientTesting, } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { DaffCart, @@ -19,7 +20,7 @@ import { import { DaffInMemoryCartCouponService } from './cart-coupon.service'; -describe('Driver | In Memory | Cart | CartCouponService', () => { +describe('@daffodil/cart/driver/in-memory | CartCouponService', () => { let service: DaffInMemoryCartCouponService; let httpMock: HttpTestingController; let cartFactory: DaffCartFactory; @@ -34,6 +35,12 @@ describe('Driver | In Memory | Cart | CartCouponService', () => { imports: [], providers: [ DaffInMemoryCartCouponService, + { + provide: InMemoryBackendConfig, + useValue: { + apiBase: 'api', + }, + }, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting(), ], @@ -66,7 +73,7 @@ describe('Driver | In Memory | Cart | CartCouponService', () => { done(); }); - const req = httpMock.expectOne(`${service.url}/${cartId}/`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/`); expect(req.request.method).toBe('GET'); req.flush(mockCart.coupons); @@ -80,7 +87,7 @@ describe('Driver | In Memory | Cart | CartCouponService', () => { done(); }); - const req = httpMock.expectOne(`${service.url}/${cartId}/`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/`); expect(req.request.method).toBe('POST'); expect(req.request.body).toEqual(mockCartCoupon); @@ -96,7 +103,7 @@ describe('Driver | In Memory | Cart | CartCouponService', () => { done(); }); - const req = httpMock.expectOne(`${service.url}/${cartId}/${mockCartCoupon.code}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/${mockCartCoupon.code}`); expect(req.request.method).toBe('DELETE'); @@ -113,7 +120,7 @@ describe('Driver | In Memory | Cart | CartCouponService', () => { done(); }); - const req = httpMock.expectOne(`${service.url}/${cartId}/`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/`); expect(req.request.method).toBe('DELETE'); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-coupon/cart-coupon.service.ts b/libs/cart/driver/in-memory/src/drivers/cart-coupon/cart-coupon.service.ts index 16f93f3571..4b396f4e87 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-coupon/cart-coupon.service.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-coupon/cart-coupon.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { Observable } from 'rxjs'; import { @@ -7,6 +8,9 @@ import { DaffCartCoupon, } from '@daffodil/cart'; import { DaffCartCouponServiceInterface } from '@daffodil/cart/driver'; +import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_COUPON_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -14,13 +18,13 @@ import { DaffCartCouponServiceInterface } from '@daffodil/cart/driver'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryCartCouponService implements DaffCartCouponServiceInterface { - /** - * The URL with which the driver makes calls to the backend. - */ - readonly url = '/api/cart-coupon'; - - constructor(private http: HttpClient) {} +export class DaffInMemoryCartCouponService extends DaffInMemoryDriverBase implements DaffCartCouponServiceInterface { + constructor( + private http: HttpClient, + config: InMemoryBackendConfig, + ) { + super(config, DAFF_CART_IN_MEMORY_CART_COUPON_COLLECTION_NAME); + } list(cartId: DaffCart['id']): Observable { return this.http.get(`${this.url}/${cartId}/`); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-driver.module.ts b/libs/cart/driver/in-memory/src/drivers/cart-driver.module.ts index 827b1a3c63..f50a042304 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-driver.module.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-driver.module.ts @@ -17,6 +17,7 @@ import { DaffCartCouponDriver, DaffCartAddressDriver, } from '@daffodil/cart/driver'; +import { provideDaffInMemoryBackends } from '@daffodil/driver/in-memory'; import { DaffInMemoryCartService } from './cart/cart.service'; import { DaffInMemoryCartAddressService } from './cart-address/cart-address.service'; @@ -29,6 +30,7 @@ import { DaffInMemoryCartPaymentMethodsService } from './cart-payment-methods/ca import { DaffInMemoryCartShippingAddressService } from './cart-shipping-address/cart-shipping-address.service'; import { DaffInMemoryCartShippingInformationService } from './cart-shipping-information/cart-shipping-information.service'; import { DaffInMemoryCartShippingMethodsService } from './cart-shipping-methods/cart-shipping-methods.service'; +import { DaffInMemoryBackendCartRootService } from '../backend/public_api'; @NgModule({ imports: [ @@ -84,6 +86,9 @@ export class DaffCartInMemoryDriverModule { provide: DaffCartCouponDriver, useExisting: DaffInMemoryCartCouponService, }, + provideDaffInMemoryBackends( + DaffInMemoryBackendCartRootService, + ), ], }; } diff --git a/libs/cart/driver/in-memory/src/drivers/cart-item/cart-item.service.spec.ts b/libs/cart/driver/in-memory/src/drivers/cart-item/cart-item.service.spec.ts index ab973623b9..f5a7198aa8 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-item/cart-item.service.spec.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-item/cart-item.service.spec.ts @@ -7,6 +7,7 @@ import { provideHttpClientTesting, } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { DaffCart, @@ -21,8 +22,8 @@ import { import { DaffInMemoryCartItemService } from './cart-item.service'; -describe('Driver | In Memory | Cart | CartItemService', () => { - let cartItemService: DaffInMemoryCartItemService; +describe('@daffodil/cart/driver/in-memory | CartItemService', () => { + let service: DaffInMemoryCartItemService; let httpMock: HttpTestingController; let cartFactory: DaffCartFactory; let cartItemFactory: DaffCartItemFactory; @@ -37,13 +38,19 @@ describe('Driver | In Memory | Cart | CartItemService', () => { imports: [], providers: [ DaffInMemoryCartItemService, + { + provide: InMemoryBackendConfig, + useValue: { + apiBase: 'api', + }, + }, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting(), ], }); httpMock = TestBed.inject(HttpTestingController); - cartItemService = TestBed.inject(DaffInMemoryCartItemService); + service = TestBed.inject(DaffInMemoryCartItemService); cartFactory = TestBed.inject(DaffCartFactory); cartItemFactory = TestBed.inject(DaffCartItemFactory); @@ -60,16 +67,16 @@ describe('Driver | In Memory | Cart | CartItemService', () => { }); it('should be created', () => { - expect(cartItemService).toBeTruthy(); + expect(service).toBeTruthy(); }); describe('list | getting all the cart items', () => { it('should send a get request', done => { - cartItemService.list(cartId).subscribe(res => { + service.list(cartId).subscribe(res => { done(); }); - const req = httpMock.expectOne(`${cartItemService.url}/${cartId}/`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/`); expect(req.request.method).toBe('GET'); req.flush(mockCart.items); @@ -78,11 +85,11 @@ describe('Driver | In Memory | Cart | CartItemService', () => { describe('get | getting a cart item', () => { it('should send a get request with the item id', done => { - cartItemService.get(cartId, itemId).subscribe(res => { + service.get(cartId, itemId).subscribe(res => { done(); }); - const req = httpMock.expectOne(`${cartItemService.url}/${cartId}/${itemId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/${itemId}`); expect(req.request.method).toBe('GET'); req.flush(mockCartItem); @@ -107,11 +114,11 @@ describe('Driver | In Memory | Cart | CartItemService', () => { }); it('should send a post request', done => { - cartItemService.add(cartId, cartItemInput).subscribe(cart => { + service.add(cartId, cartItemInput).subscribe(cart => { done(); }); - const req = httpMock.expectOne(`${cartItemService.url}/${cartId}/`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/`); expect(req.request.method).toBe('POST'); expect(req.request.body).toEqual(cartItemInput); @@ -130,11 +137,11 @@ describe('Driver | In Memory | Cart | CartItemService', () => { }); it('should send a put request', done => { - cartItemService.update(cartId, itemId, newCartItem).subscribe(cart => { + service.update(cartId, itemId, newCartItem).subscribe(cart => { done(); }); - const req = httpMock.expectOne(`${cartItemService.url}/${cartId}/${itemId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/${itemId}`); expect(req.request.method).toBe('PUT'); expect(req.request.body).toEqual(newCartItem); @@ -145,11 +152,11 @@ describe('Driver | In Memory | Cart | CartItemService', () => { describe('delete | removing an item from the cart', () => { it('should send a delete request', done => { - cartItemService.delete(cartId, itemId).subscribe(result => { + service.delete(cartId, itemId).subscribe(result => { done(); }); - const req = httpMock.expectOne(`${cartItemService.url}/${cartId}/${itemId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/${itemId}`); expect(req.request.method).toBe('DELETE'); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-item/cart-item.service.ts b/libs/cart/driver/in-memory/src/drivers/cart-item/cart-item.service.ts index 48a15f9712..7cdf06973e 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-item/cart-item.service.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-item/cart-item.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { Observable } from 'rxjs'; import { @@ -8,6 +9,9 @@ import { DaffCart, } from '@daffodil/cart'; import { DaffCartItemServiceInterface } from '@daffodil/cart/driver'; +import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_ITEMS_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -15,13 +19,13 @@ import { DaffCartItemServiceInterface } from '@daffodil/cart/driver'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryCartItemService implements DaffCartItemServiceInterface { - /** - * The URL with which the driver makes calls to the backend. - */ - readonly url = '/api/cart-items'; - - constructor(private http: HttpClient) {} +export class DaffInMemoryCartItemService extends DaffInMemoryDriverBase implements DaffCartItemServiceInterface { + constructor( + private http: HttpClient, + config: InMemoryBackendConfig, + ) { + super(config, DAFF_CART_IN_MEMORY_CART_ITEMS_COLLECTION_NAME); + } list(cartId: DaffCart['id']): Observable { return this.http.get(`${this.url}/${cartId}/`); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-order/cart-order.service.spec.ts b/libs/cart/driver/in-memory/src/drivers/cart-order/cart-order.service.spec.ts index c5224819d5..f1814cf59a 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-order/cart-order.service.spec.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-order/cart-order.service.spec.ts @@ -7,6 +7,7 @@ import { provideHttpClientTesting, } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { DaffCart, @@ -20,7 +21,7 @@ import { import { DaffInMemoryCartOrderService } from './cart-order.service'; -describe('Driver | In Memory | Cart | CartOrderService', () => { +describe('@daffodil/cart/driver/in-memory | CartOrderService', () => { let service: DaffInMemoryCartOrderService; let httpMock: HttpTestingController; let cartFactory: DaffCartFactory; @@ -36,6 +37,12 @@ describe('Driver | In Memory | Cart | CartOrderService', () => { imports: [], providers: [ DaffInMemoryCartOrderService, + { + provide: InMemoryBackendConfig, + useValue: { + apiBase: 'api', + }, + }, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting(), ], @@ -72,7 +79,7 @@ describe('Driver | In Memory | Cart | CartOrderService', () => { done(); }); - const req = httpMock.expectOne(`${service.url}/${cartId}/`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/`); expect(req.request.method).toBe('POST'); req.flush(mockCartOrderResult); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-order/cart-order.service.ts b/libs/cart/driver/in-memory/src/drivers/cart-order/cart-order.service.ts index ffcdb7f102..657ea322a4 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-order/cart-order.service.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-order/cart-order.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { Observable } from 'rxjs'; import { @@ -8,6 +9,9 @@ import { DaffCartOrderResult, } from '@daffodil/cart'; import { DaffCartOrderServiceInterface } from '@daffodil/cart/driver'; +import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_ORDER_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -15,13 +19,13 @@ import { DaffCartOrderServiceInterface } from '@daffodil/cart/driver'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryCartOrderService implements DaffCartOrderServiceInterface { - /** - * The URL with which the driver makes calls to the backend. - */ - readonly url = '/api/cart-order'; - - constructor(private http: HttpClient) {} +export class DaffInMemoryCartOrderService extends DaffInMemoryDriverBase implements DaffCartOrderServiceInterface { + constructor( + private http: HttpClient, + config: InMemoryBackendConfig, + ) { + super(config, DAFF_CART_IN_MEMORY_CART_ORDER_COLLECTION_NAME); + } placeOrder(cartId: DaffCart['id'], payment?: DaffCartPaymentMethod): Observable { return this.http.post(`${this.url}/${cartId}/`, { payment }); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-payment-methods/cart-payment-methods.service.spec.ts b/libs/cart/driver/in-memory/src/drivers/cart-payment-methods/cart-payment-methods.service.spec.ts index 5faa87b70f..cce721c14c 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-payment-methods/cart-payment-methods.service.spec.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-payment-methods/cart-payment-methods.service.spec.ts @@ -7,6 +7,7 @@ import { provideHttpClientTesting, } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { DaffCart, @@ -19,8 +20,8 @@ import { import { DaffInMemoryCartPaymentMethodsService } from './cart-payment-methods.service'; -describe('Driver | In Memory | Cart | CartPaymentMethodsService', () => { - let cartPaymentMethodsService: DaffInMemoryCartPaymentMethodsService; +describe('@daffodil/cart/driver/in-memory | CartPaymentMethodsService', () => { + let service: DaffInMemoryCartPaymentMethodsService; let httpMock: HttpTestingController; let cartFactory: DaffCartFactory; let paymentFactory: DaffCartPaymentFactory; @@ -34,13 +35,19 @@ describe('Driver | In Memory | Cart | CartPaymentMethodsService', () => { imports: [], providers: [ DaffInMemoryCartPaymentMethodsService, + { + provide: InMemoryBackendConfig, + useValue: { + apiBase: 'api', + }, + }, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting(), ], }); httpMock = TestBed.inject(HttpTestingController); - cartPaymentMethodsService = TestBed.inject(DaffInMemoryCartPaymentMethodsService); + service = TestBed.inject(DaffInMemoryCartPaymentMethodsService); cartFactory = TestBed.inject(DaffCartFactory); paymentFactory = TestBed.inject(DaffCartPaymentFactory); @@ -55,16 +62,16 @@ describe('Driver | In Memory | Cart | CartPaymentMethodsService', () => { }); it('should be created', () => { - expect(cartPaymentMethodsService).toBeTruthy(); + expect(service).toBeTruthy(); }); describe('list | list a cart\'s payment methods', () => { it('should send a get request', done => { - cartPaymentMethodsService.list(cartId).subscribe(res => { + service.list(cartId).subscribe(res => { done(); }); - const req = httpMock.expectOne(`${cartPaymentMethodsService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('GET'); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-payment-methods/cart-payment-methods.service.ts b/libs/cart/driver/in-memory/src/drivers/cart-payment-methods/cart-payment-methods.service.ts index 20adf73d11..5b755e9983 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-payment-methods/cart-payment-methods.service.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-payment-methods/cart-payment-methods.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { Observable } from 'rxjs'; import { @@ -7,6 +8,9 @@ import { DaffCart, } from '@daffodil/cart'; import { DaffCartPaymentMethodsServiceInterface } from '@daffodil/cart/driver'; +import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_PAYMENT_METHODS_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -14,13 +18,13 @@ import { DaffCartPaymentMethodsServiceInterface } from '@daffodil/cart/driver'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryCartPaymentMethodsService implements DaffCartPaymentMethodsServiceInterface { - /** - * The URL with which the driver makes calls to the backend. - */ - readonly url = '/api/cart-payment-methods'; - - constructor(private http: HttpClient) {} +export class DaffInMemoryCartPaymentMethodsService extends DaffInMemoryDriverBase implements DaffCartPaymentMethodsServiceInterface { + constructor( + private http: HttpClient, + config: InMemoryBackendConfig, + ) { + super(config, DAFF_CART_IN_MEMORY_CART_PAYMENT_METHODS_COLLECTION_NAME); + } list(cartId: DaffCart['id']): Observable { return this.http.get(`${this.url}/${cartId}`); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-payment/cart-payment.service.spec.ts b/libs/cart/driver/in-memory/src/drivers/cart-payment/cart-payment.service.spec.ts index 768c1855c3..20bf4e10a1 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-payment/cart-payment.service.spec.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-payment/cart-payment.service.spec.ts @@ -7,6 +7,7 @@ import { provideHttpClientTesting, } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { DaffCart, @@ -21,8 +22,8 @@ import { import { DaffInMemoryCartPaymentService } from './cart-payment.service'; -describe('Driver | In Memory | Cart | CartPaymentService', () => { - let cartPaymentService: DaffInMemoryCartPaymentService; +describe('@daffodil/cart/driver/in-memory | CartPaymentService', () => { + let service: DaffInMemoryCartPaymentService; let httpMock: HttpTestingController; let cartFactory: DaffCartFactory; let cartPaymentFactory: DaffCartPaymentFactory; @@ -38,13 +39,19 @@ describe('Driver | In Memory | Cart | CartPaymentService', () => { imports: [], providers: [ DaffInMemoryCartPaymentService, + { + provide: InMemoryBackendConfig, + useValue: { + apiBase: 'api', + }, + }, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting(), ], }); httpMock = TestBed.inject(HttpTestingController); - cartPaymentService = TestBed.inject(DaffInMemoryCartPaymentService); + service = TestBed.inject(DaffInMemoryCartPaymentService); cartFactory = TestBed.inject(DaffCartFactory); cartPaymentFactory = TestBed.inject(DaffCartPaymentFactory); @@ -63,16 +70,16 @@ describe('Driver | In Memory | Cart | CartPaymentService', () => { }); it('should be created', () => { - expect(cartPaymentService).toBeTruthy(); + expect(service).toBeTruthy(); }); describe('get | getting a cart payment method', () => { it('should send a get request', done => { - cartPaymentService.get(cartId).subscribe(res => { + service.get(cartId).subscribe(res => { done(); }); - const req = httpMock.expectOne(`${cartPaymentService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('GET'); req.flush(mockPayment); @@ -85,11 +92,11 @@ describe('Driver | In Memory | Cart | CartPaymentService', () => { }); it('should send a put request', done => { - cartPaymentService.update(cartId, mockPayment).subscribe(cart => { + service.update(cartId, mockPayment).subscribe(cart => { done(); }); - const req = httpMock.expectOne(`${cartPaymentService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('PUT'); expect(req.request.body).toEqual({ payment: mockPayment }); @@ -107,11 +114,11 @@ describe('Driver | In Memory | Cart | CartPaymentService', () => { }); it('should send a put request', done => { - cartPaymentService.updateWithBilling(cartId, mockPayment, mockCartAddress).subscribe(cart => { + service.updateWithBilling(cartId, mockPayment, mockCartAddress).subscribe(cart => { done(); }); - const req = httpMock.expectOne(`${cartPaymentService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('PUT'); expect(req.request.body).toEqual({ @@ -128,9 +135,9 @@ describe('Driver | In Memory | Cart | CartPaymentService', () => { describe('remove | removing the payment method from the cart', () => { it('should send a delete request', () => { - cartPaymentService.remove(cartId).subscribe(); + service.remove(cartId).subscribe(); - const req = httpMock.expectOne(`${cartPaymentService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('DELETE'); }); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-payment/cart-payment.service.ts b/libs/cart/driver/in-memory/src/drivers/cart-payment/cart-payment.service.ts index d0ec9981fb..3c38bd9714 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-payment/cart-payment.service.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-payment/cart-payment.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { Observable } from 'rxjs'; import { @@ -8,6 +9,9 @@ import { DaffCartAddress, } from '@daffodil/cart'; import { DaffCartPaymentServiceInterface } from '@daffodil/cart/driver'; +import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_PAYMENT_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -15,13 +19,13 @@ import { DaffCartPaymentServiceInterface } from '@daffodil/cart/driver'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryCartPaymentService implements DaffCartPaymentServiceInterface { - /** - * The URL with which the driver makes calls to the backend. - */ - readonly url = '/api/cart-payment'; - - constructor(private http: HttpClient) {} +export class DaffInMemoryCartPaymentService extends DaffInMemoryDriverBase implements DaffCartPaymentServiceInterface { + constructor( + private http: HttpClient, + config: InMemoryBackendConfig, + ) { + super(config, DAFF_CART_IN_MEMORY_CART_PAYMENT_COLLECTION_NAME); + } get(cartId: DaffCart['id']): Observable { return this.http.get(`${this.url}/${cartId}`); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-shipping-address/cart-shipping-address.service.spec.ts b/libs/cart/driver/in-memory/src/drivers/cart-shipping-address/cart-shipping-address.service.spec.ts index b6ae4dfdc9..d1bd07cd9b 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-shipping-address/cart-shipping-address.service.spec.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-shipping-address/cart-shipping-address.service.spec.ts @@ -7,6 +7,7 @@ import { provideHttpClientTesting, } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { DaffCart, @@ -19,8 +20,8 @@ import { import { DaffInMemoryCartShippingAddressService } from './cart-shipping-address.service'; -describe('Driver | In Memory | Cart | CartShippingAddressService', () => { - let cartShippingAddressService: DaffInMemoryCartShippingAddressService; +describe('@daffodil/cart/driver/in-memory | CartShippingAddressService', () => { + let service: DaffInMemoryCartShippingAddressService; let httpMock: HttpTestingController; let cartFactory: DaffCartFactory; let cartAddressFactory: DaffCartAddressFactory; @@ -34,6 +35,12 @@ describe('Driver | In Memory | Cart | CartShippingAddressService', () => { imports: [], providers: [ DaffInMemoryCartShippingAddressService, + { + provide: InMemoryBackendConfig, + useValue: { + apiBase: 'api', + }, + }, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting(), ], @@ -42,7 +49,7 @@ describe('Driver | In Memory | Cart | CartShippingAddressService', () => { httpMock = TestBed.inject(HttpTestingController); cartFactory = TestBed.inject(DaffCartFactory); cartAddressFactory = TestBed.inject(DaffCartAddressFactory); - cartShippingAddressService = TestBed.inject(DaffInMemoryCartShippingAddressService); + service = TestBed.inject(DaffInMemoryCartShippingAddressService); mockCart = cartFactory.create(); mockCartAddress = cartAddressFactory.create(); @@ -55,16 +62,16 @@ describe('Driver | In Memory | Cart | CartShippingAddressService', () => { }); it('should be created', () => { - expect(cartShippingAddressService).toBeTruthy(); + expect(service).toBeTruthy(); }); describe('get | getting a cart shipping address', () => { it('should send a get request', done => { - cartShippingAddressService.get(cartId).subscribe(res => { + service.get(cartId).subscribe(res => { done(); }); - const req = httpMock.expectOne(`${cartShippingAddressService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('GET'); req.flush(mockCartAddress); @@ -80,11 +87,11 @@ describe('Driver | In Memory | Cart | CartShippingAddressService', () => { }); it('should send a put request', done => { - cartShippingAddressService.update(cartId, mockCartAddressUpdate).subscribe(cart => { + service.update(cartId, mockCartAddressUpdate).subscribe(cart => { done(); }); - const req = httpMock.expectOne(`${cartShippingAddressService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('PUT'); expect(req.request.body).toEqual(mockCartAddressUpdate); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-shipping-address/cart-shipping-address.service.ts b/libs/cart/driver/in-memory/src/drivers/cart-shipping-address/cart-shipping-address.service.ts index 929b7173cc..a12735048f 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-shipping-address/cart-shipping-address.service.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-shipping-address/cart-shipping-address.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { Observable } from 'rxjs'; import { @@ -7,6 +8,9 @@ import { DaffCart, } from '@daffodil/cart'; import { DaffCartShippingAddressServiceInterface } from '@daffodil/cart/driver'; +import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_SHIPPING_ADDRESS_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -14,13 +18,13 @@ import { DaffCartShippingAddressServiceInterface } from '@daffodil/cart/driver'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryCartShippingAddressService implements DaffCartShippingAddressServiceInterface { - /** - * The URL with which the driver makes calls to the backend. - */ - readonly url = '/api/cart-shipping-address'; - - constructor(private http: HttpClient) {} +export class DaffInMemoryCartShippingAddressService extends DaffInMemoryDriverBase implements DaffCartShippingAddressServiceInterface { + constructor( + private http: HttpClient, + config: InMemoryBackendConfig, + ) { + super(config, DAFF_CART_IN_MEMORY_CART_SHIPPING_ADDRESS_COLLECTION_NAME); + } get(cartId: DaffCart['id']): Observable { return this.http.get(`${this.url}/${cartId}`); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-shipping-information/cart-shipping-information.service.spec.ts b/libs/cart/driver/in-memory/src/drivers/cart-shipping-information/cart-shipping-information.service.spec.ts index a3a5ffef82..169e90ffc4 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-shipping-information/cart-shipping-information.service.spec.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-shipping-information/cart-shipping-information.service.spec.ts @@ -7,6 +7,7 @@ import { provideHttpClientTesting, } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { DaffCart, @@ -19,8 +20,8 @@ import { import { DaffInMemoryCartShippingInformationService } from './cart-shipping-information.service'; -describe('Driver | In Memory | Cart | CartShippingInformationService', () => { - let cartShippingInformationService: DaffInMemoryCartShippingInformationService; +describe('@daffodil/cart/driver/in-memory | CartShippingInformationService', () => { + let service: DaffInMemoryCartShippingInformationService; let httpMock: HttpTestingController; let cartFactory: DaffCartFactory; let cartShippingRateFactory: DaffCartShippingRateFactory; @@ -34,13 +35,19 @@ describe('Driver | In Memory | Cart | CartShippingInformationService', () => { imports: [], providers: [ DaffInMemoryCartShippingInformationService, + { + provide: InMemoryBackendConfig, + useValue: { + apiBase: 'api', + }, + }, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting(), ], }); httpMock = TestBed.inject(HttpTestingController); - cartShippingInformationService = TestBed.inject(DaffInMemoryCartShippingInformationService); + service = TestBed.inject(DaffInMemoryCartShippingInformationService); cartFactory = TestBed.inject(DaffCartFactory); cartShippingRateFactory = TestBed.inject(DaffCartShippingRateFactory); @@ -56,16 +63,16 @@ describe('Driver | In Memory | Cart | CartShippingInformationService', () => { }); it('should be created', () => { - expect(cartShippingInformationService).toBeTruthy(); + expect(service).toBeTruthy(); }); describe('get | getting a cart\'s shipping info', () => { it('should send a get request', done => { - cartShippingInformationService.get(cartId).subscribe(cart => { + service.get(cartId).subscribe(cart => { done(); }); - const req = httpMock.expectOne(`${cartShippingInformationService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('GET'); @@ -78,11 +85,11 @@ describe('Driver | In Memory | Cart | CartShippingInformationService', () => { const info: Partial = { price: newPrice }; it('should send a put request', done => { - cartShippingInformationService.update(cartId, info).subscribe(res => { + service.update(cartId, info).subscribe(res => { done(); }); - const req = httpMock.expectOne(`${cartShippingInformationService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('PUT'); expect(req.request.body).toEqual(info); @@ -95,11 +102,11 @@ describe('Driver | In Memory | Cart | CartShippingInformationService', () => { describe('delete | deleting the selected shipping method', () => { it('should send a delete request', done => { - cartShippingInformationService.delete(cartId).subscribe(result => { + service.delete(cartId).subscribe(result => { done(); }); - const req = httpMock.expectOne(`${cartShippingInformationService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('DELETE'); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-shipping-information/cart-shipping-information.service.ts b/libs/cart/driver/in-memory/src/drivers/cart-shipping-information/cart-shipping-information.service.ts index 9be63e08a9..d5312beb20 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-shipping-information/cart-shipping-information.service.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-shipping-information/cart-shipping-information.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { Observable } from 'rxjs'; import { @@ -7,6 +8,9 @@ import { DaffCart, } from '@daffodil/cart'; import { DaffCartShippingInformationServiceInterface } from '@daffodil/cart/driver'; +import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_SHIPPING_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -14,13 +18,13 @@ import { DaffCartShippingInformationServiceInterface } from '@daffodil/cart/driv @Injectable({ providedIn: 'root', }) -export class DaffInMemoryCartShippingInformationService implements DaffCartShippingInformationServiceInterface { - /** - * The URL with which the driver makes calls to the backend. - */ - readonly url = '/api/cart-shipping-information'; - - constructor(private http: HttpClient) {} +export class DaffInMemoryCartShippingInformationService extends DaffInMemoryDriverBase implements DaffCartShippingInformationServiceInterface { + constructor( + private http: HttpClient, + config: InMemoryBackendConfig, + ) { + super(config, DAFF_CART_IN_MEMORY_CART_SHIPPING_COLLECTION_NAME); + } get(cartId: DaffCart['id']): Observable { return this.http.get(`${this.url}/${cartId}`); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-shipping-methods/cart-shipping-methods.service.spec.ts b/libs/cart/driver/in-memory/src/drivers/cart-shipping-methods/cart-shipping-methods.service.spec.ts index 1b6fe14e39..647d443b54 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-shipping-methods/cart-shipping-methods.service.spec.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-shipping-methods/cart-shipping-methods.service.spec.ts @@ -7,6 +7,7 @@ import { provideHttpClientTesting, } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { DaffCart, @@ -19,8 +20,8 @@ import { import { DaffInMemoryCartShippingMethodsService } from './cart-shipping-methods.service'; -describe('Driver | In Memory | Cart | CartShippingMethodsService', () => { - let cartPaymentMethodsService: DaffInMemoryCartShippingMethodsService; +describe('@daffodil/cart/driver/in-memory | CartShippingMethodsService', () => { + let service: DaffInMemoryCartShippingMethodsService; let httpMock: HttpTestingController; let cartFactory: DaffCartFactory; let shippingRateFactory: DaffCartShippingRateFactory; @@ -34,13 +35,19 @@ describe('Driver | In Memory | Cart | CartShippingMethodsService', () => { imports: [], providers: [ DaffInMemoryCartShippingMethodsService, + { + provide: InMemoryBackendConfig, + useValue: { + apiBase: 'api', + }, + }, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting(), ], }); httpMock = TestBed.inject(HttpTestingController); - cartPaymentMethodsService = TestBed.inject(DaffInMemoryCartShippingMethodsService); + service = TestBed.inject(DaffInMemoryCartShippingMethodsService); cartFactory = TestBed.inject(DaffCartFactory); shippingRateFactory = TestBed.inject(DaffCartShippingRateFactory); @@ -55,16 +62,16 @@ describe('Driver | In Memory | Cart | CartShippingMethodsService', () => { }); it('should be created', () => { - expect(cartPaymentMethodsService).toBeTruthy(); + expect(service).toBeTruthy(); }); describe('list | list a cart\'s payment methods', () => { it('should send a get request', done => { - cartPaymentMethodsService.list(cartId).subscribe(res => { + service.list(cartId).subscribe(res => { done(); }); - const req = httpMock.expectOne(`${cartPaymentMethodsService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('GET'); diff --git a/libs/cart/driver/in-memory/src/drivers/cart-shipping-methods/cart-shipping-methods.service.ts b/libs/cart/driver/in-memory/src/drivers/cart-shipping-methods/cart-shipping-methods.service.ts index 6d0713bf56..f77d2f31cf 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart-shipping-methods/cart-shipping-methods.service.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart-shipping-methods/cart-shipping-methods.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { Observable } from 'rxjs'; import { @@ -7,6 +8,9 @@ import { DaffCart, } from '@daffodil/cart'; import { DaffCartShippingMethodsServiceInterface } from '@daffodil/cart/driver'; +import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_SHIPPING_METHODS_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -14,13 +18,13 @@ import { DaffCartShippingMethodsServiceInterface } from '@daffodil/cart/driver'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryCartShippingMethodsService implements DaffCartShippingMethodsServiceInterface { - /** - * The URL with which the driver makes calls to the backend. - */ - readonly url = '/api/cart-shipping-methods'; - - constructor(private http: HttpClient) {} +export class DaffInMemoryCartShippingMethodsService extends DaffInMemoryDriverBase implements DaffCartShippingMethodsServiceInterface { + constructor( + private http: HttpClient, + config: InMemoryBackendConfig, + ) { + super(config, DAFF_CART_IN_MEMORY_CART_SHIPPING_METHODS_COLLECTION_NAME); + } list(cartId: DaffCart['id']): Observable { return this.http.get(`${this.url}/${cartId}`); diff --git a/libs/cart/driver/in-memory/src/drivers/cart/cart.service.spec.ts b/libs/cart/driver/in-memory/src/drivers/cart/cart.service.spec.ts index 7ab39c4276..aad10b2757 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart/cart.service.spec.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart/cart.service.spec.ts @@ -7,6 +7,7 @@ import { provideHttpClientTesting, } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { of } from 'rxjs'; import { catchError } from 'rxjs/operators'; @@ -17,7 +18,7 @@ import { DaffCartFactory } from '@daffodil/cart/testing'; import { DaffInMemoryCartService } from './cart.service'; describe('@daffodil/cart/driver/in-memory | DaffInMemoryCartService', () => { - let cartService: DaffInMemoryCartService; + let service: DaffInMemoryCartService; let httpMock: HttpTestingController; let cartFactory: DaffCartFactory; @@ -29,6 +30,12 @@ describe('@daffodil/cart/driver/in-memory | DaffInMemoryCartService', () => { imports: [], providers: [ DaffInMemoryCartService, + { + provide: InMemoryBackendConfig, + useValue: { + apiBase: 'api', + }, + }, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting(), ], @@ -36,7 +43,7 @@ describe('@daffodil/cart/driver/in-memory | DaffInMemoryCartService', () => { httpMock = TestBed.inject(HttpTestingController); cartFactory = TestBed.inject(DaffCartFactory); - cartService = TestBed.inject(DaffInMemoryCartService); + service = TestBed.inject(DaffInMemoryCartService); mockCart = cartFactory.create(); cartId = mockCart.id; @@ -47,24 +54,24 @@ describe('@daffodil/cart/driver/in-memory | DaffInMemoryCartService', () => { }); it('should be created', () => { - expect(cartService).toBeTruthy(); + expect(service).toBeTruthy(); }); describe('get | getting a cart', () => { it('should send a get request and return the cart in the response', done => { - cartService.get(cartId).subscribe(({ response }) => { + service.get(cartId).subscribe(({ response }) => { expect(response).toEqual(mockCart); done(); }); - const req = httpMock.expectOne(`${cartService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('GET'); req.flush(mockCart); }); it('should throw a daffodil error when it receives an error', (done) => { - cartService.get(cartId).pipe( + service.get(cartId).pipe( catchError((error) => { expect(error).toEqual(new DaffCartNotFoundError(error.message)); done(); @@ -72,7 +79,7 @@ describe('@daffodil/cart/driver/in-memory | DaffInMemoryCartService', () => { }), ).subscribe(); - const req = httpMock.expectOne(`${cartService.url}/${cartId}`); + const req = httpMock.expectOne(`${service['url']}/${cartId}`); expect(req.request.method).toBe('GET'); req.error(new ErrorEvent('404')); @@ -90,12 +97,12 @@ describe('@daffodil/cart/driver/in-memory | DaffInMemoryCartService', () => { describe('a successful addToCart request', () => { it('should send a post request to `api/cart/addToCart` and respond with a cart', done => { - cartService.addToCart(productId, qty).subscribe(cart => { + service.addToCart(productId, qty).subscribe(cart => { expect(cart).toEqual(mockCart); done(); }); - const req = httpMock.expectOne(`${cartService.url}/addToCart`); + const req = httpMock.expectOne(`${service['url']}/addToCart`); expect(req.request.method).toBe('POST'); expect(req.request.body).toEqual({ @@ -110,12 +117,12 @@ describe('@daffodil/cart/driver/in-memory | DaffInMemoryCartService', () => { describe('clear | removing all items from the cart', () => { it('should send a post request and return the cart that has no items', done => { - cartService.clear(cartId).subscribe(result => { + service.clear(cartId).subscribe(result => { expect(result).toEqual(mockCart); done(); }); - const req = httpMock.expectOne(`${cartService.url}/${cartId}/clear`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/clear`); expect(req.request.method).toBe('POST'); @@ -127,12 +134,12 @@ describe('@daffodil/cart/driver/in-memory | DaffInMemoryCartService', () => { describe('merge', () => { it('should send a post request and return the cart', done => { - cartService.merge(cartId).subscribe(result => { + service.merge(cartId).subscribe(result => { expect(result.response).toEqual(mockCart); done(); }); - const req = httpMock.expectOne(`${cartService.url}/${cartId}/merge`); + const req = httpMock.expectOne(`${service['url']}/${cartId}/merge`); expect(req.request.method).toBe('POST'); @@ -142,12 +149,12 @@ describe('@daffodil/cart/driver/in-memory | DaffInMemoryCartService', () => { describe('create | creating a cart', () => { it('should send a post request and return the cart', done => { - cartService.create().subscribe(result => { + service.create().subscribe(result => { expect(result).toEqual(jasmine.objectContaining({ id: cartId })); done(); }); - const req = httpMock.expectOne(`${cartService.url}`); + const req = httpMock.expectOne(`${service['url']}`); expect(req.request.method).toBe('POST'); diff --git a/libs/cart/driver/in-memory/src/drivers/cart/cart.service.ts b/libs/cart/driver/in-memory/src/drivers/cart/cart.service.ts index 9edcef6d01..371f745155 100644 --- a/libs/cart/driver/in-memory/src/drivers/cart/cart.service.ts +++ b/libs/cart/driver/in-memory/src/drivers/cart/cart.service.ts @@ -1,8 +1,8 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { InMemoryBackendConfig } from 'angular-in-memory-web-api'; import { Observable, - of, throwError, } from 'rxjs'; import { @@ -16,6 +16,9 @@ import { DaffCartNotFoundError, } from '@daffodil/cart/driver'; import { DaffDriverResponse } from '@daffodil/driver'; +import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory'; + +import { DAFF_CART_IN_MEMORY_CART_COLLECTION_NAME } from '../../collection-names'; /** * @inheritdoc @@ -23,13 +26,13 @@ import { DaffDriverResponse } from '@daffodil/driver'; @Injectable({ providedIn: 'root', }) -export class DaffInMemoryCartService implements DaffCartServiceInterface { - /** - * The URL with which the driver makes calls to the backend. - */ - readonly url = '/api/cart'; - - constructor(private http: HttpClient) {} +export class DaffInMemoryCartService extends DaffInMemoryDriverBase implements DaffCartServiceInterface { + constructor( + private http: HttpClient, + config: InMemoryBackendConfig, + ) { + super(config, DAFF_CART_IN_MEMORY_CART_COLLECTION_NAME); + } get(cartId: DaffCart['id']): Observable> { return this.http.get(`${this.url}/${cartId}`).pipe( diff --git a/libs/cart/driver/in-memory/src/public_api.ts b/libs/cart/driver/in-memory/src/public_api.ts index db7f55397a..7597d1db46 100644 --- a/libs/cart/driver/in-memory/src/public_api.ts +++ b/libs/cart/driver/in-memory/src/public_api.ts @@ -1,3 +1,5 @@ export * from './drivers/public_api'; export * from './backend/public_api'; export * from './injection-tokens/public_api'; + +export * from './collection-names';