chore(): Autoload module resources (#7291)
**What** - automatically build and consume connection and container loader if not exported by the module - therefore load the services and repositories automatically, including baseRepository - automatically build run and revert migrations if not provided - cleaup modules to remove extra unnecessary bits and pieces - remove the `initializeFactory` in favor of using `medusaApp` Should drastically improve the module building DX by removing a lot of boilerplate to handle by the user, that plus the base entity should simplify quite a lot the flow cc @shahednasser **Note** I had to choose a way to identify connection and container loader from the exported loader from the module. I decided to go with named function `connectionLoader` and `containerLoader`, also, now the factories will return named function so if the user use the factories we are providing to build those loaders, the function will also be named and identified
This commit is contained in:
+202
-203
@@ -2,242 +2,241 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
|
||||
import { initialize } from "../../../src"
|
||||
|
||||
import { createSalesChannels } from "../../__fixtures__"
|
||||
import { DB_URL, MikroOrmWrapper } from "../../utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Sales Channel Service", () => {
|
||||
let service: ISalesChannelModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let repositoryManager: SqlEntityManager
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.SALES_CHANNEL,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
medusaApp,
|
||||
}: SuiteOptions<ISalesChannelModuleService>) => {
|
||||
let service: ISalesChannelModuleService
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
repositoryManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
service = await initialize({
|
||||
database: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_SALES_CHANNEL_DB_SCHEMA,
|
||||
},
|
||||
beforeEach(() => {
|
||||
service = medusaApp.modules[Modules.SALES_CHANNEL]
|
||||
})
|
||||
|
||||
testManager = await MikroOrmWrapper.forkManager()
|
||||
describe("Sales Channel Service", () => {
|
||||
let testManager: SqlEntityManager
|
||||
let repositoryManager: SqlEntityManager
|
||||
|
||||
await createSalesChannels(testManager)
|
||||
})
|
||||
beforeEach(async () => {
|
||||
repositoryManager = await MikroOrmWrapper.forkManager()
|
||||
testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a SalesChannel successfully", async () => {
|
||||
const [created] = await service.create([
|
||||
{
|
||||
name: "test",
|
||||
description: "test",
|
||||
},
|
||||
])
|
||||
|
||||
const [channel] = await service.list({
|
||||
name: [created.name],
|
||||
await createSalesChannels(testManager)
|
||||
})
|
||||
|
||||
expect(channel.name).toEqual("test")
|
||||
expect(channel.description).toEqual("test")
|
||||
})
|
||||
})
|
||||
describe("create", () => {
|
||||
it("should create a SalesChannel successfully", async () => {
|
||||
const [created] = await service.create([
|
||||
{
|
||||
name: "test",
|
||||
description: "test",
|
||||
},
|
||||
])
|
||||
|
||||
describe("retrieve", () => {
|
||||
const id = "channel-1"
|
||||
const [channel] = await service.list({
|
||||
name: [created.name],
|
||||
})
|
||||
|
||||
it("should return SalesChannel for the given id", async () => {
|
||||
const result = await service.retrieve(id)
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
expect(channel.name).toEqual("test")
|
||||
expect(channel.description).toEqual("test")
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when SalesChannelId with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"SalesChannel with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
const id = "channel-2"
|
||||
|
||||
it("should update the name of the SalesChannel successfully", async () => {
|
||||
await service.update(id, {
|
||||
name: "Update name 2",
|
||||
is_disabled: true,
|
||||
})
|
||||
|
||||
const channel = await service.retrieve(id)
|
||||
describe("retrieve", () => {
|
||||
const id = "channel-1"
|
||||
|
||||
expect(channel.name).toEqual("Update name 2")
|
||||
expect(channel.is_disabled).toEqual(true)
|
||||
})
|
||||
it("should return SalesChannel for the given id", async () => {
|
||||
const result = await service.retrieve(id)
|
||||
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update("does-not-exist", {
|
||||
name: "does-not-exist",
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'SalesChannel with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
it("should throw an error when SalesChannelId with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
describe("list", () => {
|
||||
it("should return a list of SalesChannels", async () => {
|
||||
const result = await service.list()
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(result).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "channel-1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "channel-2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "channel-3",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list SalesChannels by name", async () => {
|
||||
const result = await service.list({
|
||||
name: ["Channel 2", "Channel 3"],
|
||||
expect(error.message).toEqual(
|
||||
"SalesChannel with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
expect(result).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "channel-2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "channel-3",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
describe("update", () => {
|
||||
const id = "channel-2"
|
||||
|
||||
describe("listAndCount", () => {
|
||||
it("should return sales channels and count", async () => {
|
||||
const [result, count] = await service.listAndCount()
|
||||
it("should update the name of the SalesChannel successfully", async () => {
|
||||
await service.update(id, {
|
||||
name: "Update name 2",
|
||||
is_disabled: true,
|
||||
})
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(result).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "channel-1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "channel-2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "channel-3",
|
||||
}),
|
||||
])
|
||||
})
|
||||
const channel = await service.retrieve(id)
|
||||
|
||||
it("should return sales channels and count when filtered", async () => {
|
||||
const [result, count] = await service.listAndCount({
|
||||
id: ["channel-2"],
|
||||
expect(channel.name).toEqual("Update name 2")
|
||||
expect(channel.is_disabled).toEqual(true)
|
||||
})
|
||||
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update("does-not-exist", {
|
||||
name: "does-not-exist",
|
||||
})
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'SalesChannel with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(result).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "channel-2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
describe("list", () => {
|
||||
it("should return a list of SalesChannels", async () => {
|
||||
const result = await service.list()
|
||||
|
||||
it("should return sales channels and count when using skip and take", async () => {
|
||||
const [results, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 1, take: 1 }
|
||||
)
|
||||
expect(result).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "channel-1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "channel-2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "channel-3",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "channel-2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
it("should list SalesChannels by name", async () => {
|
||||
const result = await service.list({
|
||||
name: ["Channel 2", "Channel 3"],
|
||||
})
|
||||
|
||||
it("should return requested fields", async () => {
|
||||
const [result, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["id", "name"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(result))
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(serialized).toEqual([
|
||||
{
|
||||
id: "channel-1",
|
||||
name: "Channel 1",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should filter disabled channels", async () => {
|
||||
const [result, count] = await service.listAndCount(
|
||||
{ is_disabled: true },
|
||||
{ select: ["id"] }
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(result))
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(serialized).toEqual([
|
||||
{
|
||||
id: "channel-3",
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
const id = "channel-2"
|
||||
|
||||
it("should delete the SalesChannel given an id successfully", async () => {
|
||||
await service.delete([id])
|
||||
|
||||
const result = await service.list({
|
||||
id: [id],
|
||||
expect(result).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "channel-2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "channel-3",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
expect(result).toHaveLength(0)
|
||||
describe("listAndCount", () => {
|
||||
it("should return sales channels and count", async () => {
|
||||
const [result, count] = await service.listAndCount()
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(result).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "channel-1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "channel-2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "channel-3",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return sales channels and count when filtered", async () => {
|
||||
const [result, count] = await service.listAndCount({
|
||||
id: ["channel-2"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(result).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "channel-2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return sales channels and count when using skip and take", async () => {
|
||||
const [results, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 1, take: 1 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "channel-2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return requested fields", async () => {
|
||||
const [result, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["id", "name"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(result))
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(serialized).toEqual([
|
||||
{
|
||||
id: "channel-1",
|
||||
name: "Channel 1",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should filter disabled channels", async () => {
|
||||
const [result, count] = await service.listAndCount(
|
||||
{ is_disabled: true },
|
||||
{ select: ["id"] }
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(result))
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(serialized).toEqual([
|
||||
{
|
||||
id: "channel-3",
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
const id = "channel-2"
|
||||
|
||||
it("should delete the SalesChannel given an id successfully", async () => {
|
||||
await service.delete([id])
|
||||
|
||||
const result = await service.list({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(result).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user