feat(medusa, admin-ui): increase tree depth + scope categories on store + allow categories relation in products API (#3450)
What: - increase tree depth in react nestable - scope categories on store queries - allow categories relation in products API RESOLVES CORE-1238 RESOLVES CORE-1237 RESOLVES CORE-1236
This commit is contained in:
@@ -6,7 +6,6 @@ const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
const productSeeder = require("../../helpers/product-seeder")
|
||||
const { ProductCategory } = require("@medusajs/medusa")
|
||||
|
||||
const {
|
||||
ProductVariant,
|
||||
@@ -19,7 +18,6 @@ const priceListSeeder = require("../../helpers/price-list-seeder")
|
||||
const {
|
||||
simpleProductFactory,
|
||||
simpleDiscountFactory,
|
||||
simpleProductCategoryFactory,
|
||||
simpleSalesChannelFactory,
|
||||
simpleRegionFactory,
|
||||
} = require("../../factories")
|
||||
@@ -46,6 +44,7 @@ describe("/admin/products", () => {
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({
|
||||
cwd,
|
||||
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true }
|
||||
})
|
||||
})
|
||||
|
||||
@@ -454,127 +453,6 @@ describe("/admin/products", () => {
|
||||
}
|
||||
})
|
||||
|
||||
describe("Product Category filtering", () => {
|
||||
let categoryWithProduct
|
||||
let categoryWithoutProduct
|
||||
let nestedCategoryWithProduct
|
||||
let 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()
|
||||
|
||||
@@ -1573,7 +1451,6 @@ describe("/admin/products", () => {
|
||||
],
|
||||
type: null,
|
||||
collection: null,
|
||||
categories: [],
|
||||
})
|
||||
)
|
||||
})
|
||||
@@ -1604,152 +1481,6 @@ describe("/admin/products", () => {
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
describe("Categories", () => {
|
||||
let categoryWithProduct
|
||||
let categoryWithoutProduct
|
||||
const categoryWithProductId = "category-with-product-id"
|
||||
const categoryWithoutProductId = "category-without-product-id"
|
||||
|
||||
beforeEach(async () => {
|
||||
const manager = dbConnection.manager
|
||||
categoryWithProduct = await manager.create(ProductCategory, {
|
||||
id: categoryWithProductId,
|
||||
name: "category with Product",
|
||||
products: [{ id: testProductId }],
|
||||
})
|
||||
await manager.save(categoryWithProduct)
|
||||
|
||||
categoryWithoutProduct = await manager.create(ProductCategory, {
|
||||
id: categoryWithoutProductId,
|
||||
name: "category without product",
|
||||
})
|
||||
await manager.save(categoryWithoutProduct)
|
||||
})
|
||||
|
||||
it("creates a product with categories associated to it", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const payload = {
|
||||
title: "Test",
|
||||
description: "test-product-description",
|
||||
categories: [
|
||||
{ id: categoryWithProductId },
|
||||
{ id: categoryWithoutProductId },
|
||||
],
|
||||
}
|
||||
|
||||
const response = await api
|
||||
.post("/admin/products", payload, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product).toEqual(
|
||||
expect.objectContaining({
|
||||
categories: [
|
||||
expect.objectContaining({
|
||||
id: categoryWithProductId,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: categoryWithoutProductId,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("throws error when creating a product with invalid category ID", async () => {
|
||||
const api = useApi()
|
||||
const categoryNotFoundId = "category-doesnt-exist"
|
||||
|
||||
const payload = {
|
||||
title: "Test",
|
||||
description: "test-product-description",
|
||||
categories: [{ id: categoryNotFoundId }],
|
||||
}
|
||||
|
||||
const error = await api
|
||||
.post("/admin/products", payload, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(404)
|
||||
expect(error.response.data.type).toEqual("not_found")
|
||||
expect(error.response.data.message).toEqual(
|
||||
`Product_category with product_category_id ${categoryNotFoundId} does not exist.`
|
||||
)
|
||||
})
|
||||
|
||||
it("updates a product's categories", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const payload = {
|
||||
categories: [{ id: categoryWithoutProductId }],
|
||||
}
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/products/${testProductId}`,
|
||||
payload,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product).toEqual(
|
||||
expect.objectContaining({
|
||||
id: testProductId,
|
||||
handle: "test-product",
|
||||
categories: [
|
||||
expect.objectContaining({
|
||||
id: categoryWithoutProductId,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("remove all categories of a product", async () => {
|
||||
const api = useApi()
|
||||
const category = await simpleProductCategoryFactory(dbConnection, {
|
||||
id: "existing-category",
|
||||
name: "existing category",
|
||||
products: [{ id: "test-product" }],
|
||||
})
|
||||
|
||||
const payload = {
|
||||
categories: [],
|
||||
}
|
||||
|
||||
const response = await api.post(
|
||||
"/admin/products/test-product",
|
||||
payload,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-product",
|
||||
categories: [],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("throws error if product categories input is incorreect", async () => {
|
||||
const api = useApi()
|
||||
const payload = {
|
||||
categories: [{ incorrect: "test-category-d2B" }],
|
||||
}
|
||||
|
||||
const error = await api
|
||||
.post("/admin/products/test-product", payload, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(400)
|
||||
expect(error.response.data.type).toEqual("invalid_data")
|
||||
expect(error.response.data.message).toEqual(
|
||||
"property incorrect should not exist, id must be a string"
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("DELETE /admin/products/:id/options/:option_id", () => {
|
||||
@@ -2032,6 +1763,7 @@ describe("/admin/products", () => {
|
||||
|
||||
it("successfully updates a variant's prices by replacing a price", async () => {
|
||||
const api = useApi()
|
||||
const variantId = "test-variant"
|
||||
const data = {
|
||||
prices: [
|
||||
{
|
||||
@@ -2043,7 +1775,7 @@ describe("/admin/products", () => {
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/admin/products/test-product/variants/test-variant",
|
||||
`/admin/products/test-product/variants/${variantId}`,
|
||||
data,
|
||||
adminHeaders
|
||||
)
|
||||
@@ -2052,9 +1784,9 @@ describe("/admin/products", () => {
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
expect(response.data.product.variants[0].prices.length).toEqual(1)
|
||||
expect(response.data.product.variants[0].prices).toEqual(
|
||||
const variant = response.data.product.variants.find(v => v.id === variantId)
|
||||
expect(variant.prices.length).toEqual(1)
|
||||
expect(variant.prices).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
amount: 4500,
|
||||
|
||||
@@ -0,0 +1,352 @@
|
||||
const path = require("path")
|
||||
const { ProductCategory } = require("@medusajs/medusa")
|
||||
const { DiscountRuleType, AllocationType } = require("@medusajs/medusa/dist")
|
||||
const { IdMap } = require("medusa-test-utils")
|
||||
const {
|
||||
ProductVariant,
|
||||
ProductOptionValue,
|
||||
MoneyAmount,
|
||||
DiscountConditionType,
|
||||
DiscountConditionOperator,
|
||||
} = require("@medusajs/medusa")
|
||||
|
||||
const setupServer = require("../../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../../helpers/use-db")
|
||||
const adminSeeder = require("../../../helpers/admin-seeder")
|
||||
const productSeeder = require("../../../helpers/product-seeder")
|
||||
const priceListSeeder = require("../../../helpers/price-list-seeder")
|
||||
const {
|
||||
simpleProductFactory,
|
||||
simpleDiscountFactory,
|
||||
simpleProductCategoryFactory,
|
||||
simpleSalesChannelFactory,
|
||||
simpleRegionFactory,
|
||||
} = require("../../../factories")
|
||||
|
||||
const testProductId = "test-product"
|
||||
const testProduct1Id = "test-product1"
|
||||
const testProductFilteringId1 = "test-product_filtering_1"
|
||||
const adminHeaders = {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
|
||||
describe("/admin/products [MEDUSA_FF_PRODUCT_CATEGORIES=true]", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
let categoryWithProduct
|
||||
let categoryWithoutProduct
|
||||
let nestedCategoryWithProduct
|
||||
let 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"
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({
|
||||
cwd,
|
||||
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true },
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
describe("GET /admin/products", () => {
|
||||
beforeEach(async () => {
|
||||
await productSeeder(dbConnection)
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
await simpleSalesChannelFactory(dbConnection, {
|
||||
name: "Default channel",
|
||||
id: "default-channel",
|
||||
is_default: true,
|
||||
})
|
||||
|
||||
const manager = dbConnection.manager
|
||||
categoryWithProduct = await simpleProductCategoryFactory(dbConnection, {
|
||||
id: categoryWithProductId,
|
||||
name: "category with Product",
|
||||
products: [{ id: testProductId }],
|
||||
is_active: false,
|
||||
is_internal: false,
|
||||
})
|
||||
|
||||
nestedCategoryWithProduct = await simpleProductCategoryFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: nestedCategoryWithProductId,
|
||||
name: "nested category with Product1",
|
||||
parent_category: categoryWithProduct,
|
||||
products: [{ id: testProduct1Id }],
|
||||
is_active: true,
|
||||
is_internal: true,
|
||||
}
|
||||
)
|
||||
|
||||
nested2CategoryWithProduct = await simpleProductCategoryFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: nested2CategoryWithProductId,
|
||||
name: "nested2 category with Product1",
|
||||
parent_category: nestedCategoryWithProduct,
|
||||
products: [{ id: testProductFilteringId1 }],
|
||||
is_active: false,
|
||||
is_internal: true,
|
||||
}
|
||||
)
|
||||
|
||||
categoryWithoutProduct = await simpleProductCategoryFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: categoryWithoutProductId,
|
||||
name: "category without product",
|
||||
is_active: true,
|
||||
is_internal: false,
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/products", () => {
|
||||
beforeEach(async () => {
|
||||
await productSeeder(dbConnection)
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
await simpleSalesChannelFactory(dbConnection, {
|
||||
name: "Default channel",
|
||||
id: "default-channel",
|
||||
is_default: true,
|
||||
})
|
||||
|
||||
const manager = dbConnection.manager
|
||||
categoryWithProduct = await manager.create(ProductCategory, {
|
||||
id: categoryWithProductId,
|
||||
name: "category with Product",
|
||||
products: [{ id: testProductId }],
|
||||
})
|
||||
await manager.save(categoryWithProduct)
|
||||
|
||||
categoryWithoutProduct = await manager.create(ProductCategory, {
|
||||
id: categoryWithoutProductId,
|
||||
name: "category without product",
|
||||
})
|
||||
await manager.save(categoryWithoutProduct)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("creates a product with categories associated to it", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const payload = {
|
||||
title: "Test",
|
||||
description: "test-product-description",
|
||||
categories: [
|
||||
{ id: categoryWithProductId },
|
||||
{ id: categoryWithoutProductId },
|
||||
],
|
||||
}
|
||||
|
||||
const response = await api
|
||||
.post("/admin/products", payload, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product).toEqual(
|
||||
expect.objectContaining({
|
||||
categories: [
|
||||
expect.objectContaining({
|
||||
id: categoryWithProductId,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: categoryWithoutProductId,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("throws error when creating a product with invalid category ID", async () => {
|
||||
const api = useApi()
|
||||
const categoryNotFoundId = "category-doesnt-exist"
|
||||
|
||||
const payload = {
|
||||
title: "Test",
|
||||
description: "test-product-description",
|
||||
categories: [{ id: categoryNotFoundId }],
|
||||
}
|
||||
|
||||
const error = await api
|
||||
.post("/admin/products", payload, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(404)
|
||||
expect(error.response.data.type).toEqual("not_found")
|
||||
expect(error.response.data.message).toEqual(
|
||||
`Product_category with product_category_id ${categoryNotFoundId} does not exist.`
|
||||
)
|
||||
})
|
||||
|
||||
it("updates a product's categories", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const payload = {
|
||||
categories: [{ id: categoryWithoutProductId }],
|
||||
}
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/products/${testProductId}`,
|
||||
payload,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product).toEqual(
|
||||
expect.objectContaining({
|
||||
id: testProductId,
|
||||
handle: "test-product",
|
||||
categories: [
|
||||
expect.objectContaining({
|
||||
id: categoryWithoutProductId,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("remove all categories of a product", async () => {
|
||||
const api = useApi()
|
||||
const category = await simpleProductCategoryFactory(dbConnection, {
|
||||
id: "existing-category",
|
||||
name: "existing category",
|
||||
products: [{ id: "test-product" }],
|
||||
})
|
||||
|
||||
const payload = {
|
||||
categories: [],
|
||||
}
|
||||
|
||||
const response = await api.post(
|
||||
"/admin/products/test-product",
|
||||
payload,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-product",
|
||||
categories: [],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("throws error if product categories input is incorreect", async () => {
|
||||
const api = useApi()
|
||||
const payload = {
|
||||
categories: [{ incorrect: "test-category-d2B" }],
|
||||
}
|
||||
|
||||
const error = await api
|
||||
.post("/admin/products/test-product", payload, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(400)
|
||||
expect(error.response.data.type).toEqual("invalid_data")
|
||||
expect(error.response.data.message).toEqual(
|
||||
"property incorrect should not exist, id must be a string"
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -473,115 +473,6 @@ describe("/store/products", () => {
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
describe("Product Category filtering", () => {
|
||||
let categoryWithProduct
|
||||
let categoryWithoutProduct
|
||||
let nestedCategoryWithProduct
|
||||
let 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: testProductId1 }],
|
||||
}
|
||||
)
|
||||
|
||||
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(`/store/products?${params}`)
|
||||
|
||||
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(`/store/products?${params}`)
|
||||
|
||||
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(`/store/products?${params}`)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.products).toHaveLength(3)
|
||||
expect(response.data.products).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: testProductId1,
|
||||
}),
|
||||
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(`/store/products?${params}`)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.products).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("list params", () => {
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
const path = require("path")
|
||||
const setupServer = require("../../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../../helpers/use-db")
|
||||
|
||||
const {
|
||||
simpleProductCategoryFactory,
|
||||
} = require("../../../factories")
|
||||
|
||||
const productSeeder = require("../../../helpers/store-product-seeder")
|
||||
const adminSeeder = require("../../../helpers/admin-seeder")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("/store/products", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
const testProductId = "test-product"
|
||||
const testProductId1 = "test-product1"
|
||||
const testProductFilteringId1 = "test-product_filtering_1"
|
||||
const testProductFilteringId2 = "test-product_filtering_2"
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({
|
||||
cwd,
|
||||
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true }
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
describe("GET /store/products [MEDUSA_FF_PRODUCT_CATEGORIES=true]", () => {
|
||||
let categoryWithProduct
|
||||
let categoryWithoutProduct
|
||||
let inactiveCategoryWithProduct
|
||||
let internalCategoryWithProduct
|
||||
let nestedCategoryWithProduct
|
||||
let 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"
|
||||
const inactiveCategoryWithProductId = "inactive-category-with-product-id"
|
||||
const internalCategoryWithProductId = "inactive-category-with-product-id"
|
||||
|
||||
beforeEach(async () => {
|
||||
const manager = dbConnection.manager
|
||||
|
||||
await productSeeder(dbConnection)
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
categoryWithProduct = await simpleProductCategoryFactory(dbConnection, {
|
||||
id: categoryWithProductId,
|
||||
name: "category with Product",
|
||||
products: [{ id: testProductId }],
|
||||
is_active: true,
|
||||
is_internal: false,
|
||||
})
|
||||
|
||||
nestedCategoryWithProduct = await simpleProductCategoryFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: nestedCategoryWithProductId,
|
||||
name: "nested category with Product1",
|
||||
parent_category: categoryWithProduct,
|
||||
products: [{ id: testProductId1 }],
|
||||
is_active: true,
|
||||
is_internal: false,
|
||||
}
|
||||
)
|
||||
|
||||
inactiveCategoryWithProduct = await simpleProductCategoryFactory(dbConnection, {
|
||||
id: inactiveCategoryWithProductId,
|
||||
name: "inactive category with Product",
|
||||
products: [{ id: testProductFilteringId2 }],
|
||||
parent_category: nestedCategoryWithProduct,
|
||||
is_active: false,
|
||||
is_internal: false,
|
||||
rank: 0,
|
||||
})
|
||||
|
||||
internalCategoryWithProduct = await simpleProductCategoryFactory(dbConnection, {
|
||||
id: inactiveCategoryWithProductId,
|
||||
name: "inactive category with Product",
|
||||
products: [{ id: testProductFilteringId2 }],
|
||||
parent_category: nestedCategoryWithProduct,
|
||||
is_active: true,
|
||||
is_internal: true,
|
||||
rank: 1,
|
||||
})
|
||||
|
||||
nested2CategoryWithProduct = await simpleProductCategoryFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: nested2CategoryWithProductId,
|
||||
name: "nested2 category with Product1",
|
||||
parent_category: nestedCategoryWithProduct,
|
||||
products: [{ id: testProductFilteringId1 }],
|
||||
is_active: true,
|
||||
is_internal: false,
|
||||
rank: 2,
|
||||
}
|
||||
)
|
||||
|
||||
categoryWithoutProduct = await simpleProductCategoryFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: categoryWithoutProductId,
|
||||
name: "category without product",
|
||||
is_active: true,
|
||||
is_internal: false,
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
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(`/store/products?${params}`)
|
||||
|
||||
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(`/store/products?${params}`)
|
||||
|
||||
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(`/store/products?${params}`)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.products).toHaveLength(3)
|
||||
expect(response.data.products).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: testProductId1,
|
||||
}),
|
||||
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(`/store/products?${params}`)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.products).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("returns only active and public products with include_category_children", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const params = `category_id[]=${nestedCategoryWithProductId}&include_category_children=true`
|
||||
const response = await api.get(`/store/products?${params}`)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.products).toHaveLength(2)
|
||||
|
||||
expect(response.data.products).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: testProductFilteringId1,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: testProductId1,
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("does not query products with category that are inactive", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const params = `category_id[]=${inactiveCategoryWithProductId}`
|
||||
const response = await api.get(`/store/products?${params}`)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.products).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("does not query products with category that are internal", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const params = `category_id[]=${internalCategoryWithProductId}`
|
||||
const response = await api.get(`/store/products?${params}`)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.products).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user