fix(medusa): Add admin get product tests (#5480)

This commit is contained in:
Adrien de Peretti
2023-10-31 08:54:06 +01:00
committed by GitHub
parent c1b97050ab
commit a780b92b8d
5 changed files with 156 additions and 6 deletions
@@ -943,6 +943,142 @@ describe("/admin/products", () => {
})
})
describe("GET /admin/products/:id", () => {
const productId = "testing-get-product"
beforeEach(async () => {
await simpleProductFactory(dbConnection, {
id: productId,
variants: [
{
title: "Test variant",
prices: [
{
currency: "usd",
amount: 100,
},
],
},
],
})
await adminSeeder(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should get a product with default relations", async () => {
const api = useApi()
const res = await api
.get(`/admin/products/${productId}`, adminHeaders)
.catch((err) => {
console.log(err)
})
const keysInResponse = Object.keys(res.data.product)
expect(res.status).toEqual(200)
expect(res.data.product.id).toEqual(productId)
expect(keysInResponse).toEqual(
expect.arrayContaining([
// fields
"id",
"created_at",
"updated_at",
"deleted_at",
"title",
"subtitle",
"description",
"handle",
"is_giftcard",
"status",
"thumbnail",
"weight",
"length",
"height",
"width",
"hs_code",
"origin_country",
"mid_code",
"material",
"collection_id",
"type_id",
"discountable",
"external_id",
"metadata",
// relations
"categories",
"collection",
"images",
"options",
"profiles",
"profile",
"profile_id",
"sales_channels",
"tags",
"type",
"variants",
])
)
const variants = res.data.product.variants
const hasPrices = variants.some((variant) => !!variant.prices)
expect(hasPrices).toBe(true)
})
it("should get a product with prices", async () => {
const api = useApi()
const res = await api
.get(
`/admin/products/${productId}?expand=variants,variants.prices`,
adminHeaders
)
.catch((err) => {
console.log(err)
})
const { id, variants } = res.data.product
expect(id).toEqual(productId)
expect(variants[0].prices).toEqual(
expect.arrayContaining([
expect.objectContaining({
amount: 100,
currency_code: "usd",
}),
])
)
})
it("should get a product only with variants expanded", async () => {
const api = useApi()
const res = await api
.get(`/admin/products/${productId}?expand=variants`, adminHeaders)
.catch((err) => {
console.log(err)
})
const { id, variants } = res.data.product
expect(id).toEqual(productId)
expect(variants[0]).toEqual(
expect.objectContaining({
title: "Test variant",
})
)
// prices is one of many properties that should not be expanded
expect(variants[0].prices).toBeUndefined()
})
})
describe("POST /admin/products", () => {
beforeEach(async () => {
await productSeeder(dbConnection)