feat: add medusa-react (#913)
This commit is contained in:
@@ -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)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user