Merge pull request #373 from medusajs/feat/product-variant-rank

Feat: Add product variant rank
This commit is contained in:
pKorsholm
2021-09-09 09:58:27 +02:00
committed by GitHub
15 changed files with 1229 additions and 153 deletions
@@ -1,10 +1,80 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
import { ShippingProfileServiceMock } from "../../../../../services/__mocks__/shipping-profile"
describe("POST /admin/products", () => {
describe("successful creation", () => {
describe("successful creation with variants", () => {
let subject
beforeAll(async () => {
subject = await request("POST", "/admin/products", {
payload: {
title: "Test Product with variants",
description: "Test Description",
tags: [{ id: "test", value: "test" }],
handle: "test-product",
options: [{ title: "Test" }],
variants: [
{
title: "Test",
prices: [
{
currency_code: "USD",
amount: 100,
},
],
options: [
{
value: "100",
},
],
},
],
},
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
})
})
afterAll(async () => {
jest.clearAllMocks()
})
it("returns 200", () => {
expect(subject.status).toEqual(200)
})
it("assigns invokes productVariantService with ranked variants", () => {
expect(ProductVariantServiceMock.create).toHaveBeenCalledTimes(1)
expect(ProductVariantServiceMock.create).toHaveBeenCalledWith(
IdMap.getId("productWithOptions"),
{
title: "Test",
variant_rank: 0,
prices: [
{
currency_code: "USD",
amount: 100,
},
],
options: [
{
option_id: IdMap.getId("option1"),
value: "100",
},
],
inventory_quantity: 0,
}
)
})
})
describe("successful creation test", () => {
let subject
beforeAll(async () => {
@@ -14,6 +84,7 @@ describe("POST /admin/products", () => {
description: "Test Description",
tags: [{ id: "test", value: "test" }],
handle: "test-product",
options: [{ title: "Denominations" }],
},
adminSession: {
jwt: {
@@ -40,6 +111,7 @@ describe("POST /admin/products", () => {
tags: [{ id: "test", value: "test" }],
handle: "test-product",
is_giftcard: false,
options: [{ title: "Denominations" }],
profile_id: IdMap.getId("default_shipping_profile"),
})
})
@@ -328,6 +328,8 @@ export default async (req, res) => {
.create({ ...value, profile_id: shippingProfile.id })
if (variants) {
for (const [i, variant] of variants.entries()) variant.variant_rank = i
const optionIds = value.options.map(
o => newProduct.options.find(newO => newO.title === o.title).id
)
@@ -341,6 +343,7 @@ export default async (req, res) => {
option_id: optionIds[index],
})),
}
await productVariantService
.withTransaction(manager)
.create(newProduct.id, variant)