implement events to the pricing module (#7584)

This commit is contained in:
Harminder Virk
2024-06-05 17:33:49 +05:30
committed by GitHub
parent b2f2c366ec
commit 2b62686ec6
8 changed files with 435 additions and 17 deletions
@@ -1,8 +1,13 @@
import { Modules } from "@medusajs/modules-sdk"
import { IPricingModuleService } from "@medusajs/types"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import {
MockEventBusService,
moduleIntegrationTestRunner,
SuiteOptions,
} from "medusa-test-utils"
import { createPriceLists } from "../../../__fixtures__/price-list"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { CommonEvents, composeMessage, PricingEvents } from "@medusajs/utils"
jest.setTimeout(30000)
@@ -12,6 +17,16 @@ moduleIntegrationTestRunner({
MikroOrmWrapper,
service,
}: SuiteOptions<IPricingModuleService>) => {
let eventBusEmitSpy
beforeEach(() => {
eventBusEmitSpy = jest.spyOn(MockEventBusService.prototype, "emit")
})
afterEach(() => {
jest.clearAllMocks()
})
describe("PriceList Service", () => {
beforeEach(async () => {
const testManager = await MikroOrmWrapper.forkManager()
@@ -400,8 +415,8 @@ moduleIntegrationTestRunner({
{
title: "test",
description: "test",
starts_at: new Date("10/01/2023"),
ends_at: new Date("10/30/2023"),
starts_at: new Date("10/01/2023").toISOString(),
ends_at: new Date("10/30/2023").toISOString(),
rules: {
customer_group_id: [
"vip-customer-group-id",
@@ -488,6 +503,41 @@ moduleIntegrationTestRunner({
]),
})
)
const events = eventBusEmitSpy.mock.calls[0][0]
expect(events).toHaveLength(4)
expect(events[0]).toEqual(
composeMessage(PricingEvents.price_list_created, {
service: Modules.PRICING,
action: CommonEvents.CREATED,
object: "price_list",
data: { id: priceList.id },
})
)
expect(events[1]).toEqual(
composeMessage(PricingEvents.price_list_rule_created, {
service: Modules.PRICING,
action: CommonEvents.CREATED,
object: "price_list_rule",
data: { id: priceList.price_list_rules?.[0].id },
})
)
expect(events[2]).toEqual(
composeMessage(PricingEvents.price_list_rule_created, {
service: Modules.PRICING,
action: CommonEvents.CREATED,
object: "price_list_rule",
data: { id: priceList.price_list_rules?.[1].id },
})
)
expect(events[3]).toEqual(
composeMessage(PricingEvents.price_created, {
service: Modules.PRICING,
action: CommonEvents.CREATED,
object: "price",
data: { id: priceList.prices![0].id },
})
)
})
it("should create a price list with granular rules within prices", async () => {
@@ -745,6 +795,8 @@ moduleIntegrationTestRunner({
},
])
jest.clearAllMocks()
await service.addPriceListPrices([
{
price_list_id: "price-list-1",
@@ -809,6 +861,26 @@ moduleIntegrationTestRunner({
price_list_rules: [],
})
)
const events = eventBusEmitSpy.mock.calls[0][0]
expect(events).toHaveLength(2)
expect(events[0]).toEqual(
composeMessage(PricingEvents.price_created, {
service: Modules.PRICING,
action: CommonEvents.CREATED,
object: "price",
data: { id: priceList.prices![0].id },
})
)
expect(events[1]).toEqual(
composeMessage(PricingEvents.price_rule_created, {
service: Modules.PRICING,
action: CommonEvents.CREATED,
object: "price_rule",
data: { id: priceList.prices![0].price_rules![0].id },
})
)
})
})
@@ -5,9 +5,14 @@ import {
IPricingModuleService,
} from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import {
MockEventBusService,
moduleIntegrationTestRunner,
SuiteOptions,
} from "medusa-test-utils"
import { PriceSetRuleType } from "../../../../src"
import { seedPriceData } from "../../../__fixtures__/seed-price-data"
import { CommonEvents, PricingEvents, composeMessage } from "@medusajs/utils"
jest.setTimeout(30000)
@@ -32,6 +37,16 @@ moduleIntegrationTestRunner({
MikroOrmWrapper,
service,
}: SuiteOptions<IPricingModuleService>) => {
let eventBusEmitSpy
beforeEach(() => {
eventBusEmitSpy = jest.spyOn(MockEventBusService.prototype, "emit")
})
afterEach(() => {
jest.clearAllMocks()
})
describe("PricingModule Service - PriceSet", () => {
beforeEach(async () => {
const testManager = await MikroOrmWrapper.forkManager()
@@ -422,6 +437,41 @@ moduleIntegrationTestRunner({
],
})
)
const [priceRules] = await service.listPriceRules({
price_set_id: [priceSet.id],
})
const events = eventBusEmitSpy.mock.calls[0][0]
expect(events).toHaveLength(3)
expect(events[0]).toEqual(
composeMessage(PricingEvents.price_set_created, {
service: Modules.PRICING,
action: CommonEvents.CREATED,
object: "price_set",
data: { id: priceSet.id },
})
)
expect(events[1]).toEqual(
composeMessage(PricingEvents.price_created, {
service: Modules.PRICING,
action: CommonEvents.CREATED,
object: "price",
data: { id: priceSet.prices![0].id },
})
)
expect(events[2]).toEqual(
composeMessage(PricingEvents.price_rule_created, {
service: Modules.PRICING,
action: CommonEvents.CREATED,
object: "price_rule",
data: {
id: priceRules.id,
},
})
)
})
it("should create a price set with money amounts with and without rules", async () => {
@@ -625,7 +675,7 @@ moduleIntegrationTestRunner({
const [priceSet] = await service.list(
{ id: ["price-set-1"] },
{ relations: ["prices"] }
{ relations: ["prices", "prices.price_rules"] }
)
expect(priceSet).toEqual(
@@ -639,6 +689,25 @@ moduleIntegrationTestRunner({
]),
})
)
const events = eventBusEmitSpy.mock.calls[0][0]
expect(events).toHaveLength(2)
expect(events[0]).toEqual(
composeMessage(PricingEvents.price_created, {
service: Modules.PRICING,
action: CommonEvents.CREATED,
object: "price",
data: { id: priceSet.prices![1].id },
})
)
expect(events[1]).toEqual(
composeMessage(PricingEvents.price_rule_created, {
service: Modules.PRICING,
action: CommonEvents.CREATED,
object: "price_rule",
data: { id: priceSet.prices![1].price_rules[0].id },
})
)
})
it("should add prices to multiple existing price set", async () => {