Feat: add product default relations support for variant prices (#359)
Co-authored-by: olivermrbl <oliver@mrbltech.com>
This commit is contained in:
1 parent
cf66f97758
commit
682741041a
17 files changed
+1311
-453
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import { request } from "../../../../../helpers/test-request"
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
|
||||
import { defaultRelations } from ".."
|
||||
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
|
||||
|
||||
describe("Get product by id", () => {
|
||||
describe("get product by id successfull", () => {
|
||||
@@ -20,7 +22,7 @@ describe("Get product by id", () => {
|
||||
expect(ProductServiceMock.retrieve).toHaveBeenCalledTimes(1)
|
||||
expect(ProductServiceMock.retrieve).toHaveBeenCalledWith(
|
||||
IdMap.getId("product1"),
|
||||
{ relations: ["images", "variants", "options"] }
|
||||
{ relations: defaultRelations }
|
||||
)
|
||||
})
|
||||
|
||||
@@ -28,4 +30,37 @@ describe("Get product by id", () => {
|
||||
expect(subject.body.product.id).toEqual(IdMap.getId("product1"))
|
||||
})
|
||||
})
|
||||
|
||||
describe("Query products with relations", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
subject = await request(
|
||||
"GET",
|
||||
`/store/products/${IdMap.getId("variantsWithPrices")}`
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("calls retrieve() once", () => {
|
||||
expect(ProductServiceMock.retrieve).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("endpoint called with defaultRelations", () => {
|
||||
expect(ProductServiceMock.retrieve).toHaveBeenCalledWith(
|
||||
IdMap.getId("variantsWithPrices"),
|
||||
{ relations: defaultRelations }
|
||||
)
|
||||
})
|
||||
|
||||
it("returns product with variant prices", () => {
|
||||
expect(
|
||||
subject.body.product.variants.some(variant => variant.prices)
|
||||
).toEqual(true)
|
||||
expect(subject.body.product.variants[0].prices[0].amount).toEqual(100)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,5 @@
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
import { defaultRelations } from ".."
|
||||
import { request } from "../../../../../helpers/test-request"
|
||||
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
|
||||
|
||||
@@ -18,7 +19,7 @@ describe("GET /store/products", () => {
|
||||
expect(ProductServiceMock.list).toHaveBeenCalledTimes(1)
|
||||
expect(ProductServiceMock.list).toHaveBeenCalledWith(
|
||||
{},
|
||||
{ relations: ["variants", "options", "images"], skip: 0, take: 100 }
|
||||
{ relations: defaultRelations, skip: 0, take: 100 }
|
||||
)
|
||||
})
|
||||
|
||||
@@ -43,7 +44,7 @@ describe("GET /store/products", () => {
|
||||
expect(ProductServiceMock.list).toHaveBeenCalledTimes(1)
|
||||
expect(ProductServiceMock.list).toHaveBeenCalledWith(
|
||||
{ is_giftcard: true },
|
||||
{ relations: ["variants", "options", "images"], skip: 0, take: 100 }
|
||||
{ relations: defaultRelations, skip: 0, take: 100 }
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { defaultRelations } from "."
|
||||
|
||||
/**
|
||||
* @oas [get] /products/{id}
|
||||
* operationId: GetProductsProduct
|
||||
@@ -22,7 +24,7 @@ export default async (req, res) => {
|
||||
|
||||
const productService = req.scope.resolve("productService")
|
||||
let product = await productService.retrieve(id, {
|
||||
relations: ["images", "variants", "options"],
|
||||
relations: defaultRelations,
|
||||
})
|
||||
|
||||
res.json({ product })
|
||||
|
||||
@@ -11,3 +11,14 @@ export default app => {
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
export const defaultRelations = [
|
||||
"variants",
|
||||
"variants.prices",
|
||||
"options",
|
||||
"options.values",
|
||||
"images",
|
||||
"tags",
|
||||
"collection",
|
||||
"type",
|
||||
]
|
||||
@@ -1,3 +1,5 @@
|
||||
import { defaultRelations } from "."
|
||||
|
||||
/**
|
||||
* @oas [get] /products
|
||||
* operationId: GetProducts
|
||||
@@ -40,7 +42,7 @@ export default async (req, res) => {
|
||||
}
|
||||
|
||||
const listConfig = {
|
||||
relations: ["variants", "options", "images"],
|
||||
relations: defaultRelations,
|
||||
skip: offset,
|
||||
take: limit,
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { IdMap } from "../../../../../../../medusa-test-utils/dist"
|
||||
import { request } from "../../../../../helpers/test-request"
|
||||
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
|
||||
|
||||
@@ -23,4 +24,18 @@ describe("Get variant by id", () => {
|
||||
expect(subject.body.variant.id).toEqual("1")
|
||||
})
|
||||
})
|
||||
|
||||
describe("get variant with prices", () => {
|
||||
let subject
|
||||
beforeAll(async () => {
|
||||
subject = await request(
|
||||
"GET",
|
||||
`/store/variants/${IdMap.getId("variantWithPrices")}`
|
||||
)
|
||||
})
|
||||
it("successfully retrieves variants with prices", async () => {
|
||||
expect(subject.status).toEqual(200)
|
||||
expect(subject.body.variant.prices[0].amount).toEqual(100)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -90,6 +90,35 @@ const invalidVariant = {
|
||||
],
|
||||
}
|
||||
|
||||
const variantWithPrices = {
|
||||
id: "variant_with_prices",
|
||||
title: "Variant with prices",
|
||||
prices: [
|
||||
{
|
||||
id: "price_1",
|
||||
currency_code: "usd",
|
||||
amount: 100,
|
||||
sale_amount: null,
|
||||
variant_id: "variant_with_prices",
|
||||
region_id: null,
|
||||
created_at: "2021-03-16T21:24:13.657Z",
|
||||
updated_at: "2021-03-16T21:24:13.657Z",
|
||||
deleted_at: null,
|
||||
},
|
||||
{
|
||||
id: "price_2",
|
||||
currency_code: "dk",
|
||||
amount: 100,
|
||||
sale_amount: null,
|
||||
variant_id: "variant_with_prices",
|
||||
region_id: null,
|
||||
created_at: "2021-03-16T21:24:13.657Z",
|
||||
updated_at: "2021-03-16T21:24:13.657Z",
|
||||
deleted_at: null,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const testVariant = {
|
||||
id: IdMap.getId("testVariant"),
|
||||
title: "test variant",
|
||||
@@ -161,6 +190,7 @@ export const variants = {
|
||||
eur10us12: eur10us12,
|
||||
testVariant: testVariant,
|
||||
giftCard: giftCardVar,
|
||||
variantWithPrices: variantWithPrices,
|
||||
}
|
||||
|
||||
export const ProductVariantServiceMock = {
|
||||
@@ -193,6 +223,9 @@ export const ProductVariantServiceMock = {
|
||||
if (variantId === "4") {
|
||||
return Promise.resolve(variant4)
|
||||
}
|
||||
if (variantId === IdMap.getId("variantWithPrices")) {
|
||||
return Promise.resolve(variantWithPrices)
|
||||
}
|
||||
if (variantId === IdMap.getId("validId")) {
|
||||
return Promise.resolve(variant5)
|
||||
}
|
||||
|
||||
@@ -26,6 +26,40 @@ export const products = {
|
||||
},
|
||||
],
|
||||
},
|
||||
variantsWithPrices: {
|
||||
id: IdMap.getId("variantsWithPrices"),
|
||||
title: "Variant with prices",
|
||||
variants: [
|
||||
{
|
||||
id: IdMap.getId("variant_with_prices"),
|
||||
title: "Variant with prices",
|
||||
prices: [
|
||||
{
|
||||
id: "price_1",
|
||||
currency_code: "usd",
|
||||
amount: 100,
|
||||
sale_amount: null,
|
||||
variant_id: "variant_with_prices",
|
||||
region_id: null,
|
||||
created_at: "2021-03-16T21:24:13.657Z",
|
||||
updated_at: "2021-03-16T21:24:13.657Z",
|
||||
deleted_at: null,
|
||||
},
|
||||
{
|
||||
id: "price_2",
|
||||
currency_code: "dk",
|
||||
amount: 100,
|
||||
sale_amount: null,
|
||||
variant_id: "variant_with_prices",
|
||||
region_id: null,
|
||||
created_at: "2021-03-16T21:24:13.657Z",
|
||||
updated_at: "2021-03-16T21:24:13.657Z",
|
||||
deleted_at: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
export const ProductServiceMock = {
|
||||
@@ -93,6 +127,9 @@ export const ProductServiceMock = {
|
||||
if (productId === IdMap.getId("productWithOptions")) {
|
||||
return Promise.resolve(products.productWithOptions)
|
||||
}
|
||||
if (productId === IdMap.getId("variantsWithPrices")) {
|
||||
return Promise.resolve(products.variantsWithPrices)
|
||||
}
|
||||
return Promise.resolve(undefined)
|
||||
}),
|
||||
update: jest.fn().mockImplementation((product, data) => {
|
||||
|
||||
Reference in new issue
Block a user