feat(medusa-react): add product category queries and mutations (#3218)

This commit is contained in:
Riqwan Thamir
2023-02-16 10:22:23 +01:00
committed by GitHub
parent 12d304307a
commit 75924b682f
12 changed files with 491 additions and 4 deletions
@@ -0,0 +1,132 @@
import { renderHook } from "@testing-library/react-hooks"
import {
useAdminAddProductsToCategory,
useAdminCreateProductCategory,
useAdminDeleteProductCategory,
useAdminDeleteProductsFromCategory,
useAdminUpdateProductCategory,
} from "../../../../src"
import { fixtures } from "../../../../mocks/data"
import { createWrapper } from "../../../utils"
describe("useAdminCreateProductCategory hook", () => {
test("creates a product category", async () => {
const category = {
name: "Jeans category",
}
const { result, waitFor } = renderHook(
() => useAdminCreateProductCategory(),
{
wrapper: createWrapper(),
}
)
result.current.mutate(category)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.product_category).toEqual(
expect.objectContaining({
...fixtures.get("product_category"),
...category,
})
)
})
})
describe("useAdminUpdateProductCategory hook", () => {
test("updates a product category", async () => {
const category = {
name: "Updated name",
}
const categoryId = fixtures.get("product_category").id
const { result, waitFor } = renderHook(
() => useAdminUpdateProductCategory(categoryId),
{
wrapper: createWrapper(),
}
)
result.current.mutate(category)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.product_category).toEqual({
...fixtures.get("product_category"),
...category,
})
})
})
describe("useAdminDeleteProductCategory hook", () => {
test("deletes a product category", async () => {
const id = fixtures.get("product_category").id
const { result, waitFor } = renderHook(
() => useAdminDeleteProductCategory(id),
{ wrapper: createWrapper() }
)
result.current.mutate()
await waitFor(() => result.current.isSuccess)
expect(result.current.data).toEqual(
expect.objectContaining({
id,
object: "product-category",
deleted: true,
})
)
})
})
describe("useAdminAddProductsToCategory hook", () => {
test("add products to a product category", async () => {
const id = fixtures.get("product_category").id
const productId = fixtures.get("product").id
const { result, waitFor } = renderHook(
() => useAdminAddProductsToCategory(id),
{ wrapper: createWrapper() }
)
result.current.mutate({ product_ids: [{ id: productId }] })
await waitFor(() => result.current.isSuccess)
expect(result.current.data).toEqual(
expect.objectContaining({
product_category: fixtures.get("product_category"),
})
)
})
})
describe("useAdminDeleteProductsFromCategory hook", () => {
test("remove products from a product category", async () => {
const id = fixtures.get("product_category").id
const productId = fixtures.get("product").id
const { result, waitFor } = renderHook(
() => useAdminDeleteProductsFromCategory(id),
{ wrapper: createWrapper() }
)
result.current.mutate({ product_ids: [{ id: productId }] })
await waitFor(() => result.current.isSuccess)
expect(result.current.data).toEqual(
expect.objectContaining({
product_category: fixtures.get("product_category"),
})
)
})
})
@@ -0,0 +1,37 @@
import { useAdminProductCategory, useAdminProductCategories } from "../../../../src"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../../mocks/data"
import { createWrapper } from "../../../utils"
describe("useAdminProductCategories hook", () => {
test("returns a list of categories", async () => {
const categories = fixtures.list("product_category")
const { result, waitFor } = renderHook(() => useAdminProductCategories(), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.product_categories).toEqual(categories)
})
})
describe("useAdminProductCategory hook", () => {
test("returns a category", async () => {
const category = fixtures.get("product_category")
const { result, waitFor } = renderHook(
() => useAdminProductCategory(category.id),
{
wrapper: createWrapper(),
}
)
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.product_category).toEqual(category)
})
})