-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6a9815
commit 15c7afb
Showing
7 changed files
with
132 additions
and
30 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/modules-sdk/src/loaders/__mocks__/@plugins/default.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const service = class TestService {} | ||
|
||
export default { | ||
services: [service], | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/modules-sdk/src/loaders/__mocks__/@plugins/no-default.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const service = class TestService {} | ||
|
||
export const defaultExport = { | ||
services: [service], | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/modules-sdk/src/loaders/__mocks__/@plugins/no-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
loaders: [], | ||
} |
94 changes: 94 additions & 0 deletions
94
packages/modules-sdk/src/loaders/__tests__/module-provider-loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters