From b1d1fa6ff41be86eea9bd150ea45a7901ba7d28b Mon Sep 17 00:00:00 2001 From: Mohan Narayana Date: Wed, 8 Dec 2021 13:20:08 -0600 Subject: [PATCH 1/2] chore: add transformAndValidateCart tests Signed-off-by: Mohan Narayana --- src/mutations/transformAndValidateCart.js | 2 +- .../transformAndValidateCart.test.js | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/mutations/transformAndValidateCart.test.js diff --git a/src/mutations/transformAndValidateCart.js b/src/mutations/transformAndValidateCart.js index d61f4d21305..544ea4ce836 100644 --- a/src/mutations/transformAndValidateCart.js +++ b/src/mutations/transformAndValidateCart.js @@ -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; diff --git a/src/mutations/transformAndValidateCart.test.js b/src/mutations/transformAndValidateCart.test.js new file mode 100644 index 00000000000..944dca3f0e3 --- /dev/null +++ b/src/mutations/transformAndValidateCart.test.js @@ -0,0 +1,78 @@ +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() +}; +test("valid account cart", async () => { + await transformAndValidateCart(mockContext, accountCart); + const result = { ...accountCart, shipping: [] }; + expect(result).toEqual(result); +}); + + +const anonymousCart = { + _id: "cartId", + anonymousAccessToken: "anonymousAccessToken", + shopId: "shopId", + currencyCode: "currencyCode", + createdAt: new Date() +}; +test("valid anonymous cart", async () => { + await transformAndValidateCart(mockContext, anonymousCart); + const result = { ...anonymousCart, shipping: [] }; + expect(result).toEqual(result); +}); + + +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"); + } +}); From 2b71d6768eba388a39a6aae22026e56dcd9fd8b2 Mon Sep 17 00:00:00 2001 From: Mohan Narayana Date: Wed, 8 Dec 2021 14:27:22 -0600 Subject: [PATCH 2/2] chore: change assertion parameters Signed-off-by: Mohan Narayana --- src/mutations/transformAndValidateCart.test.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/mutations/transformAndValidateCart.test.js b/src/mutations/transformAndValidateCart.test.js index 944dca3f0e3..aeb15921482 100644 --- a/src/mutations/transformAndValidateCart.test.js +++ b/src/mutations/transformAndValidateCart.test.js @@ -12,10 +12,10 @@ const accountCart = { currencyCode: "currencyCode", createdAt: new Date() }; +const expectedResult = { ...accountCart, shipping: [] }; test("valid account cart", async () => { await transformAndValidateCart(mockContext, accountCart); - const result = { ...accountCart, shipping: [] }; - expect(result).toEqual(result); + expect(accountCart).toEqual(expectedResult); }); @@ -26,10 +26,10 @@ const anonymousCart = { currencyCode: "currencyCode", createdAt: new Date() }; +const expectedAnonymousResult = { ...anonymousCart, shipping: [] }; test("valid anonymous cart", async () => { await transformAndValidateCart(mockContext, anonymousCart); - const result = { ...anonymousCart, shipping: [] }; - expect(result).toEqual(result); + expect(anonymousCart).toEqual(expectedAnonymousResult); }); @@ -62,6 +62,7 @@ test("invalid account cart - no currency code", async () => { } }); + const accountCartInvalidDate = { _id: "cartId", accountId: "accountId",