Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { DAFF_CART_IN_MEMORY_CART_ORDER_COLLECTION_NAME } from '../../collection

/**
* @inheritdoc
*
* @deprecated Use {@link DaffCheckoutInMemoryBackendOrderService} instead.
*/
@Injectable({
providedIn: 'root',
Expand Down
2 changes: 2 additions & 0 deletions libs/cart/src/models/cart-order-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ID } from '@daffodil/core';
/**
* The result of placing an order for a cart.
* Stores a reference to a cart and order object.
*
* @deprecated Use {@link DaffCheckoutOrderResult} instead.
*/
export interface DaffCartOrderResult {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import {
} from '../../reducers/public_api';
import { getDaffCartFeatureSelector } from '../cart-feature.selector';

/**
* @deprecated Use {@link DaffCheckoutOrderMemoizedSelectors} instead.
*/
export interface DaffCartOrderMemoizedSelectors<
T extends DaffCart = DaffCart,
V extends DaffCartOrderResult = DaffCartOrderResult,
Expand Down Expand Up @@ -90,6 +93,9 @@ const createCartOrderSelectors = <
};
};

/**
* @deprecated Use {@link getCheckoutOrderSelectors} instead.
*/
export const getCartOrderSelectors: <
T extends DaffCart = DaffCart,
V extends DaffCartOrderResult = DaffCartOrderResult,
Expand Down
6 changes: 6 additions & 0 deletions libs/checkout/driver/in-memory/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../../../../node_modules/ng-packagr/ng-entrypoint.schema.json",
"lib": {
"entryFile": "src/index.ts"
}
}
1 change: 1 addition & 0 deletions libs/checkout/driver/in-memory/src/backend/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { DaffCheckoutInMemoryBackendRootService } from './root.service';
75 changes: 75 additions & 0 deletions libs/checkout/driver/in-memory/src/backend/root.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { TestBed } from '@angular/core/testing';

import { DaffCheckoutInMemoryBackendOrderService } from '@daffodil/checkout/driver/in-memory';
import { DaffProductTestingModule } from '@daffodil/product/testing';

import { DaffCheckoutInMemoryBackendRootService } from './root.service';
import { DAFF_CHECKOUT_IN_MEMORY_CHECKOUT_ORDER_COLLECTION_NAME } from '../collection-names.const';

describe('DaffCheckoutInMemoryBackendRootService | Unit', () => {
let service: DaffCheckoutInMemoryBackendRootService;

let orderBackendService: DaffCheckoutInMemoryBackendOrderService;

let reqInfoStub;
let baseUrl;

let orderPostSpy: jasmine.Spy;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
DaffProductTestingModule,
],
providers: [
DaffCheckoutInMemoryBackendRootService,
],
});
service = TestBed.inject(DaffCheckoutInMemoryBackendRootService);

orderBackendService = TestBed.inject(DaffCheckoutInMemoryBackendOrderService);

baseUrl = 'api/checkout';
reqInfoStub = {
id: '',
resourceUrl: baseUrl,
collection: [],
collectionName: '',
method: '',
req: {
body: {},
},
utils: {
createResponse$: func => func(),
getJsonBody: req => req.body,
findById: (ary, id) => ary.find(e => e.id === id),
},
};

orderPostSpy = spyOn(orderBackendService, 'post');
});

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

describe('processing a post request', () => {
beforeEach(() => {
reqInfoStub.method = 'post';
});

describe(`when the collectionName is ${DAFF_CHECKOUT_IN_MEMORY_CHECKOUT_ORDER_COLLECTION_NAME}`, () => {
let result;

beforeEach(() => {
reqInfoStub.collectionName = DAFF_CHECKOUT_IN_MEMORY_CHECKOUT_ORDER_COLLECTION_NAME;

result = service.post(reqInfoStub);
});

it('should delegate the request to the order service', () => {
expect(orderPostSpy).toHaveBeenCalledWith(reqInfoStub);
});
});
});
});
63 changes: 63 additions & 0 deletions libs/checkout/driver/in-memory/src/backend/root.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
Inject,
Injectable,
} from '@angular/core';
import {
InMemoryDbService,
RequestInfo,
} from 'angular-in-memory-web-api';
import {
Observable,
of,
} from 'rxjs';

import {
DAFF_IN_MEMORY_DRIVER_CONFIG,
DaffInMemoryBackendDelegate,
DaffInMemoryDriverConfig,
DaffInMemoryMultiRouteableBackend,
} from '@daffodil/driver/in-memory';

import { DAFF_CHECKOUT_IN_MEMORY_CHECKOUT_ORDER_COLLECTION_NAME } from '../collection-names.const';
import { DaffCheckoutInMemoryBackendOrderService } from '../order/backend.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 = [
DAFF_CHECKOUT_IN_MEMORY_CHECKOUT_ORDER_COLLECTION_NAME,
];

/**
* The root in-memory backend.
* Creates the database and delegates requests to child backends.
*/
@Injectable({
providedIn: 'root',
})
export class DaffCheckoutInMemoryBackendRootService extends DaffInMemoryBackendDelegate implements InMemoryDbService, DaffInMemoryMultiRouteableBackend {
constructor(
orderService: DaffCheckoutInMemoryBackendOrderService,
@Inject(DAFF_IN_MEMORY_DRIVER_CONFIG) _config: DaffInMemoryDriverConfig,
) {
super([
orderService,
], _config);
}

protected override delegateRequest(reqInfo: RequestInfo, method): Observable<any> {
return super.delegateRequest({
...reqInfo,
collection: [],
}, method);
}

canHandle(collectionName: string): boolean {
return COLLECTION_NAMES.includes(collectionName);
}

createDb(reqInfo: RequestInfo) {
return of({});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DAFF_CHECKOUT_IN_MEMORY_CHECKOUT_ORDER_COLLECTION_NAME = 'checkout-order';
1 change: 1 addition & 0 deletions libs/checkout/driver/in-memory/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public_api';
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { TestBed } from '@angular/core/testing';

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

import { DaffCheckoutInMemoryBackendOrderService } from './backend.service';
import { DAFF_CHECKOUT_IN_MEMORY_CHECKOUT_ORDER_COLLECTION_NAME } from '../collection-names.const';


describe('DaffCheckoutInMemoryBackendOrderService', () => {
let service: DaffCheckoutInMemoryBackendOrderService;

let cartId: DaffCart['id'];
let reqInfoStub;
let baseUrl;

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

cartId = 'cartId';
baseUrl = `api/${DAFF_CHECKOUT_IN_MEMORY_CHECKOUT_ORDER_COLLECTION_NAME}/`;
reqInfoStub = {
id: cartId,
resourceUrl: baseUrl,
collection: [],
req: {
body: {},
},
utils: {
createResponse$: func => func(),
getJsonBody: req => req.body,
findById: (ary, id) => ary.find(e => e.id === id),
},
};
});

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

describe('processing a place order request', () => {
let result;

beforeEach(() => {
reqInfoStub.url = baseUrl;
reqInfoStub.req.body.cartId = cartId;

result = service.post(reqInfoStub);
});

it('should return the cart order result with a defined cart and order ID', () => {
expect(result.body.orderId).toBeDefined();
expect(result.body.cartId).toBeDefined();
});
});
});
39 changes: 39 additions & 0 deletions libs/checkout/driver/in-memory/src/order/backend.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injectable } from '@angular/core';
import {
STATUS,
RequestInfo,
} from 'angular-in-memory-web-api';

import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory';

import { DAFF_CHECKOUT_IN_MEMORY_CHECKOUT_ORDER_COLLECTION_NAME } from '../collection-names.const';

/**
* @inheritdoc
*/
@Injectable({
providedIn: 'root',
})
export class DaffCheckoutInMemoryBackendOrderService implements DaffInMemorySingleRouteableBackend {
readonly collectionName = DAFF_CHECKOUT_IN_MEMORY_CHECKOUT_ORDER_COLLECTION_NAME;

post(reqInfo: RequestInfo) {
return this.placeOrder(reqInfo);
}

private placeOrder(reqInfo: RequestInfo) {
const { cartId } = reqInfo.utils.getJsonBody(reqInfo.req);
return reqInfo.utils.createResponse$(() => cartId
? {
body: {
cartId,
orderId: '8235422034',
},
status: STATUS.OK,
}
: {
status: STATUS.NOT_FOUND,
},
);
}
}
78 changes: 78 additions & 0 deletions libs/checkout/driver/in-memory/src/order/driver.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
provideHttpClient,
withInterceptorsFromDi,
} from '@angular/common/http';
import {
HttpTestingController,
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';

import { DaffCart } from '@daffodil/cart';
import { DaffCartFactory } from '@daffodil/cart/testing';
import { DaffCheckoutOrderResult } from '@daffodil/checkout';

import { DaffInMemoryCheckoutOrderService } from './driver.service';

describe('@daffodil/checkout/driver/in-memory | CheckoutOrderService', () => {
let service: DaffInMemoryCheckoutOrderService;
let httpMock: HttpTestingController;
let cartFactory: DaffCartFactory;

let mockCart: DaffCart;
let mockCheckoutOrderResult: DaffCheckoutOrderResult;
let cartId;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [],
providers: [
DaffInMemoryCheckoutOrderService,
{
provide: InMemoryBackendConfig,
useValue: {
apiBase: 'api',
},
},
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
});

httpMock = TestBed.inject(HttpTestingController);
service = TestBed.inject(DaffInMemoryCheckoutOrderService);

cartFactory = TestBed.inject(DaffCartFactory);

mockCart = cartFactory.create();
mockCheckoutOrderResult = {
orderId: 'orderId',
cartId: 'cartId',
};
cartId = mockCart.id;
});

afterEach(() => {
httpMock.verify();
});

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

describe('placeOrder | placing an order and getting an order result', () => {
it('should send a post request and return the order result', done => {
service.placeOrder(cartId).subscribe(res => {
expect(res).toEqual(mockCheckoutOrderResult);
done();
});

const req = httpMock.expectOne(service['url']);

expect(req.request.method).toEqual('POST');
expect(req.request.body).toEqual(jasmine.objectContaining({ cartId }));
req.flush(mockCheckoutOrderResult);
});
});
});
Loading
Loading