chore: Remove redundant product tests, add missing to http layer (#7543)

This commit is contained in:
Stevche Radevski
2024-05-30 11:26:50 +00:00
committed by GitHub
parent 8a5d9d04a3
commit eeb8225b5e
6 changed files with 66 additions and 2235 deletions
@@ -1,190 +0,0 @@
import { PricingModuleService } from "@medusajs/pricing"
import { ProductModuleService } from "@medusajs/product"
import {
simpleProductFactory,
simpleRegionFactory,
} from "../../../../factories"
import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createAdminUser } from "../../../../helpers/create-admin-user"
jest.setTimeout(50000)
const adminHeaders = {
headers: {
"x-medusa-access-token": "test_token",
},
}
const env = {
MEDUSA_FF_MEDUSA_V2: true,
}
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe.skip("POST /admin/products/:id/variants", () => {
let appContainer
let product
let variant
beforeAll(async () => {
appContainer = getContainer()
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
await createDefaultRuleTypes(appContainer)
await simpleRegionFactory(dbConnection, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
product = await simpleProductFactory(dbConnection, {
id: "test-product-with-variant",
variants: [
{
options: [{ option_id: "test-product-option-1", value: "test" }],
},
],
options: [
{
id: "test-product-option-1",
title: "Test option 1",
},
],
})
variant = product.variants[0]
})
it("should create a product variant with its price sets and prices through the workflow", async () => {
const data = {
title: "test variant create",
prices: [
{
amount: 66600,
region_id: "test-region",
},
{
amount: 55500,
currency_code: "usd",
region_id: null,
},
],
material: "boo",
mid_code: "234asdfadsf",
hs_code: "asdfasdf234",
origin_country: "DE",
sku: "asdf",
ean: "234",
upc: "234",
barcode: "asdf",
inventory_quantity: 234,
manage_inventory: true,
allow_backorder: true,
weight: 234,
width: 234,
height: 234,
length: 234,
metadata: { asdf: "asdf" },
options: [
{ option_id: "test-product-option-1", value: "test option" },
],
}
let response = await api.post(
`/admin/products/${product.id}/variants`,
data,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
id: expect.any(String),
variants: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
title: "test variant create",
prices: expect.arrayContaining([
expect.objectContaining({
amount: 66600,
currency_code: "usd",
region_id: "test-region",
}),
expect.objectContaining({
amount: 55500,
currency_code: "usd",
}),
]),
}),
]),
})
)
})
it("should compensate creating product variants when error throws in future step", async () => {
jest
.spyOn(PricingModuleService.prototype, "create")
.mockImplementation(() => {
throw new Error("Random Error")
})
const productSpy = jest.spyOn(
ProductModuleService.prototype,
"deleteVariants"
)
const data = {
title: "test variant create",
prices: [
{
amount: 66600,
region_id: "test-region",
},
{
amount: 55500,
currency_code: "usd",
region_id: null,
},
],
material: "boo",
mid_code: "234asdfadsf",
hs_code: "asdfasdf234",
origin_country: "DE",
sku: "asdf",
ean: "234",
upc: "234",
barcode: "asdf",
inventory_quantity: 234,
manage_inventory: true,
allow_backorder: true,
weight: 234,
width: 234,
height: 234,
length: 234,
metadata: { asdf: "asdf" },
options: [
{ option_id: "test-product-option-1", value: "test option" },
],
}
await api
.post(`/admin/products/${product.id}/variants`, data, adminHeaders)
.catch((e) => e)
expect(productSpy).toBeCalledWith([expect.any(String)])
const getProductResponse = await api.get(
`/admin/products/${product.id}`,
adminHeaders
)
expect(getProductResponse.data.product.variants).toHaveLength(1)
})
})
},
})
@@ -1,462 +0,0 @@
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist"
import { simpleSalesChannelFactory } from "../../../../factories"
import { createAdminUser } from "../../../../helpers/create-admin-user"
import productSeeder from "../../../../helpers/product-seeder"
import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types"
jest.setTimeout(50000)
const adminHeaders = {
headers: {
"x-medusa-access-token": "test_token",
},
}
const env = {
MEDUSA_FF_MEDUSA_V2: true,
}
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
// TODO: unskip this when there is a module compatible productSeeder
describe.skip("/admin/products", () => {
let medusaContainer
beforeAll(async () => {
medusaContainer = getContainer()
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, medusaContainer)
await productSeeder(dbConnection)
await createDefaultRuleTypes(medusaContainer)
await simpleSalesChannelFactory(dbConnection, {
name: "Default channel",
id: "default-channel",
is_default: true,
})
})
describe("POST /admin/products", () => {
it("should create a product", async () => {
const payload = {
title: "Test",
description: "test-product-description",
type: { value: "test-type" },
images: ["test-image.png", "test-image-2.png"],
collection_id: "test-collection",
tags: [{ value: "123" }, { value: "456" }],
// options: [{ title: "size" }, { title: "color" }],
variants: [
{
title: "Test variant",
inventory_quantity: 10,
prices: [
{
currency_code: "usd",
amount: 100,
},
{
currency_code: "eur",
amount: 45,
},
{
currency_code: "dkk",
amount: 30,
},
],
// options: [{ value: "large" }, { value: "green" }],
},
],
}
const response = await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
id: expect.stringMatching(/^prod_*/),
title: "Test",
discountable: true,
is_giftcard: false,
handle: "test",
status: "draft",
// profile_id: expect.stringMatching(/^sp_*/),
thumbnail: "test-image.png",
created_at: expect.any(String),
updated_at: expect.any(String),
})
)
expect(response?.data.product.images).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
url: "test-image.png",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
expect.objectContaining({
id: expect.any(String),
url: "test-image-2.png",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
])
)
expect(response?.data.product.variants).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.stringMatching(/^variant_*/),
title: "Test variant",
// product_id: expect.stringMatching(/^prod_*/),
updated_at: expect.any(String),
created_at: expect.any(String),
// prices: expect.arrayContaining([
// expect.objectContaining({
// id: expect.stringMatching(/^ma_*/),
// currency_code: "usd",
// amount: 100,
// // TODO: enable this in the Pricing Module PR
// // created_at: expect.any(String),
// // updated_at: expect.any(String),
// // variant_id: expect.stringMatching(/^variant_*/),
// }),
// expect.objectContaining({
// id: expect.stringMatching(/^ma_*/),
// currency_code: "eur",
// amount: 45,
// // TODO: enable this in the Pricing Module PR
// // created_at: expect.any(String),
// // updated_at: expect.any(String),
// // variant_id: expect.stringMatching(/^variant_*/),
// }),
// expect.objectContaining({
// id: expect.stringMatching(/^ma_*/),
// currency_code: "dkk",
// amount: 30,
// // TODO: enable this in the Pricing Module PR
// // created_at: expect.any(String),
// // updated_at: expect.any(String),
// // variant_id: expect.stringMatching(/^variant_*/),
// }),
// ]),
// options: expect.arrayContaining([
// expect.objectContaining({
// value: "large",
// created_at: expect.any(String),
// updated_at: expect.any(String),
// variant_id: expect.stringMatching(/^variant_*/),
// option_id: expect.stringMatching(/^opt_*/),
// id: expect.stringMatching(/^optval_*/),
// }),
// expect.objectContaining({
// value: "green",
// created_at: expect.any(String),
// updated_at: expect.any(String),
// variant_id: expect.stringMatching(/^variant_*/),
// option_id: expect.stringMatching(/^opt_*/),
// id: expect.stringMatching(/^optval_*/),
// }),
// ]),
}),
])
)
// expect(response?.data.product.options).toEqual(
// expect.arrayContaining([
// expect.objectContaining({
// id: expect.stringMatching(/^opt_*/),
// // product_id: expect.stringMatching(/^prod_*/),
// title: "size",
// created_at: expect.any(String),
// updated_at: expect.any(String),
// }),
// expect.objectContaining({
// id: expect.stringMatching(/^opt_*/),
// // product_id: expect.stringMatching(/^prod_*/),
// title: "color",
// created_at: expect.any(String),
// updated_at: expect.any(String),
// }),
// ])
// )
// tags: expect.arrayContaining([
// expect.objectContaining({
// id: expect.any(String),
// value: "123",
// created_at: expect.any(String),
// updated_at: expect.any(String),
// }),
// expect.objectContaining({
// id: expect.any(String),
// value: "456",
// created_at: expect.any(String),
// updated_at: expect.any(String),
// }),
// ]),
// type: expect.objectContaining({
// value: "test-type",
// created_at: expect.any(String),
// updated_at: expect.any(String),
// }),
// collection: expect.objectContaining({
// id: "test-collection",
// title: "Test collection",
// created_at: expect.any(String),
// updated_at: expect.any(String),
// }),
})
it("should create a product that is not discountable", async () => {
const payload = {
title: "Test",
discountable: false,
description: "test-product-description",
type: { value: "test-type" },
images: ["test-image.png", "test-image-2.png"],
collection_id: "test-collection",
tags: [{ value: "123" }, { value: "456" }],
// options: [{ title: "size" }, { title: "color" }],
variants: [
{
title: "Test variant",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
// options: [{ value: "large" }, { value: "green" }],
},
],
}
const response = await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
discountable: false,
})
)
})
it("should sets the variant ranks when creating a product", async () => {
const payload = {
title: "Test product - 1",
description: "test-product-description 1",
type: { value: "test-type 1" },
images: ["test-image.png", "test-image-2.png"],
collection_id: "test-collection",
tags: [{ value: "123" }, { value: "456" }],
// options: [{ title: "size" }, { title: "color" }],
variants: [
{
title: "Test variant 1",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
// options: [{ value: "large" }, { value: "green" }],
},
{
title: "Test variant 2",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
// options: [{ value: "large" }, { value: "green" }],
},
],
}
const creationResponse = await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(creationResponse?.status).toEqual(200)
const productId = creationResponse?.data.product.id
const response = await api
.get(
`/admin/products/${productId}?fields=title,variants.title`,
adminHeaders
)
.catch((err) => {
console.log(err)
})
expect(response?.data.product).toEqual(
expect.objectContaining({
title: "Test product - 1",
variants: [
expect.objectContaining({
title: "Test variant 1",
}),
expect.objectContaining({
title: "Test variant 2",
}),
],
})
)
})
it("should create a giftcard", async () => {
const payload = {
title: "Test Giftcard",
is_giftcard: true,
description: "test-giftcard-description",
// options: [{ title: "Denominations" }],
variants: [
{
title: "Test variant",
prices: [{ currency_code: "usd", amount: 100 }],
// options: [{ value: "100" }],
},
],
}
const response = await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
title: "Test Giftcard",
discountable: false,
})
)
})
it("should create variants with inventory items", async () => {
const response = await api.post(
`/admin/products`,
{
title: "Test product - 1",
description: "test-product-description 1",
type: { value: "test-type 1" },
images: ["test-image.png", "test-image-2.png"],
collection_id: "test-collection",
tags: [{ value: "123" }, { value: "456" }],
// options: [{ title: "size" }, { title: "color" }],
variants: [
{
title: "Test variant 1",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
// options: [{ value: "large" }, { value: "green" }],
},
{
title: "Test variant 2",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
// options: [{ value: "large" }, { value: "green" }],
},
],
},
adminHeaders
)
expect(response.status).toEqual(200)
// const variantIds = response.data.product.variants.map(
// (v: { id: string }) => v.id
// )
// const variantInventoryService = medusaContainer.resolve(
// "productVariantInventoryService"
// )
// const inventory = await variantInventoryService.listByVariant(variantIds)
// expect(inventory).toHaveLength(2)
// expect(inventory).toContainEqual(
// expect.objectContaining({
// variant_id: variantIds[0],
// required_quantity: 1,
// })
// )
// expect(inventory).toContainEqual(
// expect.objectContaining({
// variant_id: variantIds[1],
// required_quantity: 1,
// })
// )
})
// it("should create prices with region_id and currency_code context", async () => {
// const api = useApi()! as AxiosInstance
// const data = {
// title: "test product",
// options: [{ title: "test-option" }],
// variants: [
// {
// title: "test variant",
// prices: [
// {
// amount: 66600,
// region_id: "test-region",
// },
// {
// amount: 55500,
// currency_code: "usd",
// },
// ],
// options: [{ value: "test-option" }],
// },
// ],
// }
// let response = await api.post(
// "/admin/products?relations=variants.prices",
// data,
// adminHeaders
// )
// expect(response.status).toEqual(200)
// expect(response.data).toEqual({
// product: expect.objectContaining({
// id: expect.any(String),
// title: "test product",
// variants: expect.arrayContaining([
// expect.objectContaining({
// id: expect.any(String),
// title: "test variant",
// prices: expect.arrayContaining([
// expect.objectContaining({
// amount: 66600,
// currency_code: "usd",
// }),
// expect.objectContaining({
// amount: 55500,
// currency_code: "usd",
// }),
// ]),
// }),
// ]),
// }),
// })
// const pricingModuleService: IPricingModuleService = appContainer.resolve(
// "pricingModuleService"
// )
// const [_, count] = await pricingModuleService.listAndCount()
// expect(count).toEqual(1)
// })
})
})
},
})
@@ -1,656 +0,0 @@
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
import { MedusaV2Flag } from "@medusajs/utils"
import { AxiosInstance } from "axios"
import { getContainer } from "../../../../environment-helpers/use-container"
import {
simpleProductFactory,
simpleSalesChannelFactory,
} from "../../../../factories"
import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types"
import { createAdminUser } from "../../../../helpers/create-admin-user"
jest.setTimeout(50000)
const adminHeaders = {
headers: {
"x-medusa-access-token": "test_token",
},
}
const env = {
MEDUSA_FF_MEDUSA_V2: true,
}
// TODO SEE to use new test runner medusaIntegrationTestRunner({
// env,
// testSuite: ({ dbConnection, getContainer, api }) => {})
describe.skip("/admin/products", () => {
let dbConnection
let shutdownServer
let medusaContainer
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env })
shutdownServer = await startBootstrapApp({ cwd, env })
medusaContainer = getContainer()
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
it("Should have loaded the product module", function () {
const productRegistrationName =
ModulesDefinition[Modules.PRODUCT].registrationName
expect(
medusaContainer.hasRegistration(productRegistrationName)
).toBeTruthy()
})
it("Should have enabled workflows feature flag", function () {
const flagRouter = medusaContainer.resolve("featureFlagRouter")
const workflowsFlag = flagRouter.isFeatureEnabled(MedusaV2Flag.key)
expect(workflowsFlag).toBe(true)
})
describe("POST /admin/products", () => {
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, medusaContainer)
await createDefaultRuleTypes(medusaContainer)
await simpleSalesChannelFactory(dbConnection, {
name: "Default channel",
id: "default-channel",
is_default: true,
})
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should create a product", async () => {
const api = useApi()! as AxiosInstance
const payload = {
title: "Test",
description: "test-product-description",
type: { value: "test-type" },
images: ["test-image.png", "test-image-2.png"],
collection_id: "test-collection",
tags: [{ value: "123" }, { value: "456" }],
options: [{ title: "size" }, { title: "color" }],
variants: [
{
title: "Test variant",
inventory_quantity: 10,
prices: [
{
currency_code: "usd",
amount: 100,
},
{
currency_code: "eur",
amount: 45,
},
{
currency_code: "dkk",
amount: 30,
},
],
options: [{ value: "large" }, { value: "green" }],
},
],
}
const response = await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
id: expect.stringMatching(/^prod_*/),
title: "Test",
discountable: true,
is_giftcard: false,
handle: "test",
status: "draft",
created_at: expect.any(String),
updated_at: expect.any(String),
profile_id: expect.stringMatching(/^sp_*/),
images: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
url: "test-image.png",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
expect.objectContaining({
id: expect.any(String),
url: "test-image-2.png",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
]),
thumbnail: "test-image.png",
tags: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
value: "123",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
expect.objectContaining({
id: expect.any(String),
value: "456",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
]),
type: expect.objectContaining({
value: "test-type",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
collection: expect.objectContaining({
id: "test-collection",
title: "Test collection",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
options: expect.arrayContaining([
expect.objectContaining({
id: expect.stringMatching(/^opt_*/),
product_id: expect.stringMatching(/^prod_*/),
title: "size",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
expect.objectContaining({
id: expect.stringMatching(/^opt_*/),
product_id: expect.stringMatching(/^prod_*/),
title: "color",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
]),
variants: expect.arrayContaining([
expect.objectContaining({
id: expect.stringMatching(/^variant_*/),
product_id: expect.stringMatching(/^prod_*/),
updated_at: expect.any(String),
created_at: expect.any(String),
title: "Test variant",
prices: expect.arrayContaining([
expect.objectContaining({
id: expect.stringMatching(/^ma_*/),
currency_code: "usd",
amount: 100,
// TODO: enable this in the Pricing Module PR
// created_at: expect.any(String),
// updated_at: expect.any(String),
// variant_id: expect.stringMatching(/^variant_*/),
}),
expect.objectContaining({
id: expect.stringMatching(/^ma_*/),
currency_code: "eur",
amount: 45,
// TODO: enable this in the Pricing Module PR
// created_at: expect.any(String),
// updated_at: expect.any(String),
// variant_id: expect.stringMatching(/^variant_*/),
}),
expect.objectContaining({
id: expect.stringMatching(/^ma_*/),
currency_code: "dkk",
amount: 30,
// TODO: enable this in the Pricing Module PR
// created_at: expect.any(String),
// updated_at: expect.any(String),
// variant_id: expect.stringMatching(/^variant_*/),
}),
]),
options: expect.arrayContaining([
expect.objectContaining({
value: "large",
created_at: expect.any(String),
updated_at: expect.any(String),
variant_id: expect.stringMatching(/^variant_*/),
option_id: expect.stringMatching(/^opt_*/),
id: expect.stringMatching(/^optval_*/),
}),
expect.objectContaining({
value: "green",
created_at: expect.any(String),
updated_at: expect.any(String),
variant_id: expect.stringMatching(/^variant_*/),
option_id: expect.stringMatching(/^opt_*/),
id: expect.stringMatching(/^optval_*/),
}),
]),
}),
]),
})
)
})
it("should create a product that is not discountable", async () => {
const api = useApi()! as AxiosInstance
const payload = {
title: "Test",
discountable: false,
description: "test-product-description",
type: { value: "test-type" },
images: ["test-image.png", "test-image-2.png"],
collection_id: "test-collection",
tags: [{ value: "123" }, { value: "456" }],
options: [{ title: "size" }, { title: "color" }],
variants: [
{
title: "Test variant",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
options: [{ value: "large" }, { value: "green" }],
},
],
}
const response = await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
discountable: false,
})
)
})
it("should sets the variant ranks when creating a product", async () => {
const api = useApi()! as AxiosInstance
const payload = {
title: "Test product - 1",
description: "test-product-description 1",
type: { value: "test-type 1" },
images: ["test-image.png", "test-image-2.png"],
collection_id: "test-collection",
tags: [{ value: "123" }, { value: "456" }],
options: [{ title: "size" }, { title: "color" }],
variants: [
{
title: "Test variant 1",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
options: [{ value: "large" }, { value: "green" }],
},
{
title: "Test variant 2",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
options: [{ value: "large" }, { value: "green" }],
},
],
}
const creationResponse = await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(creationResponse?.status).toEqual(200)
const productId = creationResponse?.data.product.id
const response = await api
.get(`/admin/products/${productId}`, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response?.data.product).toEqual(
expect.objectContaining({
title: "Test product - 1",
variants: [
expect.objectContaining({
title: "Test variant 1",
}),
expect.objectContaining({
title: "Test variant 2",
}),
],
})
)
})
it("should create a giftcard", async () => {
const api = useApi()! as AxiosInstance
const payload = {
title: "Test Giftcard",
is_giftcard: true,
description: "test-giftcard-description",
options: [{ title: "Denominations" }],
variants: [
{
title: "Test variant",
prices: [{ currency_code: "usd", amount: 100 }],
options: [{ value: "100" }],
},
],
}
const response = await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
title: "Test Giftcard",
discountable: false,
})
)
})
it("should create variants with inventory items", async () => {
const api = useApi()! as AxiosInstance
const response = await api.post(
`/admin/products`,
{
title: "Test product - 1",
description: "test-product-description 1",
type: { value: "test-type 1" },
images: ["test-image.png", "test-image-2.png"],
collection_id: "test-collection",
tags: [{ value: "123" }, { value: "456" }],
options: [{ title: "size" }, { title: "color" }],
variants: [
{
title: "Test variant 1",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
options: [{ value: "large" }, { value: "green" }],
},
{
title: "Test variant 2",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
options: [{ value: "large" }, { value: "green" }],
},
],
},
{ headers: { "x-medusa-access-token": "test_token" } }
)
expect(response.status).toEqual(200)
const variantIds = response.data.product.variants.map(
(v: { id: string }) => v.id
)
const variantInventoryService = medusaContainer.resolve(
"productVariantInventoryService"
)
const inventory = await variantInventoryService.listByVariant(variantIds)
expect(inventory).toHaveLength(2)
expect(inventory).toContainEqual(
expect.objectContaining({
variant_id: variantIds[0],
required_quantity: 1,
})
)
expect(inventory).toContainEqual(
expect.objectContaining({
variant_id: variantIds[1],
required_quantity: 1,
})
)
})
})
describe("POST /admin/products/:id", () => {
const toUpdateWithSalesChannels = "to-update-with-sales-channels"
const toUpdateWithVariants = "to-update-with-variants"
const toUpdate = "to-update"
beforeEach(async () => {
await productSeeder(dbConnection)
await createAdminUser(dbConnection, adminHeaders, medusaContainer)
await createDefaultRuleTypes(medusaContainer)
await simpleSalesChannelFactory(dbConnection, {
name: "Default channel",
id: "default-channel",
is_default: true,
})
await simpleSalesChannelFactory(dbConnection, {
name: "Channel 3",
id: "channel-3",
is_default: true,
})
await simpleProductFactory(dbConnection, {
title: "To update product",
id: toUpdate,
})
await simpleProductFactory(dbConnection, {
title: "To update product with channels",
id: toUpdateWithSalesChannels,
sales_channels: [
{ name: "channel 1", id: "channel-1" },
{ name: "channel 2", id: "channel-2" },
],
})
await simpleSalesChannelFactory(dbConnection, {
name: "To be added",
id: "to-be-added",
})
await simpleProductFactory(dbConnection, {
title: "To update product with variants",
id: toUpdateWithVariants,
variants: [
{
id: "variant-1",
title: "Variant 1",
},
{
id: "variant-2",
title: "Variant 2",
},
],
})
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should do a basic product update", async () => {
const api = useApi()! as AxiosInstance
const payload = {
title: "New title",
description: "test-product-description",
}
const response = await api
.post(`/admin/products/${toUpdate}`, payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
id: toUpdate,
title: "New title",
description: "test-product-description",
})
)
})
it("should update product and also update a variant and create a variant", async () => {
const api = useApi()! as AxiosInstance
const payload = {
title: "New title",
description: "test-product-description",
variants: [
{
id: "variant-1",
title: "Variant 1 updated",
},
{
title: "Variant 3",
},
],
}
const response = await api
.post(`/admin/products/${toUpdateWithVariants}`, payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
id: toUpdateWithVariants,
title: "New title",
description: "test-product-description",
variants: expect.arrayContaining([
expect.objectContaining({
id: "variant-1",
title: "Variant 1 updated",
}),
expect.objectContaining({
title: "Variant 3",
}),
]),
})
)
})
it("should update product's sales channels", async () => {
const api = useApi()! as AxiosInstance
const payload = {
title: "New title",
description: "test-product-description",
sales_channels: [{ id: "channel-2" }, { id: "channel-3" }],
}
const response = await api
.post(
`/admin/products/${toUpdateWithSalesChannels}?expand=sales_channels`,
payload,
adminHeaders
)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
id: toUpdateWithSalesChannels,
sales_channels: [
expect.objectContaining({ id: "channel-2" }),
expect.objectContaining({ id: "channel-3" }),
],
})
)
})
it("should update inventory when variants are updated", async () => {
const api = useApi()! as AxiosInstance
const variantInventoryService = medusaContainer.resolve(
"productVariantInventoryService"
)
const payload = {
title: "New title",
description: "test-product-description",
variants: [
{
id: "variant-1",
title: "Variant 1 updated",
},
{
title: "Variant 3",
},
],
}
const response = await api
.post(`/admin/products/${toUpdateWithVariants}`, payload, adminHeaders)
.catch((err) => {
console.log(err)
})
let inventory = await variantInventoryService.listInventoryItemsByVariant(
"variant-2"
)
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
id: toUpdateWithVariants,
title: "New title",
description: "test-product-description",
variants: expect.arrayContaining([
expect.objectContaining({
id: "variant-1",
title: "Variant 1 updated",
}),
expect.objectContaining({
title: "Variant 3",
}),
]),
})
)
expect(inventory).toEqual([]) // no inventory items for removed variant
inventory = await variantInventoryService.listInventoryItemsByVariant(
response?.data.product.variants.find((v) => v.title === "Variant 3").id
)
expect(inventory).toEqual([
expect.objectContaining({ id: expect.any(String) }),
])
})
})
})
@@ -1,486 +0,0 @@
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import {
simpleProductFactory,
simpleRegionFactory,
} from "../../../../factories"
import { AxiosInstance } from "axios"
import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types"
import { createVariantPriceSet } from "../../../helpers/create-variant-price-set"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { createAdminUser } from "../../../../helpers/create-admin-user"
jest.setTimeout(50000)
const adminHeaders = {
headers: {
"x-medusa-access-token": "test_token",
},
}
const env = {
MEDUSA_FF_MEDUSA_V2: true,
}
// TODO SEE to use new test runner medusaIntegrationTestRunner({
// env,
// testSuite: ({ dbConnection, getContainer, api }) => {})
describe.skip("POST /admin/products/:id/variants/:id", () => {
let dbConnection
let appContainer
let shutdownServer
let product
let variant
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
await createDefaultRuleTypes(appContainer)
await simpleRegionFactory(dbConnection, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
product = await simpleProductFactory(dbConnection, {
id: "test-product-with-variant",
variants: [
{
options: [{ option_id: "test-product-option-1", value: "test" }],
},
{
options: [{ option_id: "test-product-option-1", value: "test 2" }],
},
],
options: [
{
id: "test-product-option-1",
title: "Test option 1",
},
],
})
variant = product.variants[0]
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should create product variant price sets and prices", async () => {
const api = useApi()! as AxiosInstance
const data = {
title: "test variant update",
prices: [
{
amount: 66600,
region_id: "test-region",
},
{
amount: 55500,
currency_code: "usd",
region_id: null,
},
],
}
let response = await api.post(
`/admin/products/${product.id}/variants/${variant.id}`,
data,
adminHeaders
)
response = await api.get(`/admin/products/${product.id}`, adminHeaders)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
id: expect.any(String),
variants: expect.arrayContaining([
expect.objectContaining({
id: variant.id,
title: "test variant update",
prices: expect.arrayContaining([
expect.objectContaining({
amount: 66600,
currency_code: "usd",
region_id: "test-region",
}),
expect.objectContaining({
amount: 55500,
currency_code: "usd",
}),
]),
}),
]),
})
)
})
it("should update money amounts if money amount id is present in prices", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [
{
amount: 3000,
currency_code: "usd",
},
],
})
const moneyAmountToUpdate = priceSet.money_amounts?.[0]
const api = useApi()! as AxiosInstance
const data = {
title: "test variant update",
prices: [
{
amount: 66600,
region_id: "test-region",
},
{
id: moneyAmountToUpdate?.id,
amount: 2222,
currency_code: "usd",
region_id: null,
},
],
}
let response = await api.post(
`/admin/products/${product.id}/variants/${variant.id}`,
data,
adminHeaders
)
response = await api.get(`/admin/products/${product.id}`, adminHeaders)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
id: expect.any(String),
variants: expect.arrayContaining([
expect.objectContaining({
id: variant.id,
title: "test variant update",
prices: expect.arrayContaining([
expect.objectContaining({
amount: 66600,
currency_code: "usd",
region_id: "test-region",
}),
expect.objectContaining({
id: moneyAmountToUpdate?.id,
amount: 2222,
currency_code: "usd",
}),
]),
}),
]),
})
)
})
it("should add prices if price set is already present", async () => {
await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [],
})
const api = useApi()! as AxiosInstance
const data = {
title: "test variant update",
prices: [
{
amount: 123,
region_id: "test-region",
},
{
amount: 456,
currency_code: "usd",
region_id: null,
},
],
}
let response = await api.post(
`/admin/products/${product.id}/variants/${variant.id}`,
data,
adminHeaders
)
response = await api.get(`/admin/products/${product.id}`, adminHeaders)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
id: expect.any(String),
variants: expect.arrayContaining([
expect.objectContaining({
id: variant.id,
title: "test variant update",
prices: expect.arrayContaining([
expect.objectContaining({
amount: 123,
currency_code: "usd",
region_id: "test-region",
}),
expect.objectContaining({
amount: 456,
currency_code: "usd",
}),
]),
}),
]),
})
)
})
it("should update variant option value", async () => {
const api = useApi()! as AxiosInstance
const data = {
options: [
{
option_id: "test-product-option-1",
value: "updated",
},
],
}
await api.post(
`/admin/products/${product.id}/variants/${variant.id}`,
data,
adminHeaders
)
const response = await api.get(
`/admin/products/${product.id}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
id: expect.any(String),
variants: expect.arrayContaining([
expect.objectContaining({
id: variant.id,
options: [
expect.objectContaining({
option_id: "test-product-option-1",
value: "updated",
}),
],
}),
expect.objectContaining({
id: product.variants[1].id,
options: [
expect.objectContaining({
option_id: "test-product-option-1",
value: "test 2",
}),
],
}),
]),
})
)
})
it("should update variant metadata", async () => {
const api = useApi()! as AxiosInstance
const data = {
metadata: {
test: "string",
},
}
await api.post(
`/admin/products/${product.id}/variants/${variant.id}`,
data,
adminHeaders
)
const response = await api.get(
`/admin/products/${product.id}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
id: expect.any(String),
variants: expect.arrayContaining([
expect.objectContaining({
id: variant.id,
metadata: {
test: "string",
},
}),
]),
})
)
})
it("should remove options not present in update", async () => {
const api = useApi()! as AxiosInstance
product = await simpleProductFactory(dbConnection, {
id: "test-product-with-multiple-options",
variants: [
{
options: [
{ option_id: "test-product-multi-option-1", value: "test" },
{ option_id: "test-product-multi-option-2", value: "test value" },
],
},
],
options: [
{
id: "test-product-multi-option-1",
title: "Test option 1",
},
{
id: "test-product-multi-option-2",
title: "Test option 2",
},
],
})
variant = product.variants[0]
const data = {
options: [
{
option_id: "test-product-multi-option-1",
value: "updated",
},
],
}
await api.post(
`/admin/products/${product.id}/variants/${variant.id}`,
data,
adminHeaders
)
const response = await api.get(
`/admin/products/${product.id}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
id: expect.any(String),
variants: [
expect.objectContaining({
id: variant.id,
options: [
expect.objectContaining({
option_id: "test-product-multi-option-1",
value: "updated",
}),
],
}),
],
})
)
})
it("should update several options in the same api call", async () => {
const api = useApi()! as AxiosInstance
product = await simpleProductFactory(dbConnection, {
id: "test-product-with-multiple-options",
variants: [
{
options: [
{ option_id: "test-product-multi-option-1", value: "test" },
{ option_id: "test-product-multi-option-2", value: "test value" },
],
},
],
options: [
{
id: "test-product-multi-option-1",
title: "Test option 1",
},
{
id: "test-product-multi-option-2",
title: "Test option 2",
},
],
})
variant = product.variants[0]
const data = {
options: [
{
option_id: "test-product-multi-option-1",
value: "updated",
},
{
option_id: "test-product-multi-option-2",
value: "updated 2",
},
],
}
await api.post(
`/admin/products/${product.id}/variants/${variant.id}`,
data,
adminHeaders
)
const response = await api.get(
`/admin/products/${product.id}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
id: expect.any(String),
variants: [
expect.objectContaining({
id: variant.id,
options: [
expect.objectContaining({
option_id: "test-product-multi-option-1",
value: "updated",
}),
expect.objectContaining({
option_id: "test-product-multi-option-2",
value: "updated 2",
}),
],
}),
],
})
)
})
})
@@ -1,440 +0,0 @@
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { simpleProductFactory } from "../../../../factories"
import { Region } from "@medusajs/medusa"
import { AxiosInstance } from "axios"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types"
import { createVariantPriceSet } from "../../../helpers/create-variant-price-set"
import { createAdminUser } from "../../../../helpers/create-admin-user"
jest.setTimeout(50000)
const adminHeaders = {
headers: {
"x-medusa-access-token": "test_token",
},
}
const env = {
MEDUSA_FF_MEDUSA_V2: true,
}
// TODO SEE to use new test runner medusaIntegrationTestRunner({
// env,
// testSuite: ({ dbConnection, getContainer, api }) => {})
describe.skip("POST /admin/products/:id", () => {
let dbConnection
let appContainer
let shutdownServer
let product
let variant
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
beforeEach(async () => {
const manager = dbConnection.manager
await createAdminUser(dbConnection, adminHeaders, appContainer)
await createDefaultRuleTypes(appContainer)
await manager.insert(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
product = await simpleProductFactory(dbConnection, {
id: "test-product-with-variant",
variants: [
{
options: [{ option_id: "test-product-option-1", value: "test" }],
},
],
options: [
{
id: "test-product-option-1",
title: "Test option 1",
},
],
})
variant = product.variants[0]
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should do a basic product update", async () => {
const api = useApi()! as AxiosInstance
const payload = {
title: "New title",
description: "test-product-description",
}
const response = await api
.post(`/admin/products/${product.id}`, payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
id: product.id,
title: "New title",
description: "test-product-description",
})
)
})
it("should update product and also update a variant and create a variant", async () => {
const api = useApi()! as AxiosInstance
const payload = {
title: "New title",
description: "test-product-description",
variants: [
{
id: "variant-1",
title: "Variant 1 updated",
},
{
title: "Variant 3",
},
],
}
const response = await api
.post(`/admin/products/${product.id}`, payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
id: product.id,
title: "New title",
description: "test-product-description",
variants: expect.arrayContaining([
expect.objectContaining({
id: "variant-1",
title: "Variant 1 updated",
}),
expect.objectContaining({
title: "Variant 3",
}),
]),
})
)
})
it("should update product's sales channels", async () => {
const api = useApi()! as AxiosInstance
const payload = {
title: "New title",
description: "test-product-description",
sales_channels: [{ id: "channel-2" }, { id: "channel-3" }],
}
const response = await api
.post(
`/admin/products/${product.id}?expand=sales_channels`,
payload,
adminHeaders
)
.catch((err) => {
console.log(err)
})
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
id: product.id,
sales_channels: [
expect.objectContaining({ id: "channel-2" }),
expect.objectContaining({ id: "channel-3" }),
],
})
)
})
it("should update inventory when variants are updated", async () => {
const api = useApi()! as AxiosInstance
const variantInventoryService = appContainer.resolve(
"productVariantInventoryService"
)
const payload = {
title: "New title",
description: "test-product-description",
variants: [
{
id: "variant-1",
title: "Variant 1 updated",
},
{
title: "Variant 3",
},
],
}
const response = await api
.post(`/admin/products/${product.id}`, payload, adminHeaders)
.catch((err) => {
console.log(err)
})
let inventory = await variantInventoryService.listInventoryItemsByVariant(
"variant-2"
)
expect(response?.status).toEqual(200)
expect(response?.data.product).toEqual(
expect.objectContaining({
id: product.id,
title: "New title",
description: "test-product-description",
variants: expect.arrayContaining([
expect.objectContaining({
id: "variant-1",
title: "Variant 1 updated",
}),
expect.objectContaining({
title: "Variant 3",
}),
]),
})
)
expect(inventory).toEqual([]) // no inventory items for removed variant
inventory = await variantInventoryService.listInventoryItemsByVariant(
response?.data.product.variants.find((v) => v.title === "Variant 3").id
)
expect(inventory).toEqual([
expect.objectContaining({ id: expect.any(String) }),
])
})
it("should update product variant price sets and prices", async () => {
const api = useApi() as any
const data = {
title: "test product update",
variants: [
{
id: variant.id,
title: "test variant update",
prices: [
{
amount: 66600,
region_id: "test-region",
},
{
amount: 55500,
currency_code: "usd",
region_id: null,
},
],
},
],
}
await api.post(`/admin/products/${product.id}`, data, adminHeaders)
const response = await api.get(
`/admin/products/${product.id}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.product.variants).toHaveLength(1)
expect(response.data.product).toEqual(
expect.objectContaining({
id: expect.any(String),
variants: [
expect.objectContaining({
id: variant.id,
title: "test variant update",
prices: expect.arrayContaining([
expect.objectContaining({
amount: 66600,
currency_code: "usd",
region_id: "test-region",
}),
expect.objectContaining({
amount: 55500,
currency_code: "usd",
}),
]),
}),
],
})
)
})
it("should update money amounts if money amount id is present in prices", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [
{
amount: 3000,
currency_code: "usd",
},
],
})
const moneyAmountToUpdate = priceSet.money_amounts?.[0]
const api = useApi() as any
const data = {
title: "test product update",
variants: [
{
id: variant.id,
title: "test variant update",
prices: [
{
amount: 66600,
region_id: "test-region",
},
{
id: moneyAmountToUpdate?.id,
amount: 2222,
currency_code: "usd",
region_id: null,
},
],
},
],
}
await api.post(`/admin/products/${product.id}`, data, adminHeaders)
const response = await api.get(
`/admin/products/${product.id}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
id: expect.any(String),
variants: expect.arrayContaining([
expect.objectContaining({
id: variant.id,
title: "test variant update",
prices: expect.arrayContaining([
expect.objectContaining({
amount: 66600,
currency_code: "usd",
region_id: "test-region",
}),
expect.objectContaining({
id: moneyAmountToUpdate?.id,
amount: 2222,
currency_code: "usd",
}),
]),
}),
]),
})
)
})
it("should add prices if price set is already present", async () => {
const remoteLink = appContainer.resolve("remoteLink")
const pricingModuleService = appContainer.resolve("pricingModuleService")
const priceSet = await pricingModuleService.create({
rules: [{ rule_attribute: "region_id" }],
prices: [],
})
await remoteLink.create({
productService: {
variant_id: variant.id,
},
pricingService: {
price_set_id: priceSet.id,
},
})
const api = useApi()! as AxiosInstance
const data = {
title: "test product update",
variants: [
{
id: variant.id,
title: "test variant update",
prices: [
{
amount: 123,
region_id: "test-region",
},
{
amount: 456,
currency_code: "usd",
region_id: null,
},
],
},
],
}
await api.post(`/admin/products/${product.id}`, data, adminHeaders)
const response = await api.get(
`/admin/products/${product.id}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
id: expect.any(String),
variants: expect.arrayContaining([
expect.objectContaining({
id: variant.id,
title: "test variant update",
prices: expect.arrayContaining([
expect.objectContaining({
amount: 123,
currency_code: "usd",
region_id: "test-region",
}),
expect.objectContaining({
amount: 456,
currency_code: "usd",
}),
]),
}),
]),
})
)
})
})