feat(medusa): Get route for admin product categories API (#2961)

This commit is contained in:
Riqwan Thamir
2023-01-10 10:08:16 +01:00
committed by GitHub
parent 33b3e5f16d
commit 47d075351f
13 changed files with 393 additions and 2 deletions

View File

@@ -0,0 +1,114 @@
import path from "path"
import startServerWithEnvironment from "../../../helpers/start-server-with-environment"
import { useApi } from "../../../helpers/use-api"
import { useDb } from "../../../helpers/use-db"
import adminSeeder from "../../helpers/admin-seeder"
import { simpleProductCategoryFactory } from "../../factories"
jest.setTimeout(30000)
const adminHeaders = {
headers: {
Authorization: "Bearer test_token",
},
}
describe("/admin/product-categories", () => {
let medusaProcess
let dbConnection
let productCategory = null
let productCategoryChild = null
let productCategoryParent = null
let productCategoryChild2 = null
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true },
})
dbConnection = connection
medusaProcess = process
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
medusaProcess.kill()
})
describe("GET /admin/product-categories/:id", () => {
beforeEach(async () => {
await adminSeeder(dbConnection)
productCategoryParent = await simpleProductCategoryFactory(dbConnection, {
name: "category parent",
handle: "category-parent",
})
productCategory = await simpleProductCategoryFactory(dbConnection, {
name: "category",
handle: "category",
parent_category: productCategoryParent,
})
productCategoryChild = await simpleProductCategoryFactory(dbConnection, {
name: "category child",
handle: "category-child",
parent_category: productCategory,
})
productCategoryChild2 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 2",
handle: "category-child-2",
parent_category: productCategoryChild,
})
})
afterEach(async () => {
const db = useDb()
return await db.teardown()
})
it("gets product category with children tree and parent", async () => {
const api = useApi()
const response = await api.get(
`/admin/product-categories/${productCategory.id}`,
adminHeaders
)
expect(response.data.product_category).toEqual(
expect.objectContaining({
id: productCategory.id,
name: productCategory.name,
handle: productCategory.handle,
parent_category: expect.objectContaining({
id: productCategoryParent.id,
name: productCategoryParent.name,
handle: productCategoryParent.handle,
}),
category_children: [
expect.objectContaining({
id: productCategoryChild.id,
name: productCategoryChild.name,
handle: productCategoryChild.handle,
category_children: [
expect.objectContaining({
id: productCategoryChild2.id,
name: productCategoryChild2.name,
handle: productCategoryChild2.handle,
category_children: []
})
]
})
]
})
)
expect(response.status).toEqual(200)
})
})
})

View File

@@ -22,3 +22,4 @@ export * from "./simple-payment-collection-factory"
export * from "./simple-order-edit-factory"
export * from "./simple-order-item-change-factory"
export * from "./simple-customer-factory"
export * from "./simple-product-category-factory"

View File

@@ -0,0 +1,12 @@
import { Connection } from "typeorm"
import { ProductCategory } from "@medusajs/medusa"
export const simpleProductCategoryFactory = async (
connection: Connection,
data: Partial<ProductCategory> = {}
): Promise<ProductCategory> => {
const manager = connection.manager
const address = manager.create(ProductCategory, data)
return await manager.save(address)
}