chore: Hide repository creation if they are not custom + add upsert support by default (#6127)
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { ProductCategory } from "@models"
|
||||
import { ProductCategoryRepository } from "@repositories"
|
||||
import { ProductCategoryService } from "@services"
|
||||
|
||||
import { createProductCategories } from "../../../__fixtures__/product-category"
|
||||
import { productCategoriesData, productCategoriesRankData } from "../../../__fixtures__/product-category/data"
|
||||
import {
|
||||
productCategoriesData,
|
||||
productCategoriesRankData,
|
||||
} from "../../../__fixtures__/product-category/data"
|
||||
import { TestDatabase } from "../../../utils"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -20,13 +25,12 @@ describe("Product category Service", () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
|
||||
const productCategoryRepository = new ProductCategoryRepository({
|
||||
manager: repositoryManager,
|
||||
})
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
|
||||
service = new ProductCategoryService<ProductCategory>({
|
||||
productCategoryRepository,
|
||||
})
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productCategoryService")
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -244,13 +248,11 @@ describe("Product category Service", () => {
|
||||
})
|
||||
|
||||
it("should return category for the given id", async () => {
|
||||
const productCategoryResults = await service.retrieve(
|
||||
categoryOneId,
|
||||
)
|
||||
const productCategoryResults = await service.retrieve(categoryOneId)
|
||||
|
||||
expect(productCategoryResults).toEqual(
|
||||
expect.objectContaining({
|
||||
id: categoryOneId
|
||||
id: categoryOneId,
|
||||
})
|
||||
)
|
||||
})
|
||||
@@ -264,7 +266,9 @@ describe("Product category Service", () => {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('ProductCategory with id: does-not-exist was not found')
|
||||
expect(error.message).toEqual(
|
||||
"ProductCategory with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an id is not provided", async () => {
|
||||
@@ -280,44 +284,38 @@ describe("Product category Service", () => {
|
||||
})
|
||||
|
||||
it("should return category based on config select param", async () => {
|
||||
const productCategoryResults = await service.retrieve(
|
||||
categoryOneId,
|
||||
{
|
||||
select: ["id", "parent_category_id"],
|
||||
}
|
||||
)
|
||||
const productCategoryResults = await service.retrieve(categoryOneId, {
|
||||
select: ["id", "parent_category_id"],
|
||||
})
|
||||
|
||||
expect(productCategoryResults).toEqual(
|
||||
expect.objectContaining({
|
||||
id: categoryOneId,
|
||||
id: categoryOneId,
|
||||
parent_category_id: "category-0",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return category based on config relation param", async () => {
|
||||
const productCategoryResults = await service.retrieve(
|
||||
categoryOneId,
|
||||
{
|
||||
select: ["id", "parent_category_id"],
|
||||
relations: ["parent_category"]
|
||||
}
|
||||
)
|
||||
const productCategoryResults = await service.retrieve(categoryOneId, {
|
||||
select: ["id", "parent_category_id"],
|
||||
relations: ["parent_category"],
|
||||
})
|
||||
|
||||
expect(productCategoryResults).toEqual(
|
||||
expect.objectContaining({
|
||||
id: categoryOneId,
|
||||
id: categoryOneId,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: 'category-1-a',
|
||||
id: "category-1-a",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: 'category-1-b',
|
||||
})
|
||||
id: "category-1-b",
|
||||
}),
|
||||
],
|
||||
parent_category: expect.objectContaining({
|
||||
id: "category-0"
|
||||
})
|
||||
id: "category-0",
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
@@ -352,7 +350,7 @@ describe("Product category Service", () => {
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
skip: 1
|
||||
skip: 1,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -377,7 +375,7 @@ describe("Product category Service", () => {
|
||||
expect(productCategoryResults[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "category-0",
|
||||
parent_category: null
|
||||
parent_category: null,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "category-1",
|
||||
@@ -568,11 +566,14 @@ describe("Product category Service", () => {
|
||||
parent_category_id: null,
|
||||
})
|
||||
|
||||
const [productCategory] = await service.list({
|
||||
name: "New Category"
|
||||
}, {
|
||||
select: ["name", "rank"]
|
||||
})
|
||||
const [productCategory] = await service.list(
|
||||
{
|
||||
name: "New Category",
|
||||
},
|
||||
{
|
||||
select: ["name", "rank"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productCategory).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -586,7 +587,7 @@ describe("Product category Service", () => {
|
||||
await service.create({
|
||||
name: "New Category",
|
||||
parent_category_id: null,
|
||||
rank: 0
|
||||
rank: 0,
|
||||
})
|
||||
|
||||
await service.create({
|
||||
@@ -594,11 +595,14 @@ describe("Product category Service", () => {
|
||||
parent_category_id: null,
|
||||
})
|
||||
|
||||
const [productCategoryNew] = await service.list({
|
||||
name: "New Category 2"
|
||||
}, {
|
||||
select: ["name", "rank"]
|
||||
})
|
||||
const [productCategoryNew] = await service.list(
|
||||
{
|
||||
name: "New Category 2",
|
||||
},
|
||||
{
|
||||
select: ["name", "rank"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productCategoryNew).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -612,11 +616,14 @@ describe("Product category Service", () => {
|
||||
parent_category_id: productCategoryNew.id,
|
||||
})
|
||||
|
||||
const [productCategoryWithParent] = await service.list({
|
||||
name: "New Category 2.1"
|
||||
}, {
|
||||
select: ["name", "rank", "parent_category_id"]
|
||||
})
|
||||
const [productCategoryWithParent] = await service.list(
|
||||
{
|
||||
name: "New Category 2.1",
|
||||
},
|
||||
{
|
||||
select: ["name", "rank", "parent_category_id"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productCategoryWithParent).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -655,11 +662,11 @@ describe("Product category Service", () => {
|
||||
|
||||
it("should update the name of the category successfully", async () => {
|
||||
await service.update(productCategoryZero.id, {
|
||||
name: "New Category"
|
||||
name: "New Category",
|
||||
})
|
||||
|
||||
const productCategory = await service.retrieve(productCategoryZero.id, {
|
||||
select: ["name"]
|
||||
select: ["name"],
|
||||
})
|
||||
|
||||
expect(productCategory.name).toEqual("New Category")
|
||||
@@ -670,13 +677,15 @@ describe("Product category Service", () => {
|
||||
|
||||
try {
|
||||
await service.update("does-not-exist", {
|
||||
name: "New Category"
|
||||
name: "New Category",
|
||||
})
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(`ProductCategory not found ({ id: 'does-not-exist' })`)
|
||||
expect(error.message).toEqual(
|
||||
`ProductCategory not found ({ id: 'does-not-exist' })`
|
||||
)
|
||||
})
|
||||
|
||||
it("should reorder rank successfully in the same parent", async () => {
|
||||
@@ -684,11 +693,14 @@ describe("Product category Service", () => {
|
||||
rank: 0,
|
||||
})
|
||||
|
||||
const productCategories = await service.list({
|
||||
parent_category_id: null
|
||||
}, {
|
||||
select: ["name", "rank"]
|
||||
})
|
||||
const productCategories = await service.list(
|
||||
{
|
||||
parent_category_id: null,
|
||||
},
|
||||
{
|
||||
select: ["name", "rank"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productCategories).toEqual(
|
||||
expect.arrayContaining([
|
||||
@@ -703,7 +715,7 @@ describe("Product category Service", () => {
|
||||
expect.objectContaining({
|
||||
id: productCategoryOne.id,
|
||||
rank: "2",
|
||||
})
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
@@ -711,14 +723,17 @@ describe("Product category Service", () => {
|
||||
it("should reorder rank successfully when changing parent", async () => {
|
||||
await service.update(productCategoryTwo.id, {
|
||||
rank: 0,
|
||||
parent_category_id: productCategoryZero.id
|
||||
parent_category_id: productCategoryZero.id,
|
||||
})
|
||||
|
||||
const productCategories = await service.list({
|
||||
parent_category_id: productCategoryZero.id
|
||||
}, {
|
||||
select: ["name", "rank"]
|
||||
})
|
||||
const productCategories = await service.list(
|
||||
{
|
||||
parent_category_id: productCategoryZero.id,
|
||||
},
|
||||
{
|
||||
select: ["name", "rank"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productCategories).toEqual(
|
||||
expect.arrayContaining([
|
||||
@@ -737,7 +752,7 @@ describe("Product category Service", () => {
|
||||
expect.objectContaining({
|
||||
id: productCategoryZeroTwo.id,
|
||||
rank: "3",
|
||||
})
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
@@ -745,14 +760,17 @@ describe("Product category Service", () => {
|
||||
it("should reorder rank successfully when changing parent and in first position", async () => {
|
||||
await service.update(productCategoryTwo.id, {
|
||||
rank: 0,
|
||||
parent_category_id: productCategoryZero.id
|
||||
parent_category_id: productCategoryZero.id,
|
||||
})
|
||||
|
||||
const productCategories = await service.list({
|
||||
parent_category_id: productCategoryZero.id
|
||||
}, {
|
||||
select: ["name", "rank"]
|
||||
})
|
||||
const productCategories = await service.list(
|
||||
{
|
||||
parent_category_id: productCategoryZero.id,
|
||||
},
|
||||
{
|
||||
select: ["name", "rank"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productCategories).toEqual(
|
||||
expect.arrayContaining([
|
||||
@@ -771,7 +789,7 @@ describe("Product category Service", () => {
|
||||
expect.objectContaining({
|
||||
id: productCategoryZeroTwo.id,
|
||||
rank: "3",
|
||||
})
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
@@ -805,7 +823,9 @@ describe("Product category Service", () => {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(`ProductCategory not found ({ id: 'does-not-exist' })`)
|
||||
expect(error.message).toEqual(
|
||||
`ProductCategory not found ({ id: 'does-not-exist' })`
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when it has children", async () => {
|
||||
@@ -817,17 +837,22 @@ describe("Product category Service", () => {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(`Deleting ProductCategory (category-0-0) with category children is not allowed`)
|
||||
expect(error.message).toEqual(
|
||||
`Deleting ProductCategory (category-0-0) with category children is not allowed`
|
||||
)
|
||||
})
|
||||
|
||||
it("should reorder siblings rank successfully on deleting", async () => {
|
||||
await service.delete(productCategoryOne.id)
|
||||
|
||||
const productCategories = await service.list({
|
||||
parent_category_id: null
|
||||
}, {
|
||||
select: ["id", "rank"]
|
||||
})
|
||||
const productCategories = await service.list(
|
||||
{
|
||||
parent_category_id: null,
|
||||
},
|
||||
{
|
||||
select: ["id", "rank"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productCategories).toEqual(
|
||||
expect.arrayContaining([
|
||||
@@ -838,7 +863,7 @@ describe("Product category Service", () => {
|
||||
expect.objectContaining({
|
||||
id: productCategoryTwo.id,
|
||||
rank: "1",
|
||||
})
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { ProductCollection } from "@models"
|
||||
import { ProductCollectionRepository } from "@repositories"
|
||||
import { ProductCollectionService } from "@services"
|
||||
|
||||
import { createCollections } from "../../../__fixtures__/product"
|
||||
import { TestDatabase } from "../../../utils"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -19,13 +21,12 @@ describe("Product collection Service", () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
|
||||
const productCollectionRepository = new ProductCollectionRepository({
|
||||
manager: repositoryManager,
|
||||
})
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
|
||||
service = new ProductCollectionService({
|
||||
productCollectionRepository,
|
||||
})
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productCollectionService")
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -93,7 +94,9 @@ describe("Product collection Service", () => {
|
||||
})
|
||||
|
||||
it("list product collections by title matching string", async () => {
|
||||
const productCollectionResults = await service.list({ title: "col 3 extra" })
|
||||
const productCollectionResults = await service.list({
|
||||
title: "col 3 extra",
|
||||
})
|
||||
|
||||
expect(productCollectionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
@@ -155,7 +158,9 @@ describe("Product collection Service", () => {
|
||||
})
|
||||
|
||||
it("should return count and collections based on filter data", async () => {
|
||||
const [productCollectionResults, count] = await service.listAndCount({ id: data![0].id })
|
||||
const [productCollectionResults, count] = await service.listAndCount({
|
||||
id: data![0].id,
|
||||
})
|
||||
const serialized = JSON.parse(JSON.stringify(productCollectionResults))
|
||||
|
||||
expect(count).toEqual(1)
|
||||
@@ -168,12 +173,15 @@ describe("Product collection Service", () => {
|
||||
})
|
||||
|
||||
it("should return count and collections based on config data", async () => {
|
||||
const [productCollectionResults, count] = await service.listAndCount({}, {
|
||||
relations: ['products'],
|
||||
select: ['title'],
|
||||
take: 1,
|
||||
skip: 1,
|
||||
})
|
||||
const [productCollectionResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
relations: ["products"],
|
||||
select: ["title"],
|
||||
take: 1,
|
||||
skip: 1,
|
||||
}
|
||||
)
|
||||
const serialized = JSON.parse(JSON.stringify(productCollectionResults))
|
||||
|
||||
expect(count).toEqual(4)
|
||||
@@ -181,7 +189,7 @@ describe("Product collection Service", () => {
|
||||
{
|
||||
id: "test-2",
|
||||
title: "col 2",
|
||||
products: []
|
||||
products: [],
|
||||
},
|
||||
])
|
||||
})
|
||||
@@ -200,13 +208,11 @@ describe("Product collection Service", () => {
|
||||
})
|
||||
|
||||
it("should return collection for the given id", async () => {
|
||||
const productCollectionResults = await service.retrieve(
|
||||
collectionData.id,
|
||||
)
|
||||
const productCollectionResults = await service.retrieve(collectionData.id)
|
||||
|
||||
expect(productCollectionResults).toEqual(
|
||||
expect.objectContaining({
|
||||
id: collectionData.id
|
||||
id: collectionData.id,
|
||||
})
|
||||
)
|
||||
})
|
||||
@@ -220,7 +226,9 @@ describe("Product collection Service", () => {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('ProductCollection with id: does-not-exist was not found')
|
||||
expect(error.message).toEqual(
|
||||
"ProductCollection with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an id is not provided", async () => {
|
||||
@@ -245,12 +253,10 @@ describe("Product collection Service", () => {
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(productCollectionResults))
|
||||
|
||||
expect(serialized).toEqual(
|
||||
{
|
||||
id: collectionData.id,
|
||||
title: collectionData.title,
|
||||
}
|
||||
)
|
||||
expect(serialized).toEqual({
|
||||
id: collectionData.id,
|
||||
title: collectionData.title,
|
||||
})
|
||||
})
|
||||
|
||||
it("should return collection based on config relation param", async () => {
|
||||
@@ -258,19 +264,17 @@ describe("Product collection Service", () => {
|
||||
collectionData.id,
|
||||
{
|
||||
select: ["id", "title"],
|
||||
relations: ["products"]
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(productCollectionResults))
|
||||
|
||||
expect(serialized).toEqual(
|
||||
{
|
||||
id: collectionData.id,
|
||||
title: collectionData.title,
|
||||
products: []
|
||||
}
|
||||
)
|
||||
expect(serialized).toEqual({
|
||||
id: collectionData.id,
|
||||
title: collectionData.title,
|
||||
products: [],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -288,12 +292,10 @@ describe("Product collection Service", () => {
|
||||
})
|
||||
|
||||
it("should delete the product collection given an ID successfully", async () => {
|
||||
await service.delete(
|
||||
[collectionId],
|
||||
)
|
||||
await service.delete([collectionId])
|
||||
|
||||
const collections = await service.list({
|
||||
id: collectionId
|
||||
id: collectionId,
|
||||
})
|
||||
|
||||
expect(collections).toHaveLength(0)
|
||||
@@ -314,12 +316,12 @@ describe("Product collection Service", () => {
|
||||
})
|
||||
|
||||
it("should update the value of the collection successfully", async () => {
|
||||
await service.update(
|
||||
[{
|
||||
await service.update([
|
||||
{
|
||||
id: collectionId,
|
||||
title: "New Collection"
|
||||
}]
|
||||
)
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
const productCollection = await service.retrieve(collectionId)
|
||||
|
||||
@@ -331,29 +333,31 @@ describe("Product collection Service", () => {
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
title: "New Collection"
|
||||
}
|
||||
])
|
||||
{
|
||||
id: "does-not-exist",
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('ProductCollection with id "does-not-exist" not found')
|
||||
expect(error.message).toEqual(
|
||||
'ProductCollection with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a collection successfully", async () => {
|
||||
await service.create(
|
||||
[{
|
||||
title: "New Collection"
|
||||
}]
|
||||
)
|
||||
await service.create([
|
||||
{
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
const [productCollection] = await service.list({
|
||||
title: "New Collection"
|
||||
title: "New Collection",
|
||||
})
|
||||
|
||||
expect(productCollection.title).toEqual("New Collection")
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { ProductOptionService } from "@services"
|
||||
import { ProductOptionRepository } from "@repositories"
|
||||
import { Product } from "@models"
|
||||
|
||||
import { TestDatabase } from "../../../utils"
|
||||
import { createOptions } from "../../../__fixtures__/product"
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { asValue } from "awilix"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -34,13 +36,12 @@ describe("ProductOption Service", () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
|
||||
const productOptionRepository = new ProductOptionRepository({
|
||||
manager: repositoryManager,
|
||||
})
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
|
||||
service = new ProductOptionService({
|
||||
productOptionRepository,
|
||||
})
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productOptionService")
|
||||
|
||||
testManager = await TestDatabase.forkManager()
|
||||
productOne = testManager.create(Product, productOneData)
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { Product } from "@models"
|
||||
import { ProductTagRepository } from "@repositories"
|
||||
import { ProductTagService } from "@services"
|
||||
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { createProductAndTags } from "../../../__fixtures__/product"
|
||||
import { TestDatabase } from "../../../utils"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -53,13 +55,12 @@ describe("ProductTag Service", () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
|
||||
const productTagRepository = new ProductTagRepository({
|
||||
manager: repositoryManager,
|
||||
})
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
|
||||
service = new ProductTagService({
|
||||
productTagRepository,
|
||||
})
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productTagService")
|
||||
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
@@ -143,7 +144,9 @@ describe("ProductTag Service", () => {
|
||||
})
|
||||
|
||||
it("should return product tags and count when filtered", async () => {
|
||||
const [tagsResults, count] = await service.listAndCount({ id: data[0].tags![0].id })
|
||||
const [tagsResults, count] = await service.listAndCount({
|
||||
id: data[0].tags![0].id,
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(tagsResults).toEqual([
|
||||
@@ -154,7 +157,10 @@ describe("ProductTag Service", () => {
|
||||
})
|
||||
|
||||
it("should return product tags and count when using skip and take", async () => {
|
||||
const [tagsResults, count] = await service.listAndCount({}, { skip: 1, take: 2 })
|
||||
const [tagsResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 1, take: 2 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(4)
|
||||
expect(tagsResults).toEqual([
|
||||
@@ -168,11 +174,14 @@ describe("ProductTag Service", () => {
|
||||
})
|
||||
|
||||
it("should return requested fields and relations", async () => {
|
||||
const [tagsResults, count] = await service.listAndCount({}, {
|
||||
take: 1,
|
||||
select: ["value", "products.id"],
|
||||
relations: ["products"]
|
||||
})
|
||||
const [tagsResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["value", "products.id"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(tagsResults))
|
||||
|
||||
@@ -180,9 +189,11 @@ describe("ProductTag Service", () => {
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-1",
|
||||
products: [{
|
||||
id: "test-1"
|
||||
}]
|
||||
products: [
|
||||
{
|
||||
id: "test-1",
|
||||
},
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
@@ -194,13 +205,11 @@ describe("ProductTag Service", () => {
|
||||
const productId = "test-1"
|
||||
|
||||
it("should return tag for the given id", async () => {
|
||||
const tag = await service.retrieve(
|
||||
tagId,
|
||||
)
|
||||
const tag = await service.retrieve(tagId)
|
||||
|
||||
expect(tag).toEqual(
|
||||
expect.objectContaining({
|
||||
id: tagId
|
||||
id: tagId,
|
||||
})
|
||||
)
|
||||
})
|
||||
@@ -214,7 +223,9 @@ describe("ProductTag Service", () => {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('ProductTag with id: does-not-exist was not found')
|
||||
expect(error.message).toEqual(
|
||||
"ProductTag with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an id is not provided", async () => {
|
||||
@@ -230,43 +241,35 @@ describe("ProductTag Service", () => {
|
||||
})
|
||||
|
||||
it("should return tag based on config select param", async () => {
|
||||
const tag = await service.retrieve(
|
||||
tagId,
|
||||
{
|
||||
select: ["id", "value"],
|
||||
}
|
||||
)
|
||||
const tag = await service.retrieve(tagId, {
|
||||
select: ["id", "value"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(tag))
|
||||
|
||||
expect(serialized).toEqual(
|
||||
{
|
||||
id: tagId,
|
||||
value: tagValue,
|
||||
}
|
||||
)
|
||||
expect(serialized).toEqual({
|
||||
id: tagId,
|
||||
value: tagValue,
|
||||
})
|
||||
})
|
||||
|
||||
it("should return tag based on config relation param", async () => {
|
||||
const tag = await service.retrieve(
|
||||
tagId,
|
||||
{
|
||||
select: ["id", "value", "products.id"],
|
||||
relations: ["products"]
|
||||
}
|
||||
)
|
||||
const tag = await service.retrieve(tagId, {
|
||||
select: ["id", "value", "products.id"],
|
||||
relations: ["products"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(tag))
|
||||
|
||||
expect(serialized).toEqual(
|
||||
{
|
||||
id: tagId,
|
||||
value: tagValue,
|
||||
products: [{
|
||||
id: productId
|
||||
}]
|
||||
}
|
||||
)
|
||||
expect(serialized).toEqual({
|
||||
id: tagId,
|
||||
value: tagValue,
|
||||
products: [
|
||||
{
|
||||
id: productId,
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -274,12 +277,10 @@ describe("ProductTag Service", () => {
|
||||
const tagId = "tag-1"
|
||||
|
||||
it("should delete the product tag given an ID successfully", async () => {
|
||||
await service.delete(
|
||||
[tagId],
|
||||
)
|
||||
await service.delete([tagId])
|
||||
|
||||
const tags = await service.list({
|
||||
id: tagId
|
||||
id: tagId,
|
||||
})
|
||||
|
||||
expect(tags).toHaveLength(0)
|
||||
@@ -290,12 +291,12 @@ describe("ProductTag Service", () => {
|
||||
const tagId = "tag-1"
|
||||
|
||||
it("should update the value of the tag successfully", async () => {
|
||||
await service.update(
|
||||
[{
|
||||
await service.update([
|
||||
{
|
||||
id: tagId,
|
||||
value: "UK"
|
||||
}]
|
||||
)
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productTag = await service.retrieve(tagId)
|
||||
|
||||
@@ -307,29 +308,31 @@ describe("ProductTag Service", () => {
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
value: "UK"
|
||||
}
|
||||
])
|
||||
{
|
||||
id: "does-not-exist",
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('ProductTag with id "does-not-exist" not found')
|
||||
expect(error.message).toEqual(
|
||||
'ProductTag with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a tag successfully", async () => {
|
||||
await service.create(
|
||||
[{
|
||||
value: "UK"
|
||||
}]
|
||||
)
|
||||
await service.create([
|
||||
{
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const [productTag] = await service.list({
|
||||
value: "UK"
|
||||
value: "UK",
|
||||
})
|
||||
|
||||
expect(productTag.value).toEqual("UK")
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
import { ProductTypeService } from "@services"
|
||||
import { ProductTypeRepository } from "@repositories"
|
||||
import { Product } from "@models"
|
||||
|
||||
import { TestDatabase } from "../../../utils"
|
||||
import { createProductAndTypes } from "../../../__fixtures__/product"
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -33,7 +35,7 @@ describe("ProductType Service", () => {
|
||||
type: {
|
||||
id: "type-2",
|
||||
value: "Type 2",
|
||||
}
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
@@ -41,13 +43,12 @@ describe("ProductType Service", () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
|
||||
const productTypeRepository = new ProductTypeRepository({
|
||||
manager: repositoryManager,
|
||||
})
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
|
||||
service = new ProductTypeService({
|
||||
productTypeRepository,
|
||||
})
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productTypeService")
|
||||
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
@@ -115,7 +116,9 @@ describe("ProductType Service", () => {
|
||||
})
|
||||
|
||||
it("should return product type and count when filtered", async () => {
|
||||
const [typeResults, count] = await service.listAndCount({ id: data[0].type.id })
|
||||
const [typeResults, count] = await service.listAndCount({
|
||||
id: data[0].type.id,
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(typeResults).toEqual([
|
||||
@@ -126,7 +129,10 @@ describe("ProductType Service", () => {
|
||||
})
|
||||
|
||||
it("should return product type and count when using skip and take", async () => {
|
||||
const [typeResults, count] = await service.listAndCount({}, { skip: 1, take: 1 })
|
||||
const [typeResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 1, take: 1 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(typeResults).toEqual([
|
||||
@@ -137,10 +143,13 @@ describe("ProductType Service", () => {
|
||||
})
|
||||
|
||||
it("should return requested fields", async () => {
|
||||
const [typeResults, count] = await service.listAndCount({}, {
|
||||
take: 1,
|
||||
select: ["value"],
|
||||
})
|
||||
const [typeResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["value"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(typeResults))
|
||||
|
||||
@@ -158,13 +167,11 @@ describe("ProductType Service", () => {
|
||||
const typeValue = "Type 1"
|
||||
|
||||
it("should return type for the given id", async () => {
|
||||
const type = await service.retrieve(
|
||||
typeId,
|
||||
)
|
||||
const type = await service.retrieve(typeId)
|
||||
|
||||
expect(type).toEqual(
|
||||
expect.objectContaining({
|
||||
id: typeId
|
||||
id: typeId,
|
||||
})
|
||||
)
|
||||
})
|
||||
@@ -178,7 +185,9 @@ describe("ProductType Service", () => {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('ProductType with id: does-not-exist was not found')
|
||||
expect(error.message).toEqual(
|
||||
"ProductType with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an id is not provided", async () => {
|
||||
@@ -194,21 +203,16 @@ describe("ProductType Service", () => {
|
||||
})
|
||||
|
||||
it("should return type based on config select param", async () => {
|
||||
const type = await service.retrieve(
|
||||
typeId,
|
||||
{
|
||||
select: ["id", "value"],
|
||||
}
|
||||
)
|
||||
const type = await service.retrieve(typeId, {
|
||||
select: ["id", "value"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(type))
|
||||
|
||||
expect(serialized).toEqual(
|
||||
{
|
||||
id: typeId,
|
||||
value: typeValue,
|
||||
}
|
||||
)
|
||||
expect(serialized).toEqual({
|
||||
id: typeId,
|
||||
value: typeValue,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -216,12 +220,10 @@ describe("ProductType Service", () => {
|
||||
const typeId = "type-1"
|
||||
|
||||
it("should delete the product type given an ID successfully", async () => {
|
||||
await service.delete(
|
||||
[typeId],
|
||||
)
|
||||
await service.delete([typeId])
|
||||
|
||||
const types = await service.list({
|
||||
id: typeId
|
||||
id: typeId,
|
||||
})
|
||||
|
||||
expect(types).toHaveLength(0)
|
||||
@@ -232,12 +234,12 @@ describe("ProductType Service", () => {
|
||||
const typeId = "type-1"
|
||||
|
||||
it("should update the value of the type successfully", async () => {
|
||||
await service.update(
|
||||
[{
|
||||
await service.update([
|
||||
{
|
||||
id: typeId,
|
||||
value: "UK"
|
||||
}]
|
||||
)
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productType = await service.retrieve(typeId)
|
||||
|
||||
@@ -249,29 +251,31 @@ describe("ProductType Service", () => {
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
value: "UK"
|
||||
}
|
||||
])
|
||||
{
|
||||
id: "does-not-exist",
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('ProductType with id "does-not-exist" not found')
|
||||
expect(error.message).toEqual(
|
||||
'ProductType with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a type successfully", async () => {
|
||||
await service.create(
|
||||
[{
|
||||
value: "UK"
|
||||
}]
|
||||
)
|
||||
await service.create([
|
||||
{
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const [productType] = await service.list({
|
||||
value: "UK"
|
||||
value: "UK",
|
||||
})
|
||||
|
||||
expect(productType.value).toEqual("UK")
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { TestDatabase } from "../../../utils"
|
||||
import { ProductService, ProductVariantService } from "@services"
|
||||
import { ProductRepository, ProductVariantRepository } from "@repositories"
|
||||
import { ProductVariantService } from "@services"
|
||||
import { Product, ProductTag, ProductVariant } from "@models"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { Collection } from "@mikro-orm/core"
|
||||
@@ -13,6 +12,9 @@ import {
|
||||
} from "../../../__fixtures__/product"
|
||||
import { productsData, variantsData } from "../../../__fixtures__/product/data"
|
||||
import { buildProductVariantOnlyData } from "../../../__fixtures__/variant/data/create-variant"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
|
||||
describe("ProductVariant Service", () => {
|
||||
let service: ProductVariantService
|
||||
@@ -27,20 +29,12 @@ describe("ProductVariant Service", () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
|
||||
const productVariantRepository = new ProductVariantRepository({
|
||||
manager: repositoryManager,
|
||||
})
|
||||
const productRepository = new ProductRepository({
|
||||
manager: repositoryManager,
|
||||
})
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
|
||||
const productService = new ProductService({
|
||||
productRepository,
|
||||
})
|
||||
service = new ProductVariantService({
|
||||
productService,
|
||||
productVariantRepository,
|
||||
})
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productVariantService")
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
|
||||
@@ -21,12 +21,13 @@ import {
|
||||
} from "../../../__fixtures__/product/data"
|
||||
|
||||
import { ProductDTO, ProductTypes } from "@medusajs/types"
|
||||
import { kebabCase } from "@medusajs/utils"
|
||||
import { createMedusaContainer, kebabCase } from "@medusajs/utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { ProductRepository } from "@repositories"
|
||||
import { ProductService } from "@services"
|
||||
import { createProductCategories } from "../../../__fixtures__/product-category"
|
||||
import { TestDatabase } from "../../../utils"
|
||||
import { asValue } from "awilix"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -44,13 +45,12 @@ describe("Product Service", () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
|
||||
const productRepository = new ProductRepository({
|
||||
manager: repositoryManager,
|
||||
})
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
|
||||
service = new ProductService({
|
||||
productRepository,
|
||||
})
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productService")
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
|
||||
Reference in New Issue
Block a user