feat(types, product, utils, medusa): Include shared connection for modules (#4626)

This commit is contained in:
Adrien de Peretti authored and GitHub committed 2023-08-08 15:10:34 +00:00
1 parent a2d7540e40
commit 3f3a84262c
26 files changed
+476 -168

No files matched your search

@@ -1,3 +1,4 @@
import { Context } from "@medusajs/types"
import { DALUtils } from "@medusajs/utils"
class CustomRepository extends DALUtils.MikroOrmBaseRepository {
@@ -5,12 +6,44 @@ class CustomRepository extends DALUtils.MikroOrmBaseRepository {
// @ts-ignore
super(...arguments)
}
}
CustomRepository.prototype.find = jest.fn().mockImplementation(async () => [])
CustomRepository.prototype.findAndCount = jest
.fn()
.mockImplementation(async () => [])
find = jest.fn().mockImplementation(async () => [])
findAndCount = jest.fn().mockImplementation(async () => [])
create = jest.fn()
update = jest.fn()
delete = jest.fn()
softDelete = jest.fn()
restore = jest.fn()
async transaction<TManager = unknown>(
task: (transactionManager: TManager) => Promise<any>,
options: {
isolationLevel?: string
enableNestedTransactions?: boolean
transaction?: TManager
} = {}
): Promise<any> {
return super.transaction(task, options)
}
getActiveManager<TManager = unknown>({
transactionManager,
manager,
}: Context = {}): TManager {
return super.getActiveManager({ transactionManager, manager })
}
getFreshManager<TManager = unknown>(): TManager {
return super.getFreshManager()
}
async serialize<TOutput extends object | object[]>(
data: any,
options?: any
): Promise<TOutput> {
return super.serialize(data, options)
}
}
export class ProductRepository extends CustomRepository {}
export class ProductTagRepository extends CustomRepository {}
@@ -1,14 +1,24 @@
import { MedusaModule } from "@medusajs/modules-sdk"
import { initialize } from "../../src"
import * as CustomRepositories from "../__fixtures__/module"
import { ProductRepository } from "../__fixtures__/module"
import { createProductAndTags } from "../__fixtures__/product"
import {
buildProductAndRelationsData,
createProductAndTags,
} from "../__fixtures__/product"
import { productsData } from "../__fixtures__/product/data"
import { DB_URL, TestDatabase } from "../utils"
import { buildProductAndRelationsData } from "../__fixtures__/product/data/create-product"
import { IProductModuleService } from "@medusajs/types"
import { kebabCase } from "@medusajs/utils"
import { IProductModuleService } from "@medusajs/types"
import { knex } from "knex"
import { EventBusService } from "../__fixtures__/event-bus"
const sharedPgConnection = knex<any, any>({
client: "pg",
searchPath: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
connection: {
connectionString: DB_URL,
},
})
const beforeEach_ = async () => {
await TestDatabase.setupDatabase()
@@ -20,6 +30,8 @@ const afterEach_ = async () => {
}
describe("Product module", function () {
const eventBus = new EventBusService()
describe("Using built-in data access layer", function () {
let module: IProductModuleService
@@ -27,12 +39,17 @@ describe("Product module", function () {
const testManager = await beforeEach_()
await createProductAndTags(testManager, productsData)
module = await initialize({
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
module = await initialize(
{
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
},
},
})
{
eventBusModuleService: eventBus,
}
)
})
afterEach(afterEach_)
@@ -41,6 +58,12 @@ describe("Product module", function () {
expect(module).toBeDefined()
})
it("should have a connection that is not the shared connection", async () => {
expect(
(module as any).baseRepository_.manager_.getConnection().client
).not.toEqual(sharedPgConnection)
})
it("should return a list of product", async () => {
const products = await module.list()
expect(products).toHaveLength(2)
@@ -48,20 +71,25 @@ describe("Product module", function () {
})
describe("Using custom data access layer", function () {
let module: IProductModuleService
let module
beforeEach(async () => {
const testManager = await beforeEach_()
await createProductAndTags(testManager, productsData)
module = await initialize({
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
module = await initialize(
{
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
},
repositories: CustomRepositories,
},
repositories: CustomRepositories,
})
{
eventBusModuleService: eventBus,
}
)
})
afterEach(afterEach_)
@@ -70,16 +98,22 @@ describe("Product module", function () {
expect(module).toBeDefined()
})
it("should have a connection that is not the shared connection", async () => {
expect(
(module as any).baseRepository_.manager_.getConnection().client
).not.toEqual(sharedPgConnection)
})
it("should return a list of product", async () => {
const products = await module.list()
expect(ProductRepository.prototype.find).toHaveBeenCalled()
expect(module.productService_.productRepository_.find).toHaveBeenCalled()
expect(products).toHaveLength(0)
})
})
describe("Using custom data access layer and connection", function () {
let module: IProductModuleService
describe("Using custom data access layer and manager", function () {
let module
beforeEach(async () => {
const testManager = await beforeEach_()
@@ -87,10 +121,15 @@ describe("Product module", function () {
MedusaModule.clearInstances()
module = await initialize({
manager: testManager,
repositories: CustomRepositories,
})
module = await initialize(
{
manager: testManager,
repositories: CustomRepositories,
},
{
eventBusModuleService: eventBus,
}
)
})
afterEach(afterEach_)
@@ -99,14 +138,54 @@ describe("Product module", function () {
expect(module).toBeDefined()
})
it("should have a connection that is not the shared connection", async () => {
expect(
(module as any).baseRepository_.manager_.getConnection().client
).not.toEqual(sharedPgConnection)
})
it("should return a list of product", async () => {
const products = await module.list()
expect(ProductRepository.prototype.find).toHaveBeenCalled()
expect(module.productService_.productRepository_.find).toHaveBeenCalled()
expect(products).toHaveLength(0)
})
})
describe("Using an existing connection", function () {
let module: IProductModuleService
beforeEach(async () => {
const testManager = await beforeEach_()
await createProductAndTags(testManager, productsData)
MedusaModule.clearInstances()
module = await initialize(
{
database: {
connection: sharedPgConnection,
},
},
{
eventBusModuleService: eventBus,
}
)
})
afterEach(afterEach_)
it("should initialize and return a list of product", async () => {
expect(module).toBeDefined()
})
it("should have a connection that is the shared connection", async () => {
expect(
(module as any).baseRepository_.manager_.getConnection().client
).toEqual(sharedPgConnection)
})
})
describe("create", function () {
let module: IProductModuleService
let images = ["image-1"]
@@ -116,12 +195,17 @@ describe("Product module", function () {
MedusaModule.clearInstances()
module = await initialize({
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
module = await initialize(
{
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
},
},
})
{
eventBusModuleService: eventBus,
}
)
})
afterEach(afterEach_)
@@ -212,12 +296,17 @@ describe("Product module", function () {
MedusaModule.clearInstances()
module = await initialize({
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
module = await initialize(
{
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
},
},
})
{
eventBusModuleService: eventBus,
}
)
})
afterEach(afterEach_)
@@ -282,12 +371,17 @@ describe("Product module", function () {
MedusaModule.clearInstances()
module = await initialize({
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
module = await initialize(
{
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
},
},
})
{
eventBusModuleService: eventBus,
}
)
})
afterEach(afterEach_)