feat(medusa): variants expand inventory_items (#4203)
* add expand params for inventory items to product and variant endpoints in store * add changeset * update integration test naming * make priceSeelctionParams extends findParams and adjust api accordingly
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
const path = require("path")
|
||||
|
||||
const { bootstrapApp } = require("../../../../helpers/bootstrap-app")
|
||||
const { initDb, useDb } = require("../../../../helpers/use-db")
|
||||
const { setPort, useApi } = require("../../../../helpers/use-api")
|
||||
|
||||
const {
|
||||
ProductVariantInventoryService,
|
||||
ProductVariantService,
|
||||
} = require("@medusajs/medusa")
|
||||
|
||||
const adminSeeder = require("../../../helpers/admin-seeder")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
const { simpleProductFactory } = require("../../../factories")
|
||||
const { simpleSalesChannelFactory } = require("../../../../api/factories")
|
||||
|
||||
const adminHeaders = { headers: { Authorization: "Bearer test_token" } }
|
||||
|
||||
describe("Get products", () => {
|
||||
let appContainer
|
||||
let dbConnection
|
||||
let express
|
||||
const productId = "test-product"
|
||||
const variantId = "test-variant"
|
||||
let invItem
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd })
|
||||
const { container, app, port } = await bootstrapApp({ cwd })
|
||||
appContainer = container
|
||||
|
||||
setPort(port)
|
||||
express = app.listen(port, (err) => {
|
||||
process.send(port)
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
express.close()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
jest.clearAllMocks()
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const productVariantInventoryService = appContainer.resolve(
|
||||
"productVariantInventoryService"
|
||||
)
|
||||
const inventoryService = appContainer.resolve("inventoryService")
|
||||
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: productId,
|
||||
status: "published",
|
||||
variants: [{ id: variantId }],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
invItem = await inventoryService.createInventoryItem({
|
||||
sku: "test-sku",
|
||||
})
|
||||
await productVariantInventoryService.attachInventoryItem(
|
||||
variantId,
|
||||
invItem.id
|
||||
)
|
||||
})
|
||||
|
||||
it("Expands inventory items when getting product with expand parameters", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const res = await api.get(
|
||||
`/store/products/${productId}?expand=variants,variants.inventory_items`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.data.product).toEqual(
|
||||
expect.objectContaining({
|
||||
id: productId,
|
||||
variants: [
|
||||
expect.objectContaining({
|
||||
id: variantId,
|
||||
inventory_items: [
|
||||
expect.objectContaining({
|
||||
inventory_item_id: invItem.id,
|
||||
variant_id: variantId,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({})
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,101 @@
|
||||
const path = require("path")
|
||||
|
||||
const { bootstrapApp } = require("../../../../helpers/bootstrap-app")
|
||||
const { initDb, useDb } = require("../../../../helpers/use-db")
|
||||
const { setPort, useApi } = require("../../../../helpers/use-api")
|
||||
|
||||
const {
|
||||
ProductVariantInventoryService,
|
||||
ProductVariantService,
|
||||
} = require("@medusajs/medusa")
|
||||
|
||||
const adminSeeder = require("../../../helpers/admin-seeder")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
const { simpleProductFactory } = require("../../../factories")
|
||||
const { simpleSalesChannelFactory } = require("../../../../api/factories")
|
||||
|
||||
const adminHeaders = { headers: { Authorization: "Bearer test_token" } }
|
||||
|
||||
describe("Get variant", () => {
|
||||
let appContainer
|
||||
let dbConnection
|
||||
let express
|
||||
const productId = "test-product"
|
||||
const variantId = "test-variant"
|
||||
let invItem
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd })
|
||||
const { container, app, port } = await bootstrapApp({ cwd })
|
||||
appContainer = container
|
||||
|
||||
setPort(port)
|
||||
express = app.listen(port, (err) => {
|
||||
process.send(port)
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
express.close()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
jest.clearAllMocks()
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const productVariantInventoryService = appContainer.resolve(
|
||||
"productVariantInventoryService"
|
||||
)
|
||||
const inventoryService = appContainer.resolve("inventoryService")
|
||||
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: productId,
|
||||
status: "published",
|
||||
variants: [{ id: variantId }],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
invItem = await inventoryService.createInventoryItem({
|
||||
sku: "test-sku",
|
||||
})
|
||||
await productVariantInventoryService.attachInventoryItem(
|
||||
variantId,
|
||||
invItem.id
|
||||
)
|
||||
})
|
||||
|
||||
it("Expands inventory items when getting variant with expand parameters", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const res = await api.get(
|
||||
`/store/variants/${variantId}?expand=inventory_items`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.data.variant).toEqual(
|
||||
expect.objectContaining({
|
||||
id: variantId,
|
||||
inventory_items: [
|
||||
expect.objectContaining({
|
||||
inventory_item_id: invItem.id,
|
||||
variant_id: variantId,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -199,6 +199,27 @@ describe("Create Variant", () => {
|
||||
)
|
||||
})
|
||||
|
||||
it("includes inventory items when property is expanded", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const result = await api.get(
|
||||
`/store/products?expand=variants,variants.inventory_items`
|
||||
)
|
||||
|
||||
expect(result.status).toEqual(200)
|
||||
expect(result.data.products).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
variants: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
inventory_items: [expect.any(Object)],
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("lists location availability correctly for store", async () => {
|
||||
const api = useApi()
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ describe("List Variants", () => {
|
||||
|
||||
describe("Inventory Items", () => {
|
||||
const variantId = "test-variant"
|
||||
let invItem
|
||||
|
||||
beforeEach(async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
@@ -78,7 +79,7 @@ describe("List Variants", () => {
|
||||
location.id
|
||||
)
|
||||
|
||||
const invItem = await inventoryService.createInventoryItem({
|
||||
invItem = await inventoryService.createInventoryItem({
|
||||
sku: "test-sku",
|
||||
})
|
||||
const invItemId = invItem.id
|
||||
@@ -103,5 +104,28 @@ describe("List Variants", () => {
|
||||
expect.objectContaining({ id: variantId, inventory_quantity: 10 })
|
||||
)
|
||||
})
|
||||
|
||||
it("expands inventory_items when querying with expand parameter", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const listVariantsRes = await api.get(
|
||||
`/admin/variants?expand=inventory_items`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(listVariantsRes.status).toEqual(200)
|
||||
expect(listVariantsRes.data.variants.length).toEqual(1)
|
||||
expect(listVariantsRes.data.variants[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: variantId,
|
||||
inventory_items: [
|
||||
expect.objectContaining({
|
||||
inventory_item_id: invItem.id,
|
||||
variant_id: variantId,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user