-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(orchestration): modules method context (#9669)
** What ** * Test to check if the MedusaContext is being injected when calling a Module's method
- Loading branch information
1 parent
8209d93
commit 6fa98b6
Showing
7 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
integration-tests/modules/__tests__/__fixtures__/testing-module/index.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,11 @@ | ||
import { ModuleExports } from "@medusajs/types" | ||
import { ModuleService } from "./services/module-service" | ||
|
||
const moduleExports: ModuleExports = { | ||
service: ModuleService, | ||
} | ||
|
||
export * from "./models" | ||
export * from "./services/module-service" | ||
|
||
export default moduleExports |
6 changes: 6 additions & 0 deletions
6
integration-tests/modules/__tests__/__fixtures__/testing-module/models/dml-entity.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,6 @@ | ||
import { model } from "@medusajs/utils" | ||
|
||
export const dmlEntity = model.define("dmlEntity", { | ||
id: model.id().primaryKey(), | ||
name: model.text(), | ||
}) |
1 change: 1 addition & 0 deletions
1
integration-tests/modules/__tests__/__fixtures__/testing-module/models/index.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 @@ | ||
export * from "./dml-entity" |
17 changes: 17 additions & 0 deletions
17
integration-tests/modules/__tests__/__fixtures__/testing-module/services/module-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,17 @@ | ||
import { IModuleService } from "@medusajs/types" | ||
import { MedusaContext } from "@medusajs/utils" | ||
|
||
// @ts-expect-error | ||
export class ModuleService implements IModuleService { | ||
public property = "value" | ||
public dynProperty | ||
|
||
constructor() { | ||
this.dynProperty = { | ||
key: "key value", | ||
} | ||
} | ||
async methodName(input, @MedusaContext() context) { | ||
return input + " called" | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
integration-tests/modules/__tests__/workflow-engine/workflow-engine.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,93 @@ | ||
import { Modules } from "@medusajs/framework/utils" | ||
import { | ||
createStep, | ||
createWorkflow, | ||
StepResponse, | ||
WorkflowData, | ||
WorkflowResponse, | ||
} from "@medusajs/framework/workflows-sdk" | ||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" | ||
import { | ||
adminHeaders, | ||
createAdminUser, | ||
} from "../../../helpers/create-admin-user" | ||
|
||
jest.setTimeout(50000) | ||
|
||
medusaIntegrationTestRunner({ | ||
testSuite: ({ dbConnection, getContainer, api }) => { | ||
describe("Workflow Engine API", () => { | ||
let medusaContainer | ||
|
||
beforeAll(() => { | ||
medusaContainer = getContainer() | ||
}) | ||
|
||
beforeEach(async () => { | ||
await createAdminUser(dbConnection, adminHeaders, medusaContainer) | ||
}) | ||
|
||
describe("Testing WorkflowEngine.run", () => { | ||
beforeAll(async () => { | ||
const step1 = createStep( | ||
{ | ||
name: "my-step", | ||
}, | ||
async (input: { initial: string }, { container }) => { | ||
const testMod = container.resolve("testingModule") as any | ||
|
||
return new StepResponse(testMod.methodName(input.initial)) | ||
} | ||
) | ||
|
||
createWorkflow( | ||
{ | ||
name: "my-workflow-name", | ||
}, | ||
function (input: WorkflowData<{ initial: string }>) { | ||
const stepRes = step1(input) | ||
|
||
return new WorkflowResponse(stepRes) | ||
} | ||
) | ||
}) | ||
|
||
it("Should invoke modules passing the current medusa context as argument", async () => { | ||
const testMod = medusaContainer.resolve("testingModule") as any | ||
|
||
const methodSpy = jest.spyOn(testMod, "methodName") | ||
|
||
const engine = medusaContainer.resolve(Modules.WORKFLOW_ENGINE) | ||
|
||
const res = await engine.run("my-workflow-name", { | ||
transactionId: "trx-id", | ||
input: { | ||
initial: "abc", | ||
}, | ||
context: { | ||
meta: { | ||
myStuff: "myStuff", | ||
}, | ||
}, | ||
}) | ||
|
||
expect(res.result).toEqual("abc called") | ||
|
||
expect(methodSpy).toHaveBeenCalledTimes(1) | ||
expect(methodSpy).toHaveBeenCalledWith( | ||
"abc", | ||
expect.objectContaining({ | ||
transactionId: "trx-id", | ||
__type: "MedusaContext", | ||
eventGroupId: expect.any(String), | ||
idempotencyKey: "my-workflow-name:trx-id:my-step:invoke", | ||
meta: { | ||
myStuff: "myStuff", | ||
}, | ||
}) | ||
) | ||
}) | ||
}) | ||
}) | ||
}, | ||
}) |
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