fix(medusa): Add admin get product tests (#5480)
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
"medusa-react": patch
|
||||
"@medusajs/admin-ui": patch
|
||||
"@medusajs/admin": patch
|
||||
---
|
||||
|
||||
fix(medusa): admin get product should return prices when expected
|
||||
@@ -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)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
AdminGetProductParams,
|
||||
AdminGetProductsParams,
|
||||
AdminProductsListRes,
|
||||
AdminProductsListTagsRes,
|
||||
@@ -35,6 +36,7 @@ export const useAdminProducts = (
|
||||
|
||||
export const useAdminProduct = (
|
||||
id: string,
|
||||
query?: AdminGetProductParams,
|
||||
options?: UseQueryOptionsWrapper<
|
||||
Response<AdminProductsRes>,
|
||||
Error,
|
||||
@@ -44,7 +46,7 @@ export const useAdminProduct = (
|
||||
const { client } = useMedusa()
|
||||
const { data, ...rest } = useQuery(
|
||||
adminProductKeys.detail(id),
|
||||
() => client.admin.products.retrieve(id),
|
||||
() => client.admin.products.retrieve(id, query),
|
||||
options
|
||||
)
|
||||
return { ...data, ...rest } as const
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { PricingService, ProductService } from "../../../../services"
|
||||
|
||||
import IsolateProductDomainFeatureFlag from "../../../../loaders/feature-flags/isolate-product-domain"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import IsolateProductDomainFeatureFlag from "../../../../loaders/feature-flags/isolate-product-domain"
|
||||
import { PricingService, ProductService } from "../../../../services"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { defaultAdminProductRemoteQueryObject } from "./index"
|
||||
|
||||
/**
|
||||
@@ -114,3 +114,5 @@ async function getProductWithIsolatedProductModule(req, id, retrieveConfig) {
|
||||
|
||||
return product
|
||||
}
|
||||
|
||||
export class AdminGetProductParams extends FindParams {}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import "reflect-metadata"
|
||||
|
||||
import { Product, ProductTag, ProductType, ProductVariant } from "../../../.."
|
||||
import { FindParams, PaginatedResponse } from "../../../../types/common"
|
||||
import { PaginatedResponse } from "../../../../types/common"
|
||||
import middlewares, { transformQuery } from "../../../middlewares"
|
||||
|
||||
import { FlagRouter } from "@medusajs/utils"
|
||||
import { Router } from "express"
|
||||
import { PricedProduct } from "../../../../types/pricing"
|
||||
import { validateSalesChannelsExist } from "../../../middlewares/validators/sales-channel-existence"
|
||||
import { AdminGetProductParams } from "./get-product"
|
||||
import { AdminGetProductsParams } from "./list-products"
|
||||
|
||||
const route = Router()
|
||||
@@ -76,7 +77,7 @@ export default (app, featureFlagRouter: FlagRouter) => {
|
||||
)
|
||||
route.get(
|
||||
"/:id",
|
||||
transformQuery(FindParams, {
|
||||
transformQuery(AdminGetProductParams, {
|
||||
defaultRelations: defaultAdminProductRelations,
|
||||
defaultFields: defaultAdminProductFields,
|
||||
isList: false,
|
||||
@@ -496,3 +497,4 @@ export * from "./set-metadata"
|
||||
export * from "./update-option"
|
||||
export * from "./update-product"
|
||||
export * from "./update-variant"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user