* add: medusa admin hooks + tests * fix: remove unneeded props * fix: deps * fix: deps * fix: deps * fix: failing tests * fix: failing tests * fix: query key * add: yarn workspaces * fix: linting medusa-react * fix: add prepare script * fix: buildOptions * fix: useAdminShippingOptions query * fix: use qs instead for query params (#1019) * fix: formatting * debug: ci pipeline * debug: log node_modules structure * debug: use lerna bootstrap * debug: update node version * debug: print pkgs in workspace * debug: print pkgs in workspace * debug: print pkgs in workspace * debug: print pkgs in workspace * debug: add explicit build step * fix: jsdoc * debug: run build step * debug: fix build errors * debug: add build step to integration tests * fix: failing test * cleanup Co-authored-by: Sebastian Rindom <seb@medusajs.com> Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
197 lines
5.1 KiB
TypeScript
197 lines
5.1 KiB
TypeScript
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")
|
|
)
|
|
})
|
|
})
|
|
})
|