feat: restructure events payload (#8143)

* refactor: restructure events payload

Breaking change: This PR changes the event payload accepted by the event
listeners

* refactor: fix failing tests and implement feedback

* add integration tests

* fix timeout

---------

Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com>
This commit is contained in:
Harminder Virk
2024-07-16 21:39:16 +05:30
committed by GitHub
parent 5813216c88
commit f579f0b3be
26 changed files with 194 additions and 111 deletions

View File

@@ -0,0 +1,50 @@
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { MedusaContainer } from "@medusajs/types"
import { composeMessage, ModuleRegistrationName } from "@medusajs/utils"
import testEventPayloadHandlerMock from "../../dist/subscribers/test-event-payload"
jest.setTimeout(30000)
medusaIntegrationTestRunner({
testSuite: ({ getContainer }) => {
let container!: MedusaContainer
describe("EventBusModule", () => {
beforeAll(() => {
container = getContainer()
})
it(`should emit event with the expected shape to be received by the subscribers`, async () => {
const eventBus = container.resolve(ModuleRegistrationName.EVENT_BUS)
const eventName = "test-event-payload"
await eventBus.emit(
composeMessage(eventName, {
data: {
test: "foo",
},
object: "object",
source: "source",
action: "action",
})
)
expect(testEventPayloadHandlerMock).toHaveBeenCalled()
expect(
testEventPayloadHandlerMock.mock.calls[0][0].pluginOptions
).toEqual({})
expect(testEventPayloadHandlerMock.mock.calls[0][0].event).toEqual({
name: eventName,
data: {
test: "foo",
},
metadata: {
object: "object",
source: "source",
action: "action",
},
})
})
})
},
})

View File

@@ -173,7 +173,7 @@ medusaIntegrationTestRunner({
const logSpy = jest.spyOn(logger, "info")
await eventBus.emit({
eventName: "order.created",
name: "order.created",
data: {
order: {
id: "1234",

View File

@@ -7,7 +7,7 @@
"scripts": {
"test:integration": "jest --silent=false --no-cache --maxWorkers=50% --bail --detectOpenHandles --forceExit --logHeapUsage",
"test:integration:chunk": "jest --silent --no-cache --bail --maxWorkers=50% --forceExit --testPathPattern=$(echo $CHUNKS | jq -r \".[${CHUNK}] | .[]\")",
"build": "tsc ./src/* --allowJs --outDir ./dist"
"build": "tsc --allowJs --outDir ./dist"
},
"dependencies": {
"@medusajs/api-key": "workspace:^",

View File

@@ -0,0 +1,9 @@
import { SubscriberConfig } from "@medusajs/medusa/src"
const testEventPayloadHandlerMock = jest.fn()
export default testEventPayloadHandlerMock
export const config: SubscriberConfig = {
event: "test-event-payload",
}

View File

@@ -0,0 +1,31 @@
{
"compilerOptions": {
"lib": ["es5", "es6", "es2019"],
"target": "es2022",
"outDir": "./dist",
"esModuleInterop": true,
"declarationMap": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true,
"downlevelIteration": true // to use ES5 specific tooling
},
"include": ["src"],
"exclude": [
"./dist/**/*",
"__tests__",
"helpers",
"./**/helpers",
"./**/__snapshots__",
"node_modules"
]
}