Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Jan 31, 2024
1 parent f6a9815 commit 15c7afb
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const service = class TestService {}

export default {
services: [service],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const service = class TestService {}

export const defaultExport = {
services: [service],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
loaders: [],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { createMedusaContainer } from "@medusajs/utils"
import { Lifetime, asFunction } from "awilix"
import { moduleProviderLoader } from "../module-provider-loader"

const logger = {
warn: jest.fn(),
error: jest.fn(),
} as any

describe("modules loader", () => {
let container

afterEach(() => {
jest.clearAllMocks()
})

beforeEach(() => {
container = createMedusaContainer()
})

it("should register the provider service", async () => {
const moduleProviders = [
{
resolve: "@plugins/default",
options: {},
},
]

await moduleProviderLoader({ container, providers: moduleProviders })

const testService = container.resolve("testService")
expect(testService).toBeTruthy()
expect(typeof testService).toEqual("object")
})

it("should register the provider service with custom register fn", async () => {
const fn = async (klass, container, details) => {
container.register({
[`testServiceCustomRegistration`]: asFunction(
(cradle) => new klass(cradle, details.options),
{
lifetime: Lifetime.SINGLETON,
}
),
})
}
const moduleProviders = [
{
resolve: "@plugins/default",
options: {},
},
]

await moduleProviderLoader({ container, providers: moduleProviders, registerServiceFn: fn })

const testService = container.resolve("testServiceCustomRegistration")
expect(testService).toBeTruthy()
expect(typeof testService).toEqual("object")
})

it("should log the errors if no service is defined", async () => {
const moduleProviders = [
{
resolve: "@plugins/no-service",
options: {},
},
]

try {
await moduleProviderLoader({ container, providers: moduleProviders })
} catch (error) {
expect(error.message).toBe(
"No services found in plugin @plugins/no-service -- make sure your plugin exports a service."
)
}
})

it("should throw if no default export is defined", async () => {
const moduleProviders = [
{
resolve: "@plugins/no-default",
options: {},
},
]

try {
await moduleProviderLoader({ container, providers: moduleProviders })
} catch (error) {
expect(error.message).toBe(
"No default export found in plugin @plugins/no-default -- make sure your plugin has a default export."
)
}
})
})
27 changes: 13 additions & 14 deletions packages/modules-sdk/src/loaders/module-provider-loader.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { Constructor, MedusaContainer } from "@medusajs/types"
import { MedusaContainer, ModuleProvider } from "@medusajs/types"
import { isString, promiseAll } from "@medusajs/utils"
import { Lifetime, asFunction } from "awilix"

type ModuleProviderExports = {
services: Constructor<any>[]
}

type ModuleProvider = {
resolve: string | ModuleProviderExports
provider_name?: string
options: Record<string, unknown>
}

export async function moduleProviderLoader({
container,
providers,
registerServiceFn,
}: {
container: MedusaContainer
providers: ModuleProvider[]
registerServiceFn?: (
klass,
container: MedusaContainer,
pluginDetails: any
) => Promise<void>
}) {
if (!providers?.length) {
return
Expand Down Expand Up @@ -53,24 +51,25 @@ export async function loadModuleProvider(

if (!loadedProvider) {
throw new Error(
`No default export found in plugin ${pluginName} -- make sure your plugin exports a service.`
`No default export found in plugin ${pluginName} -- make sure your plugin has a default export.`
)
}

if (!loadedProvider?.services) {
if (!loadedProvider.services?.length) {
throw new Error(
`No services found in plugin ${provider.resolve} -- make sure your plugin exports a service.`
)
}

const services = await promiseAll(
loadedProvider.services.map(async (service) => {
const name = service.name[0].toLowerCase() + service.name.slice(1)
if (registerServiceFn) {
// Used to register the specific type of service in the provider
await registerServiceFn(service, container, provider.options)
} else {
container.register({
[service.name]: asFunction(
[name]: asFunction(
(cradle) => new service(cradle, provider.options),
{
lifetime: service.LIFE_TIME || Lifetime.SCOPED,
Expand Down
18 changes: 2 additions & 16 deletions packages/payment/src/loaders/providers.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import { AbstractPaymentProcessor } from "@medusajs/medusa"
import { moduleProviderLoader } from "@medusajs/modules-sdk"

import { LoaderOptions, ModulesSdkTypes } from "@medusajs/types"
import { LoaderOptions, ModuleProvider, ModulesSdkTypes } from "@medusajs/types"
import { Lifetime, asFunction } from "awilix"

type PaymentModuleProviders = {
providers: {
resolve: string
provider_id: string // e.g. stripe-usd
options: Record<string, unknown>
}[]
}

const registrationFn = async (klass, container, pluginOptions) => {
if (!AbstractPaymentProcessor.isPaymentProcessor(klass.prototype)) {
return
}

container.register({
[`payment_provider_${klass.prototype}`]: asFunction(
(cradle) => new klass(cradle, pluginOptions),
Expand All @@ -41,8 +28,7 @@ export default async ({
(
| ModulesSdkTypes.ModuleServiceInitializeOptions
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
) &
PaymentModuleProviders
) & { providers: ModuleProvider[] }
>): Promise<void> => {
const pluginProviders =
options?.providers?.filter((provider) => provider.resolve) || []
Expand Down
10 changes: 10 additions & 0 deletions packages/types/src/modules-sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,13 @@ export interface IModuleService {
onApplicationStart?: () => Promise<void>
}
}

export type ModuleProviderExports = {
services: Constructor<any>[]
}

export type ModuleProvider = {
resolve: string | ModuleProviderExports
provider_name?: string
options: Record<string, unknown>
}

0 comments on commit 15c7afb

Please sign in to comment.