feat: add medusa-react (#913)

This commit is contained in:
Zakaria El Asri authored and GitHub committed 2021-12-14 19:09:36 +01:00
1 parent 40f6e88875
commit d0d8dd7bf6
97 files changed
+22822 -7595

No files matched your search

@@ -0,0 +1,196 @@
import { useCart } from "../../src"
import { act, renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../mocks/data"
import { createCartWrapper } from "../utils"
import { Cart } from "../../src/types"
describe("useBag hook", () => {
describe("sets a cart", () => {
test("success", async () => {
const { result } = renderHook(() => useCart(), {
wrapper: createCartWrapper(),
})
const { setCart } = result.current
act(() => {
setCart(fixtures.get("cart") as unknown as Cart)
})
const { cart, totalItems } = result.current
expect(cart).toEqual(fixtures.get("cart"))
expect(totalItems).toEqual(0)
})
})
describe("createCart", () => {
test("creates a cart", async () => {
const { result, waitFor } = renderHook(() => useCart(), {
wrapper: createCartWrapper(),
})
const { createCart } = result.current
act(() => {
createCart.mutate({})
})
await waitFor(() => result.current.createCart.isSuccess)
const { cart, totalItems } = result.current
expect(cart).toEqual(fixtures.get("cart"))
expect(totalItems).toEqual(0)
})
})
describe("startCheckout", () => {
test("creates a payment session and updates the cart", async () => {
const { result, waitFor } = renderHook(() => useCart(), {
wrapper: createCartWrapper(),
initialProps: {
initialCartState: {
...fixtures.get("cart"),
id: "test-cart",
} as unknown as Cart,
},
})
const { startCheckout } = result.current
act(() => {
startCheckout.mutate()
})
await waitFor(() => result.current.startCheckout.isSuccess)
const { cart, totalItems } = result.current
expect(cart).toEqual({
...fixtures.get("cart"),
id: "test-cart",
})
expect(totalItems).toEqual(0)
})
})
describe("updateCart", () => {
test("updates the cart", async () => {
const { result, waitFor } = renderHook(() => useCart(), {
wrapper: createCartWrapper(),
initialProps: {
initialCartState: {
...fixtures.get("cart"),
id: "test-cart",
} as unknown as Cart,
},
})
const { updateCart } = result.current
act(() => {
updateCart.mutate({
email: "zak@test.com",
})
})
await waitFor(() => result.current.updateCart.isSuccess)
const { cart, totalItems } = result.current
expect(cart).toEqual({
...fixtures.get("cart"),
id: "test-cart",
email: "zak@test.com",
})
expect(totalItems).toEqual(0)
})
})
describe("addShippingMethod", () => {
test("adds a shipping method and updates the cart", async () => {
const { result, waitFor } = renderHook(() => useCart(), {
wrapper: createCartWrapper(),
initialProps: {
initialCartState: {
...fixtures.get("cart"),
id: "test-cart",
} as unknown as Cart,
},
})
const { addShippingMethod } = result.current
act(() => {
addShippingMethod.mutate({
option_id: "test-option",
})
})
await waitFor(() => result.current.addShippingMethod.isSuccess)
const { cart, totalItems } = result.current
expect(cart).toEqual({
...fixtures.get("cart"),
id: "test-cart",
})
expect(totalItems).toEqual(0)
})
})
describe("pay", () => {
test("sets a payment session and updates the cart", async () => {
const { result, waitFor } = renderHook(() => useCart(), {
wrapper: createCartWrapper(),
initialProps: {
initialCartState: {
...fixtures.get("cart"),
id: "test-cart",
} as unknown as Cart,
},
})
const { pay } = result.current
act(() => {
pay.mutate({
provider_id: "test-provider",
})
})
await waitFor(() => result.current.pay.isSuccess)
const { cart, totalItems } = result.current
expect(cart).toEqual({
...fixtures.get("cart"),
id: "test-cart",
})
expect(totalItems).toEqual(0)
})
})
describe("completeCheckout", () => {
test("calls complete cart, does not update the cart, and returns an order", async () => {
const { result, waitFor } = renderHook(() => useCart(), {
wrapper: createCartWrapper(),
initialProps: {
initialCartState: fixtures.get("cart") as unknown as Cart,
},
})
const { completeCheckout } = result.current
act(() => {
completeCheckout.mutate()
})
await waitFor(() => result.current.completeCheckout.isSuccess)
const { cart, totalItems } = result.current
expect(cart).toEqual(fixtures.get("cart"))
expect(totalItems).toEqual(0)
expect(result.current.completeCheckout.data.type).toEqual("order")
expect(result.current.completeCheckout.data.data).toEqual(
fixtures.get("order")
)
})
})
})
@@ -0,0 +1,203 @@
import {
useCreateCart,
useUpdateCart,
useCompleteCart,
useCreatePaymentSession,
useUpdatePaymentSession,
useRefreshPaymentSession,
useSetPaymentSession,
useDeletePaymentSession,
useAddShippingMethodToCart,
} from "./../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useCreateCart hook", () => {
test("creates a cart", async () => {
const { result, waitFor } = renderHook(() => useCreateCart(), {
wrapper: createWrapper(),
})
result.current.mutate({})
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.cart).toEqual(fixtures.get("cart"))
})
})
describe("useUpdateCart hook", () => {
test("updates a cart", async () => {
const { result, waitFor } = renderHook(() => useUpdateCart("some-cart"), {
wrapper: createWrapper(),
})
result.current.mutate({
email: "new@email.com",
})
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.cart).toEqual({
...fixtures.get("cart"),
id: "some-cart",
email: "new@email.com",
})
})
})
describe("useCompleteCart hook", () => {
test("completes a cart", async () => {
const { result, waitFor } = renderHook(() => useCompleteCart("test-cart"), {
wrapper: createWrapper(),
})
result.current.mutate()
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.type).toEqual("order")
expect(result.current.data.data).toEqual(fixtures.get("order"))
})
})
describe("useCreatePaymentSession hook", () => {
test("creates a payment session", async () => {
const { result, waitFor } = renderHook(
() => useCreatePaymentSession("test-cart"),
{
wrapper: createWrapper(),
}
)
result.current.mutate()
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.cart).toEqual({
...fixtures.get("cart"),
id: "test-cart",
})
})
})
describe("useUpdatePaymentSession hook", () => {
test("updates a payment session", async () => {
const { result, waitFor } = renderHook(
() => useUpdatePaymentSession("test-cart"),
{
wrapper: createWrapper(),
}
)
result.current.mutate({
data: {},
provider_id: "stripe",
})
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.cart).toEqual({
...fixtures.get("cart"),
id: "test-cart",
})
})
})
describe("useRefreshPaymentSession hook", () => {
test("refreshes a payment session", async () => {
const { result, waitFor } = renderHook(
() => useRefreshPaymentSession("test-cart"),
{
wrapper: createWrapper(),
}
)
result.current.mutate({
provider_id: "stripe",
})
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.cart).toEqual({
...fixtures.get("cart"),
id: "test-cart",
})
})
})
describe("useSetPaymentSession hook", () => {
test("sets a payment session", async () => {
const { result, waitFor } = renderHook(
() => useSetPaymentSession("test-cart"),
{
wrapper: createWrapper(),
}
)
result.current.mutate({
provider_id: "stripe",
})
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.cart).toEqual({
...fixtures.get("cart"),
id: "test-cart",
})
})
})
describe("useDeletePaymentSession hook", () => {
test("deletes a payment session", async () => {
const { result, waitFor } = renderHook(
() => useDeletePaymentSession("test-cart"),
{
wrapper: createWrapper(),
}
)
result.current.mutate({
provider_id: "stripe",
})
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.cart).toEqual({
...fixtures.get("cart"),
id: "test-cart",
})
})
})
describe("useAddShippingMethodToCart hook", () => {
test("adds a shipping method to a cart", async () => {
const { result, waitFor } = renderHook(
() => useAddShippingMethodToCart("test-cart"),
{
wrapper: createWrapper(),
}
)
result.current.mutate({
option_id: "test-option",
})
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.cart).toEqual({
...fixtures.get("cart"),
id: "test-cart",
})
})
})
@@ -0,0 +1,18 @@
import { useGetCart } from "./../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useGetCart hook", () => {
test("returns a cart", async () => {
const cart = fixtures.get("cart")
const { result, waitFor } = renderHook(() => useGetCart(cart.id), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.cart).toEqual(cart)
})
})
@@ -0,0 +1,32 @@
import { useCollections, useCollection } from "./../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useCollections hook", () => {
test("returns a list of collections", async () => {
const collections = fixtures.list("product_collection")
const { result, waitFor } = renderHook(() => useCollections(), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.collections).toEqual(collections)
})
})
describe("useCollection hook", () => {
test("returns a collection", async () => {
const collection = fixtures.get("product_collection")
const { result, waitFor } = renderHook(() => useCollection(collection.id), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.collection).toEqual(collection)
})
})
@@ -0,0 +1,59 @@
import { useCreateCustomer, useUpdateMe } from "./../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useCreateCustomer hook", () => {
test("creates a new customer", async () => {
const customer = {
first_name: "john",
last_name: "wick",
email: "johnwick@medusajs.com",
password: "supersecret",
phone: "111111",
}
const { result, waitFor } = renderHook(() => useCreateCustomer(), {
wrapper: createWrapper(),
})
result.current.mutate(customer)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.customer).toEqual({
...fixtures.get("customer"),
...customer,
})
})
})
describe("useUpdateMe hook", () => {
test("updates current customer", async () => {
const customer = {
first_name: "lebron",
last_name: "james",
email: "lebronjames@medusajs.com",
password: "supersecret",
phone: "111111",
}
const { result, waitFor } = renderHook(() => useUpdateMe(), {
wrapper: createWrapper(),
})
result.current.mutate({
id: "cus_test",
...customer,
})
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.customer).toEqual({
...fixtures.get("customer"),
...customer,
})
})
})
@@ -0,0 +1,89 @@
import { rest } from "msw"
import { server } from "./../../../mocks/server"
import { useMeCustomer, useCustomerOrders } from "./../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useMeCustomer hook", () => {
test("returns customer", async () => {
const customer = fixtures.get("customer")
const { result, waitFor } = renderHook(() => useMeCustomer(), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.customer).toEqual(customer)
})
})
describe("useCustomerOrders hook", () => {
test("returns customer's orders", async () => {
const orders = fixtures.list("order", 5)
const { result, waitFor } = renderHook(() => useCustomerOrders(), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.orders).toEqual(orders)
expect(result.current.limit).toEqual(5)
expect(result.current.offset).toEqual(0)
})
test("propagates query params and returns customer's orders", async () => {
const orders = fixtures.list("order")
server.use(
rest.get("/store/customers/me/orders", (req, res, ctx) => {
const limit = req.url.searchParams.get("limit")
const offset = req.url.searchParams.get("offset")
const expand = req.url.searchParams.get("expand")
const fields = req.url.searchParams.get("fields")
expect({
limit,
offset,
expand,
fields,
}).toEqual({
limit: "2",
offset: "5",
expand: "relation_1,relation_2",
fields: "field_1,field_2",
})
return res(
ctx.status(200),
ctx.json({
orders,
limit: 2,
offset: 5,
})
)
})
)
const { result, waitFor } = renderHook(
() =>
useCustomerOrders({
limit: 2,
offset: 5,
expand: "relation_1,relation_2",
fields: "field_1,field_2",
}),
{
wrapper: createWrapper(),
}
)
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.orders).toEqual(orders)
expect(result.current.limit).toEqual(2)
expect(result.current.offset).toEqual(5)
})
})
@@ -0,0 +1,18 @@
import { useGiftCard } from "./../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useGiftCard hook", () => {
test("returns a gift card", async () => {
const giftCard = fixtures.get("gift_card")
const { result, waitFor } = renderHook(() => useGiftCard(giftCard.id), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.gift_card).toEqual(giftCard)
})
})
@@ -0,0 +1,89 @@
import {
useCreateLineItem,
useUpdateLineItem,
useDeleteLineItem,
} from "./../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useCreateLineItem hook", () => {
test("creates a line item", async () => {
const lineItem = {
variant_id: "test-variant",
quantity: 1,
}
const { result, waitFor } = renderHook(
() => useCreateLineItem("test-cart"),
{
wrapper: createWrapper(),
}
)
result.current.mutate(lineItem)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.cart.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
...lineItem,
}),
])
)
})
})
describe("useUpdateLineItem hook", () => {
test("updates a line item", async () => {
const lineItem = {
lineId: "some-item-id",
quantity: 3,
}
const { result, waitFor } = renderHook(
() => useUpdateLineItem("test-cart"),
{
wrapper: createWrapper(),
}
)
result.current.mutate(lineItem)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.cart.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: lineItem.lineId,
quantity: lineItem.quantity,
}),
])
)
})
})
describe("useDeleteLineItem hook", () => {
test("deletes a line item", async () => {
const lineItem = {
lineId: "some-item-id",
}
const { result, waitFor } = renderHook(
() => useDeleteLineItem("test-cart"),
{
wrapper: createWrapper(),
}
)
result.current.mutate(lineItem)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.cart).toEqual(fixtures.get("cart"))
})
})
@@ -0,0 +1,79 @@
import { useOrderLookup } from "./../../../src/hooks/orders/queries"
import { rest } from "msw"
import { server } from "./../../../mocks/server"
import { useCartOrder, useOrder } from "./../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useOrder hook", () => {
test("returns an order", async () => {
const order = fixtures.get("order")
const { result, waitFor } = renderHook(() => useOrder(order.id), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.order).toEqual(order)
})
})
describe("useCartOrder hook", () => {
test("returns a cart order", async () => {
const order = fixtures.get("order")
const { result, waitFor } = renderHook(() => useCartOrder("test_cart"), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.order).toEqual(order)
})
})
describe("useOrderLookup hook", () => {
test("propagates the query params and returns an order", async () => {
const order = fixtures.get("order")
const displayId = 400,
emailParam = "customer@test.com"
server.use(
rest.get("/store/orders", (req, res, ctx) => {
const display_id = req.url.searchParams.get("display_id")
const email = req.url.searchParams.get("email")
expect({
display_id,
email,
}).toEqual({
email: emailParam,
display_id: displayId.toString(),
})
return res(
ctx.status(200),
ctx.json({
order,
})
)
})
)
const { result, waitFor } = renderHook(
() =>
useOrderLookup({
display_id: displayId,
email: emailParam,
}),
{
wrapper: createWrapper(),
}
)
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.order).toEqual(order)
})
})
@@ -0,0 +1,53 @@
import { renderHook } from "@testing-library/react-hooks"
import { useProducts, useProduct } from "../../../src"
import { createWrapper } from "../../utils"
import { fixtures } from "../../../mocks/data/index"
describe("useProducts hook", () => {
test("gets a list of products", async () => {
const { result, waitFor } = renderHook(() => useProducts(), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.products).toEqual(fixtures.list("product"))
})
test("gets a list of products based on limit and offset", async () => {
const { result, waitFor } = renderHook(
() =>
useProducts({
limit: 2,
offset: 5,
}),
{
wrapper: createWrapper(),
}
)
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.products).toEqual(fixtures.list("product"))
expect(result.current.limit).toEqual(2)
expect(result.current.offset).toEqual(5)
})
})
describe("useProducts hook", () => {
test("success", async () => {
const { result, waitFor } = renderHook(
() => useProduct("prod_01F0YESHQ27Y31CAMD0NV6W9YP"),
{
wrapper: createWrapper(),
}
)
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.product).toEqual(fixtures.get("product"))
})
})
@@ -0,0 +1,32 @@
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
import { useRegion, useRegions } from "../../../src/"
describe("useRegions hook", () => {
test("success", async () => {
const regions = fixtures.list("region")
const { result, waitFor } = renderHook(() => useRegions(), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.regions).toEqual(regions)
})
})
describe("useRegion hook", () => {
test("success", async () => {
const region = fixtures.get("region")
const { result, waitFor } = renderHook(() => useRegion(region.id), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.region).toEqual(region)
})
})
@@ -0,0 +1,35 @@
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
import { useReturnReason, useReturnReasons } from "../../../src/"
describe("useReturnReasons hook", () => {
test("returns a list of return reasons", async () => {
const return_reasons = fixtures.list("return_reason")
const { result, waitFor } = renderHook(() => useReturnReasons(), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.return_reasons).toEqual(return_reasons)
})
})
describe("useReturnReason hook", () => {
test("returns a return reason", async () => {
const return_reason = fixtures.get("return_reason")
const { result, waitFor } = renderHook(
() => useReturnReason(return_reason.id),
{
wrapper: createWrapper(),
}
)
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.return_reason).toEqual(return_reason)
})
})
@@ -0,0 +1,34 @@
import { renderHook } from "@testing-library/react-hooks"
import { useCreateReturn } from "../../../src"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useCreateReturn hook", () => {
test("creates a return", async () => {
const ret = {
order_id: "order_38",
items: [
{
item_id: "test-item",
quantity: 1,
},
],
}
const { result, waitFor } = renderHook(() => useCreateReturn(), {
wrapper: createWrapper(),
})
result.current.mutate(ret)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.return).toEqual(
expect.objectContaining({
...fixtures.get("return"),
order_id: ret.order_id,
})
)
})
})
@@ -0,0 +1,83 @@
import { rest } from "msw"
import { server } from "./../../../mocks/server"
import { useShippingOptions, useCartShippingOptions } from "../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useShippingOptions hook", () => {
test("returns a list of shipping options", async () => {
const shippingOptions = fixtures.list("shipping_option", 5)
const { result, waitFor } = renderHook(() => useShippingOptions(), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.shipping_options).toEqual(shippingOptions)
})
test("when shipping options params are provided, then they should be sent as query params", async () => {
const shippingOptions = fixtures.list("shipping_option")
server.use(
rest.get("/store/shipping-options/", (req, res, ctx) => {
const product_ids = req.url.searchParams.get("product_ids")
const is_return = req.url.searchParams.get("is_return")
const region_id = req.url.searchParams.get("region_id")
expect({
product_ids,
is_return,
region_id,
}).toEqual({
product_ids: "1,2,3",
is_return: "false",
region_id: "test-region",
})
return res(
ctx.status(200),
ctx.json({
shipping_options: fixtures.list("shipping_option"),
})
)
})
)
const { result, waitFor } = renderHook(
() =>
useShippingOptions({
product_ids: "1,2,3",
is_return: "false",
region_id: "test-region",
}),
{
wrapper: createWrapper(),
}
)
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.shipping_options).toEqual(shippingOptions)
})
})
describe("useCartShippingOptions hook", () => {
test("success", async () => {
const cartShippingOptions = fixtures.list("shipping_option")
const { result, waitFor } = renderHook(
() => useCartShippingOptions("cart_test"),
{
wrapper: createWrapper(),
}
)
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.shipping_options).toEqual(cartShippingOptions)
})
})
@@ -0,0 +1,40 @@
import { useCreateSwap } from "./../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useCreateSwap hook", () => {
test("creates a return", async () => {
const swap = {
order_id: "order_test",
additional_items: [
{
variant_id: "new-item",
quantity: 1,
},
],
return_items: [
{
item_id: "return-item",
quantity: 1,
},
],
}
const { result, waitFor } = renderHook(() => useCreateSwap(), {
wrapper: createWrapper(),
})
result.current.mutate(swap)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.swap).toEqual(
expect.objectContaining({
...fixtures.get("swap"),
order_id: swap.order_id,
})
)
})
})
@@ -0,0 +1,18 @@
import { useCartSwap } from "./../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../mocks/data"
import { createWrapper } from "../../utils"
describe("useCartSwap hook", () => {
test("returns a swap", async () => {
const swap = fixtures.get("swap")
const { result, waitFor } = renderHook(() => useCartSwap("cart_test"), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.swap).toEqual(swap)
})
})
@@ -0,0 +1,322 @@
import { generateCartState } from "../../src/contexts/session-cart"
import { ProductVariant } from "@medusajs/medusa"
import { useSessionCart } from "../../src"
import { act, renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../mocks/data"
import { createSessionCartWrapper } from "../utils"
const initialSessionCartState = {
region: fixtures.get("region"),
totalItems: 0,
total: 0,
items: [],
}
describe("useSessionCart hook", () => {
describe("sets a region", () => {
test("success", async () => {
const { result } = renderHook(() => useSessionCart(), {
wrapper: createSessionCartWrapper(),
})
const { setRegion } = result.current
act(() => {
setRegion(fixtures.get("region"))
})
const { region, total, totalItems } = result.current
expect(region).toEqual(fixtures.get("region"))
expect(total).toEqual(0)
expect(totalItems).toEqual(0)
})
})
describe("item operations", () => {
test("addItem", () => {
const { result } = renderHook(() => useSessionCart(), {
wrapper: createSessionCartWrapper(),
initialProps: {
initialState: initialSessionCartState,
},
})
const { addItem } = result.current
const variant = fixtures.get("product_variant")
act(() => {
addItem({
variant: variant as unknown as ProductVariant,
quantity: 1,
})
})
const { items, totalItems, total } = result.current
expect(totalItems).toBe(1)
expect(total).toBe(1000)
expect(items).toEqual([
{
variant: expect.objectContaining(variant),
quantity: 1,
total: 1000,
},
])
})
test("updateItem", () => {
const variant = fixtures.get("product_variant")
const { result } = renderHook(() => useSessionCart(), {
wrapper: createSessionCartWrapper(),
initialProps: {
initialState: generateCartState(initialSessionCartState, [
{
variant: variant as unknown as ProductVariant,
quantity: 1,
},
{
variant: {
...variant,
id: "test-variant",
} as unknown as ProductVariant,
quantity: 1,
},
]),
},
})
const { updateItem } = result.current
act(() => {
updateItem(variant.id, {
quantity: 4,
})
})
const { items, totalItems, total } = result.current
expect(totalItems).toBe(5)
expect(total).toBe(5 * 1000)
expect(items).toEqual([
{
variant: expect.objectContaining(variant),
quantity: 4,
total: 1000,
},
{
variant: {
...variant,
id: "test-variant",
},
quantity: 1,
total: 1000,
},
])
})
test("removeItem", () => {
const variant = fixtures.get("product_variant")
const { result } = renderHook(() => useSessionCart(), {
wrapper: createSessionCartWrapper(),
initialProps: {
initialState: generateCartState(initialSessionCartState, [
{
variant: variant as unknown as ProductVariant,
quantity: 3,
},
{
variant: {
...variant,
id: "test-variant",
} as unknown as ProductVariant,
quantity: 1,
},
]),
},
})
const { removeItem } = result.current
act(() => {
removeItem(variant.id)
})
const { items, totalItems, total } = result.current
expect(totalItems).toBe(1)
expect(total).toBe(1000)
expect(items).toEqual([
{
variant: {
...variant,
id: "test-variant",
},
quantity: 1,
total: 1000,
},
])
})
test("incrementItemQuantity", () => {
const variant = fixtures.get("product_variant")
const { result } = renderHook(() => useSessionCart(), {
wrapper: createSessionCartWrapper(),
initialProps: {
initialState: generateCartState(initialSessionCartState, [
{
variant: variant as unknown as ProductVariant,
quantity: 2,
},
]),
},
})
const { incrementItemQuantity } = result.current
act(() => {
incrementItemQuantity(variant.id)
})
const { items, totalItems, total } = result.current
expect(totalItems).toBe(3)
expect(total).toBe(3 * 1000)
expect(items).toEqual([
{
variant,
quantity: 3,
total: 1000,
},
])
})
test("decrementItemQuantity", () => {
const variant = fixtures.get("product_variant")
const { result } = renderHook(() => useSessionCart(), {
wrapper: createSessionCartWrapper(),
initialProps: {
initialState: generateCartState(initialSessionCartState, [
{
variant: variant as unknown as ProductVariant,
quantity: 4,
},
]),
},
})
const { decrementItemQuantity } = result.current
act(() => {
decrementItemQuantity(variant.id)
})
const { items, totalItems, total } = result.current
expect(totalItems).toBe(3)
expect(total).toBe(3 * 1000)
expect(items).toEqual([
{
variant,
quantity: 3,
total: 1000,
},
])
})
test("setItems", () => {
const variant = fixtures.get("product_variant")
const { result } = renderHook(() => useSessionCart(), {
wrapper: createSessionCartWrapper(),
initialProps: {
initialState: generateCartState(initialSessionCartState, [
{
variant: variant as unknown as ProductVariant,
quantity: 4,
},
]),
},
})
const { setItems } = result.current
act(() => {
setItems([
{
variant: {
...variant,
id: "test-variant",
} as unknown as ProductVariant,
quantity: 1,
},
])
})
const { items, totalItems, total } = result.current
expect(totalItems).toBe(1)
expect(total).toBe(1000)
expect(items).toEqual([
{
variant: expect.objectContaining({
id: "test-variant",
}),
quantity: 1,
total: 1000,
},
])
})
test("getItem", () => {
const variant = fixtures.get("product_variant")
const { result } = renderHook(() => useSessionCart(), {
wrapper: createSessionCartWrapper(),
initialProps: {
initialState: generateCartState(initialSessionCartState, [
{
variant: variant as unknown as ProductVariant,
quantity: 1,
},
]),
},
})
const { getItem } = result.current
let item
act(() => {
item = getItem(variant.id)
})
expect(item).toEqual({
variant,
quantity: 1,
total: 1000,
})
})
test("clearItems", () => {
const variant = fixtures.get("product_variant")
const { result } = renderHook(() => useSessionCart(), {
wrapper: createSessionCartWrapper(),
initialProps: {
initialState: generateCartState(initialSessionCartState, [
{
variant: variant as unknown as ProductVariant,
quantity: 4,
},
]),
},
})
const { clearItems } = result.current
act(() => {
clearItems()
})
const { items, totalItems, total } = result.current
expect(totalItems).toBe(0)
expect(total).toBe(0)
expect(items).toEqual([])
})
})
})
+72
View File
@@ -0,0 +1,72 @@
import * as React from "react"
import { QueryClient } from "react-query"
import {
SessionCartProvider,
SessionCartState,
CartProvider,
MedusaProvider,
} from "../src"
import { Cart } from "../src/types"
const createTestQueryClient = () =>
new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
})
export function createWrapper() {
const qc = createTestQueryClient()
return ({ children }) => (
<MedusaProvider queryClientProviderProps={{ client: qc }} baseUrl="">
{children}
</MedusaProvider>
)
}
export function createSessionCartWrapper() {
const qc = createTestQueryClient()
return ({
children,
initialState,
}: {
initialState: SessionCartState
children?: React.ReactNode
}) => {
return (
<MedusaProvider queryClientProviderProps={{ client: qc }} baseUrl="">
<SessionCartProvider initialState={initialState}>
{children}
</SessionCartProvider>
</MedusaProvider>
)
}
}
export function createCartWrapper() {
const qc = createTestQueryClient()
return ({
children,
initialSessionCartState,
initialCartState,
}: {
initialSessionCartState?: SessionCartState
initialCartState?: Cart
children?: React.ReactNode
}) => {
return (
<MedusaProvider queryClientProviderProps={{ client: qc }} baseUrl="">
<SessionCartProvider initialState={initialSessionCartState}>
<CartProvider initialState={initialCartState}>
{children}
</CartProvider>
</SessionCartProvider>
</MedusaProvider>
)
}
}
@@ -0,0 +1,169 @@
import { RegionInfo, ProductVariantInfo } from "./../../src/types"
import { fixtures } from "./../../mocks/data/"
import {
computeVariantPrice,
getVariantPrice,
computeAmount,
formatAmount,
formatVariantPrice,
} from "./../../src/"
describe("getVariantPrice", () => {
test("finds the variant price and returns its amount", () => {
const variant = fixtures.get("product_variant")
const region = fixtures.get("region")
const amount = getVariantPrice(
variant as unknown as ProductVariantInfo,
region
)
expect(amount).toEqual(1000)
})
test("when no region is provided, then it should return 0", () => {
const variant = fixtures.get("product_variant")
const amount = getVariantPrice(
variant as unknown as ProductVariantInfo,
{} as RegionInfo
)
expect(amount).toEqual(0)
})
test("when no product variant is provided, then it should return 0", () => {
const region = fixtures.get("region")
const amount = getVariantPrice({} as ProductVariantInfo, region)
expect(amount).toEqual(0)
})
test("when no product variant and region are provided, then it should return 0", () => {
const amount = getVariantPrice({} as ProductVariantInfo, {} as RegionInfo)
expect(amount).toEqual(0)
})
})
describe("computeAmount", () => {
test("given an amount and a region, it should return a decimal amount not including taxes", () => {
const region = fixtures.get("region")
const amount = computeAmount({ amount: 3000, region, includeTaxes: false })
expect(amount).toEqual(30)
})
test("given an amount and a region, it should return a decimal amount including taxes", () => {
const region = fixtures.get("region")
const amount = computeAmount({
amount: 3000,
region: {
...region,
tax_rate: 10,
},
})
expect(amount).toEqual(33)
})
test("when no region is provided, then it should return the decimal amount", () => {
const region = fixtures.get("region")
const amount = computeAmount({ amount: 2000, region })
expect(amount).toEqual(20)
})
})
describe("computeVariantPrice", () => {
test("finds the variant price and returns a decimal amount not including taxes", () => {
const variant = fixtures.get("product_variant")
const region = fixtures.get("region")
const price = computeVariantPrice({
variant: variant as unknown as ProductVariantInfo,
region,
})
expect(price).toEqual(10)
})
test("finds the variant price and returns a decimal amount including taxes", () => {
const variant = fixtures.get("product_variant")
const region = fixtures.get("region")
const price = computeVariantPrice({
variant: variant as unknown as ProductVariantInfo,
region: {
...region,
tax_rate: 15,
},
includeTaxes: true,
})
expect(price).toEqual(11.5)
})
})
describe("formatVariantPrice", () => {
test("given a variant and region, should return a decimal localized amount including taxes and the region's currency code", () => {
const region = fixtures.get("region")
const variant = fixtures.get("product_variant")
const price = formatVariantPrice({
variant: variant as unknown as ProductVariantInfo,
region: {
...region,
tax_rate: 15,
},
})
expect(price).toEqual("$11.50")
})
test("given a variant, region, and 1 digit, should return a decimal (1 fraction digit) localized amount including taxes and the region's currency code", () => {
const region = fixtures.get("region")
const variant = fixtures.get("product_variant")
const price = formatVariantPrice({
variant: variant as unknown as ProductVariantInfo,
region: {
...region,
tax_rate: 15,
},
maximumFractionDigits: 1,
})
expect(price).toEqual("$11.5")
})
test("given a variant, region, and a custom locale, should return a decimal localized amount including taxes and the region's currency code", () => {
const region = fixtures.get("region")
const variant = fixtures.get("product_variant")
const price = formatVariantPrice({
variant: variant as unknown as ProductVariantInfo,
region: {
...region,
tax_rate: 15,
},
locale: "fr-FR",
})
expect(price.replace(/\s/, " ")).toEqual("11,50 $US")
})
})
describe("formatAmount", () => {
test("given an amount and region, should return a decimal localized amount including taxes and the region's currency code", () => {
const region = fixtures.get("region")
const price = formatAmount({
amount: 3000,
region: {
...region,
tax_rate: 15,
},
})
expect(price).toEqual("$34.50")
})
test("given an amount and no region, should return a decimal localized amount", () => {
const price = formatAmount({ amount: 3000, region: {} as RegionInfo })
expect(price).toEqual("30")
})
})