forked from reactioncommerce/api-plugin-carts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'reactioncommerce:trunk' into trunk
- Loading branch information
Showing
5 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js"; | ||
import hashToken from "@reactioncommerce/api-utils/hashToken.js"; | ||
import anonymousCartByCartId from "./anonymousCartByCartId.js"; | ||
|
||
test("query anonymous cart", async () => { | ||
const cartId = "123"; | ||
const cartToken = "xyz"; | ||
const cart = { | ||
_id: cartId, | ||
anonymousAccessToken: cartToken | ||
}; | ||
const callingParams = { | ||
_id: cartId, | ||
anonymousAccessToken: hashToken(cartToken) | ||
}; | ||
|
||
mockContext.collections.Cart.findOne.mockReturnValueOnce(Promise.resolve(cart)); | ||
|
||
const result = await anonymousCartByCartId(mockContext, { cartId, cartToken }); | ||
expect(result).toEqual(cart); | ||
expect(mockContext.collections.Cart.findOne).toHaveBeenCalledWith(callingParams); | ||
}); | ||
|
||
test("query without hashed access token", async () => { | ||
const cartId = "123"; | ||
const cartToken = "xyz"; | ||
const callingParams = { | ||
_id: cartId, | ||
anonymousAccessToken: cartToken | ||
}; | ||
|
||
mockContext.collections.Cart.findOne.mockReturnValueOnce(Promise.resolve(null)); | ||
|
||
const result = await anonymousCartByCartId(mockContext, { cartId, cartToken }); | ||
expect(result).toEqual(null); | ||
expect(mockContext.collections.Cart.findOne).not.toHaveBeenCalledWith(callingParams); | ||
}); |