Feat: add product default relations support for variant prices (#359)

Co-authored-by: olivermrbl <oliver@mrbltech.com>
This commit is contained in:
Vilfred Sikker Dreijer
2021-09-10 14:53:12 +02:00
committed by GitHub
co-authored by olivermrbl
parent cf66f97758
commit 682741041a
17 changed files with 1311 additions and 453 deletions
@@ -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)
})
})
})