feat(medusa): Filter products by category params in store/admin (#3155)
What: Products can be filtered through the API by category parameters Why: To filter products by category How: - adds 2 params in admin/store route - updates repository to accept 2 new parameters RESOLVES CORE-1032 RESOLVES CORE-1033
This commit is contained in:
@@ -27,6 +27,8 @@ const { IdMap } = require("medusa-test-utils")
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const testProductId = "test-product"
|
||||
const testProduct1Id = "test-product1"
|
||||
const testProductFilteringId1 = "test-product_filtering_1"
|
||||
const adminHeaders = {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
@@ -444,6 +446,135 @@ describe("/admin/products", () => {
|
||||
}
|
||||
})
|
||||
|
||||
describe("Product Category filtering", () => {
|
||||
let categoryWithProduct, categoryWithoutProduct, nestedCategoryWithProduct, nested2CategoryWithProduct
|
||||
const nestedCategoryWithProductId = "nested-category-with-product-id"
|
||||
const nested2CategoryWithProductId = "nested2-category-with-product-id"
|
||||
const categoryWithProductId = "category-with-product-id"
|
||||
const categoryWithoutProductId = "category-without-product-id"
|
||||
|
||||
beforeEach(async () => {
|
||||
const manager = dbConnection.manager
|
||||
categoryWithProduct = await simpleProductCategoryFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: categoryWithProductId,
|
||||
name: "category with Product",
|
||||
products: [{ id: testProductId }],
|
||||
}
|
||||
)
|
||||
|
||||
nestedCategoryWithProduct = await simpleProductCategoryFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: nestedCategoryWithProductId,
|
||||
name: "nested category with Product1",
|
||||
parent_category: categoryWithProduct,
|
||||
products: [{ id: testProduct1Id }],
|
||||
}
|
||||
)
|
||||
|
||||
nested2CategoryWithProduct = await simpleProductCategoryFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: nested2CategoryWithProductId,
|
||||
name: "nested2 category with Product1",
|
||||
parent_category: nestedCategoryWithProduct,
|
||||
products: [{ id: testProductFilteringId1 }],
|
||||
}
|
||||
)
|
||||
|
||||
categoryWithoutProduct = await simpleProductCategoryFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: categoryWithoutProductId,
|
||||
name: "category without product",
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it("returns a list of products in product category without category children", async () => {
|
||||
const api = useApi()
|
||||
const params = `category_id[]=${categoryWithProductId}`
|
||||
const response = await api
|
||||
.get(
|
||||
`/admin/products?${params}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.products).toHaveLength(1)
|
||||
expect(response.data.products).toEqual(
|
||||
[
|
||||
expect.objectContaining({
|
||||
id: testProductId,
|
||||
}),
|
||||
]
|
||||
)
|
||||
})
|
||||
|
||||
it("returns a list of products in product category without category children explicitly set to false", async () => {
|
||||
const api = useApi()
|
||||
const params = `category_id[]=${categoryWithProductId}&include_category_children=false`
|
||||
const response = await api
|
||||
.get(
|
||||
`/admin/products?${params}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.products).toHaveLength(1)
|
||||
expect(response.data.products).toEqual(
|
||||
[
|
||||
expect.objectContaining({
|
||||
id: testProductId,
|
||||
}),
|
||||
]
|
||||
)
|
||||
})
|
||||
|
||||
it("returns a list of products in product category with category children", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const params = `category_id[]=${categoryWithProductId}&include_category_children=true`
|
||||
const response = await api
|
||||
.get(
|
||||
`/admin/products?${params}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.products).toHaveLength(3)
|
||||
expect(response.data.products).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: testProduct1Id,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: testProductId,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: testProductFilteringId1,
|
||||
})
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("returns no products when product category with category children does not have products", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const params = `category_id[]=${categoryWithoutProductId}&include_category_children=true`
|
||||
const response = await api
|
||||
.get(
|
||||
`/admin/products?${params}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.products).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("returns a list of products with tags", async () => {
|
||||
const api = useApi()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user