Skip to content

Commit

Permalink
Merge pull request #32 from reactioncommerce/fix-mohan-16-extend-cont…
Browse files Browse the repository at this point in the history
…ext-schema

chore: add transformAndValidateCart tests
  • Loading branch information
MohanNarayana authored Dec 8, 2021
2 parents 3ed0104 + 2b71d67 commit 9ed7b7f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/mutations/transformAndValidateCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const logCtx = { name: "cart", file: "transformAndValidateCart" };
* @returns {undefined}
*/
export default async function transformAndValidateCart(context, cart) {
const cartSchema = context.simpleSchemas.Cart;
const { simpleSchemas: { Cart: cartSchema } } = context;
updateCartFulfillmentGroups(context, cart);

let commonOrders;
Expand Down
79 changes: 79 additions & 0 deletions src/mutations/transformAndValidateCart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js";
import { Cart } from "../simpleSchemas.js";
import transformAndValidateCart from "./transformAndValidateCart.js";

mockContext.simpleSchemas = {};
mockContext.simpleSchemas.Cart = Cart;

const accountCart = {
_id: "cartId",
accountId: "accountId",
shopId: "shopId",
currencyCode: "currencyCode",
createdAt: new Date()
};
const expectedResult = { ...accountCart, shipping: [] };
test("valid account cart", async () => {
await transformAndValidateCart(mockContext, accountCart);
expect(accountCart).toEqual(expectedResult);
});


const anonymousCart = {
_id: "cartId",
anonymousAccessToken: "anonymousAccessToken",
shopId: "shopId",
currencyCode: "currencyCode",
createdAt: new Date()
};
const expectedAnonymousResult = { ...anonymousCart, shipping: [] };
test("valid anonymous cart", async () => {
await transformAndValidateCart(mockContext, anonymousCart);
expect(anonymousCart).toEqual(expectedAnonymousResult);
});


const accountCartNoShopId = {
_id: "cartId",
accountId: "accountId",
currencyCode: "currencyCode",
createdAt: new Date()
};
test("invalid account cart - no shopId", async () => {
try {
await transformAndValidateCart(mockContext, accountCartNoShopId);
} catch (error) {
expect(error.details[0].message).toEqual("Cart ShopId is required");
}
});


const accountCartNoCurrencyCode = {
_id: "cartId",
accountId: "accountId",
shopId: "shopId",
createdAt: new Date()
};
test("invalid account cart - no currency code", async () => {
try {
await transformAndValidateCart(mockContext, accountCartNoCurrencyCode);
} catch (error) {
expect(error.details[0].message).toEqual("Currency code is required");
}
});


const accountCartInvalidDate = {
_id: "cartId",
accountId: "accountId",
shopId: "shopId",
currencyCode: "currencyCode",
createdAt: "date"
};
test("invalid account cart - invalid date format", async () => {
try {
await transformAndValidateCart(mockContext, accountCartInvalidDate);
} catch (error) {
expect(error.details[0].message).toEqual("Created at must be of type Date");
}
});

0 comments on commit 9ed7b7f

Please sign in to comment.