feat: medusa-react admin hooks (#978)

* 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>
This commit is contained in:
Zakaria El Asri
2022-02-02 17:10:56 +01:00
committed by GitHub
co-authored by Sebastian Rindom Sebastian Rindom
parent 24175025d1
commit 2e384842d5
209 changed files with 27917 additions and 2727 deletions
@@ -0,0 +1,176 @@
import {
useAdminCreateSwap,
useAdminReceiveSwap,
useAdminFulfillSwap,
useAdminCancelSwap,
useAdminCreateSwapShipment,
useAdminProcessSwapPayment,
useAdminCancelSwapFulfillment,
} from "../../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../../mocks/data"
import { createWrapper } from "../../../utils"
describe("useAdminCreateSwap hook", () => {
test("creates a swap and returns the order", async () => {
const orderId = fixtures.get("order").id
const swap = {
return_items: [
{
item_id: "test-item",
quantity: 1,
},
],
additional_items: [
{
variant_id: "another-item",
quantity: 1,
},
],
}
const { result, waitFor } = renderHook(() => useAdminCreateSwap(orderId), {
wrapper: createWrapper(),
})
result.current.mutate(swap)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.order).toEqual(fixtures.get("order"))
})
})
describe("useAdminReceiveSwap hook", () => {
test("receives a swap", async () => {
const orderId = fixtures.get("order").id
const swapId = "test-swap"
const payload = {
items: [
{
item_id: "test_id",
quantity: 1,
},
],
}
const { result, waitFor } = renderHook(() => useAdminReceiveSwap(orderId), {
wrapper: createWrapper(),
})
result.current.mutate({ swap_id: swapId, ...payload })
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.order).toEqual(fixtures.get("order"))
})
})
describe("useAdminFulfillSwap hook", () => {
test("receives a swap", async () => {
const orderId = fixtures.get("order").id
const swapId = "test-swap"
const payload = {
no_notification: false,
}
const { result, waitFor } = renderHook(() => useAdminFulfillSwap(orderId), {
wrapper: createWrapper(),
})
result.current.mutate({ swap_id: swapId, ...payload })
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.order).toEqual(fixtures.get("order"))
})
})
describe("useAdminCreateSwapShipment hook", () => {
test("creates a swap shipment", async () => {
const orderId = fixtures.get("order").id
const swapId = "test-swap"
const payload = {
fulfillment_id: "test-ful",
tracking_numbers: [],
}
const { result, waitFor } = renderHook(
() => useAdminCreateSwapShipment(orderId),
{
wrapper: createWrapper(),
}
)
result.current.mutate({ swap_id: swapId, ...payload })
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.order).toEqual(fixtures.get("order"))
})
})
describe("useAdminProcessSwapPayment hook", () => {
test("process a swap's payment", async () => {
const orderId = fixtures.get("order").id
const swapId = "test-swap"
const { result, waitFor } = renderHook(
() => useAdminProcessSwapPayment(orderId),
{
wrapper: createWrapper(),
}
)
result.current.mutate(swapId)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.order).toEqual(fixtures.get("order"))
})
})
describe("useAdminCancelSwap hook", () => {
test("cancels a swap", async () => {
const orderId = fixtures.get("order").id
const swapId = "test-swap"
const { result, waitFor } = renderHook(() => useAdminCancelSwap(orderId), {
wrapper: createWrapper(),
})
result.current.mutate(swapId)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.order).toEqual(fixtures.get("order"))
})
})
describe("useAdminCancelSwapFulfillment hook", () => {
test("cancels a swap", async () => {
const orderId = fixtures.get("order").id
const swapId = "test-swap"
const fulfillmentId = "test-ful"
const { result, waitFor } = renderHook(
() => useAdminCancelSwapFulfillment(orderId),
{
wrapper: createWrapper(),
}
)
result.current.mutate({ swap_id: swapId, fulfillment_id: fulfillmentId })
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.order).toEqual(fixtures.get("order"))
})
})
@@ -0,0 +1,32 @@
import { useAdminSwap, useAdminSwaps } from "../../../../src"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../../mocks/data"
import { createWrapper } from "../../../utils"
describe("useAdminSwaps hook", () => {
test("returns a list of swaps", async () => {
const swaps = fixtures.list("swap")
const { result, waitFor } = renderHook(() => useAdminSwaps(), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.swaps).toEqual(swaps)
})
})
describe("useAdminSwap hook", () => {
test("returns a swap", async () => {
const swap = fixtures.get("swap")
const { result, waitFor } = renderHook(() => useAdminSwap(swap.id), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.swap).toEqual(swap)
})
})