import { assert } from 'chai'; import { TestReferences } from '../../fixtures/TestReferences'; import { TestRestClient } from '../../fixtures/TestRestClient'; import { PurchaseOrderStateV1, PurchaseOrderV1 } from 'bm-vslice-ecommerce-layered-client-purchaseorders-pipservices-node'; import { PaymentMethodV1 } from 'bm-vslice-ecommerce-layered-client-payments-pipservices-node'; const ORDER1: PurchaseOrderV1 = { id: '1', user_session_id: '2', currency_code: 'USD', total: 100, state: PurchaseOrderStateV1.New, items: [{ price: 40, product_id: 'product-1', product_name: 'product name 1', quantity: 2, total: 80, description: 'desctiption for product 1', }, { price: 20, product_id: 'product-2', product_name: 'product name 2', quantity: 1, total: 20, description: 'desctiption for product 2', }] }; const PAYMENT_METHOD1: PaymentMethodV1 = { id: null, type: 'card', customer_id: '2', card: { expire_month: 5, expire_year: 2022, first_name: 'Steve', last_name: 'Jobs', number: '5200828282828210', ccv: '124' } } suite('OrdersRoutesV1', () => { let references: TestReferences; let rest: TestRestClient; setup(async () => { rest = new TestRestClient(); references = new TestReferences(); await references.open(null); }); teardown(async () => { await references.close(null); }); test('CRUD Operations', async () => { // Create the order let order = await rest.post('/api/v1/orders/checkout_and_pay', {order: ORDER1, payment_method: PAYMENT_METHOD1}); assertEqualPurchaseOrder(order, ORDER1); // Get all orders const page = await rest.get('/api/v1/orders'); assert.isObject(page); assert.lengthOf(page.data, 1); // Get one order order = await rest.get('/api/v1/orders/' + order.id + '/' + order.user_session_id); assertEqualPurchaseOrder(order, ORDER1); // Get payment methods const paymentMethods = await rest.get('/api/v1/payment_methods/' + ORDER1.user_session_id); assert.isObject(paymentMethods); assert.lengthOf(paymentMethods.data, 1); }); }); function assertEqualPurchaseOrder(actual: PurchaseOrderV1, expected: PurchaseOrderV1) { assert.isNotNull(actual); assert.isNotNull(expected); assert.equal(actual.id, expected.id); assert.equal(actual.user_session_id, expected.user_session_id); assert.equal(actual.total, expected.total); assert.equal(actual.currency_code, expected.currency_code); if (expected.items != null) { assert.isNotNull(actual.items); assert.equal(actual.items.length, expected.items.length); for (let index = 0; index < expected.items.length; index++) { const expectedItem = expected.items[index]; const actualItem = actual.items.find((value) => value.product_id == expectedItem.product_id); assert.isNotNull(actualItem); } } }