fix: Sort product categories based on rank (#7702)
This commit is contained in:
@@ -62,6 +62,19 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
).data.product_category
|
||||
|
||||
productCategoryChild1 = (
|
||||
await api.post(
|
||||
"/admin/product-categories",
|
||||
{
|
||||
name: "category child 1",
|
||||
parent_category_id: productCategoryChild.id,
|
||||
rank: 1,
|
||||
description: "category child 1",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product_category
|
||||
|
||||
productCategoryChild2 = (
|
||||
await api.post(
|
||||
"/admin/product-categories",
|
||||
@@ -94,6 +107,12 @@ medusaIntegrationTestRunner({
|
||||
name: productCategoryChild.name,
|
||||
handle: productCategoryChild.handle,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild1.id,
|
||||
name: productCategoryChild1.name,
|
||||
handle: productCategoryChild1.handle,
|
||||
category_children: [],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild2.id,
|
||||
name: productCategoryChild2.name,
|
||||
@@ -108,11 +127,57 @@ medusaIntegrationTestRunner({
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("gets a category with children sorted by rank", async () => {
|
||||
const path = `/admin/product-categories/${productCategoryChild.id}?include_descendants_tree=true`
|
||||
let response = await api.get(path, adminHeaders)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product_category).toEqual(
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
parent_category_id: productCategory.id,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild1.id,
|
||||
handle: productCategoryChild1.handle,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild2.id,
|
||||
handle: productCategoryChild2.handle,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
|
||||
await api.post(
|
||||
`/admin/product-categories/${productCategoryChild2.id}`,
|
||||
{ rank: 0 },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
response = await api.get(path, adminHeaders)
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product_category).toEqual(
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
parent_category_id: productCategory.id,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild2.id,
|
||||
handle: productCategoryChild2.handle,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild1.id,
|
||||
handle: productCategoryChild1.handle,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/product-categories", () => {
|
||||
// TODO/BREAKING: We don't support rank reordering upon creation in V2
|
||||
// New categories with the same parent are always added at the end of the "list"
|
||||
beforeEach(async () => {
|
||||
productCategoryParent = (
|
||||
await api.post(
|
||||
@@ -352,6 +417,125 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
})
|
||||
|
||||
it("gets list of product category with immediate children and parents", async () => {
|
||||
const path = `/admin/product-categories?include_descendants_tree=true`
|
||||
const newChild = (
|
||||
await api.post(
|
||||
"/admin/product-categories",
|
||||
{
|
||||
name: "cotton",
|
||||
parent_category_id: productCategory.id,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product_category
|
||||
|
||||
let response = await api.get(path, adminHeaders)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product_categories).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: productCategoryParent.id,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
handle: productCategory.handle,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
parent_category_id: productCategory.id,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild0.id,
|
||||
handle: productCategoryChild0.handle,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild1.id,
|
||||
handle: productCategoryChild1.handle,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild2.id,
|
||||
handle: productCategoryChild2.handle,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild3.id,
|
||||
handle: productCategoryChild3.handle,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: newChild.id,
|
||||
handle: newChild.handle,
|
||||
category_children: [],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
await api.post(
|
||||
`/admin/product-categories/${productCategoryChild2.id}`,
|
||||
{ rank: 0 },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
await api.post(
|
||||
`/admin/product-categories/${newChild.id}`,
|
||||
{ rank: 0 },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
response = await api.get(path, adminHeaders)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product_categories).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: productCategoryParent.id,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
handle: productCategory.handle,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: newChild.id,
|
||||
handle: newChild.handle,
|
||||
category_children: [],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
parent_category_id: productCategory.id,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild2.id,
|
||||
handle: productCategoryChild2.handle,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild0.id,
|
||||
handle: productCategoryChild0.handle,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild1.id,
|
||||
handle: productCategoryChild1.handle,
|
||||
}),
|
||||
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild3.id,
|
||||
handle: productCategoryChild3.handle,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("filters based on whitelisted attributes of the data model", async () => {
|
||||
const response = await api.get(
|
||||
`/admin/product-categories?is_internal=true&limit=7`,
|
||||
|
||||
Reference in New Issue
Block a user