chore: Use module test runner in all modules (#6706)
I replaced the custom test setup with the test runner. This should make migrating to mikroorm 6 a lot easier once we do it. There are few more modules to be done, but I thought the PR is super big already so we can tackle them separately. Note that there are no or very little test changes, it is mostly the setup around the tests.
This commit is contained in:
+390
-410
@@ -1,464 +1,444 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { createCampaigns } from "../../../__fixtures__/campaigns"
|
||||
import { createPromotions } from "../../../__fixtures__/promotion"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Promotion Module Service: Campaigns", () => {
|
||||
let service: IPromotionModuleService
|
||||
let repositoryManager: SqlEntityManager
|
||||
let shutdownFunc: () => void
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.PROMOTION]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
repositoryManager = MikroOrmWrapper.forkManager()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("listAndCountCampaigns", () => {
|
||||
beforeEach(async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
})
|
||||
|
||||
it("should return all campaigns and its count", async () => {
|
||||
const [campaigns, count] = await service.listAndCountCampaigns()
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(campaigns).toEqual([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: expect.any(Date),
|
||||
ends_at: expect.any(Date),
|
||||
budget: expect.any(Object),
|
||||
created_at: expect.any(Date),
|
||||
updated_at: expect.any(Date),
|
||||
deleted_at: null,
|
||||
},
|
||||
{
|
||||
id: "campaign-id-2",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-2",
|
||||
starts_at: expect.any(Date),
|
||||
ends_at: expect.any(Date),
|
||||
budget: expect.any(Object),
|
||||
created_at: expect.any(Date),
|
||||
updated_at: expect.any(Date),
|
||||
deleted_at: null,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should return all campaigns based on config select and relations param", async () => {
|
||||
const [campaigns, count] = await service.listAndCountCampaigns(
|
||||
{
|
||||
id: ["campaign-id-1"],
|
||||
},
|
||||
{
|
||||
relations: ["budget"],
|
||||
select: ["name", "budget.limit"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(campaigns).toEqual([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
budget: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
campaign: expect.any(Object),
|
||||
limit: 1000,
|
||||
}),
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("createCampaigns", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toContain(
|
||||
"Value for Campaign.campaign_identifier is required, 'undefined' found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a basic campaign successfully", async () => {
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign(createdCampaign.id)
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PROMOTION,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPromotionModuleService>) => {
|
||||
describe("Promotion Module Service: Campaigns", () => {
|
||||
describe("listAndCountCampaigns", () => {
|
||||
beforeEach(async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a campaign with campaign budget successfully", async () => {
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
it("should return all campaigns and its count", async () => {
|
||||
const [campaigns, count] = await service.listAndCountCampaigns()
|
||||
|
||||
const [createdPromotion] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
budget: {
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
used: 10,
|
||||
},
|
||||
},
|
||||
])
|
||||
expect(count).toEqual(2)
|
||||
expect(campaigns).toEqual([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: expect.any(Date),
|
||||
ends_at: expect.any(Date),
|
||||
budget: expect.any(Object),
|
||||
created_at: expect.any(Date),
|
||||
updated_at: expect.any(Date),
|
||||
deleted_at: null,
|
||||
},
|
||||
{
|
||||
id: "campaign-id-2",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-2",
|
||||
starts_at: expect.any(Date),
|
||||
ends_at: expect.any(Date),
|
||||
budget: expect.any(Object),
|
||||
created_at: expect.any(Date),
|
||||
updated_at: expect.any(Date),
|
||||
deleted_at: null,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
const campaign = await service.retrieveCampaign(createdPromotion.id, {
|
||||
relations: ["budget"],
|
||||
it("should return all campaigns based on config select and relations param", async () => {
|
||||
const [campaigns, count] = await service.listAndCountCampaigns(
|
||||
{
|
||||
id: ["campaign-id-1"],
|
||||
},
|
||||
{
|
||||
relations: ["budget"],
|
||||
select: ["name", "budget.limit"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(campaigns).toEqual([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
budget: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
campaign: expect.any(Object),
|
||||
limit: 1000,
|
||||
}),
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
used: 10,
|
||||
}),
|
||||
describe("createCampaigns", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toContain(
|
||||
"Value for Campaign.campaign_identifier is required, 'undefined' found"
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a basic campaign with promotions successfully", async () => {
|
||||
await createPromotions(repositoryManager)
|
||||
it("should create a basic campaign successfully", async () => {
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
},
|
||||
])
|
||||
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
},
|
||||
])
|
||||
const campaign = await service.retrieveCampaign(createdCampaign.id)
|
||||
|
||||
const campaign = await service.retrieveCampaign(createdCampaign.id, {
|
||||
relations: ["promotions"],
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
promotions: [
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a campaign with campaign budget successfully", async () => {
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
|
||||
const [createdPromotion] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
budget: {
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
used: 10,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign(createdPromotion.id, {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-2",
|
||||
}),
|
||||
],
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
used: 10,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateCampaigns", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
.updateCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
it("should create a basic campaign with promotions successfully", async () => {
|
||||
await createPromotions(MikroOrmWrapper.forkManager())
|
||||
|
||||
expect(error.message).toContain('Campaign with id "" not found')
|
||||
})
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
},
|
||||
])
|
||||
|
||||
it("should update the attributes of a campaign successfully", async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
const campaign = await service.retrieveCampaign(createdCampaign.id, {
|
||||
relations: ["promotions"],
|
||||
})
|
||||
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should update the attributes of a campaign budget successfully", async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
budget: {
|
||||
limit: 100,
|
||||
used: 100,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
budget: expect.objectContaining({
|
||||
limit: 100,
|
||||
used: 100,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should update promotions of a campaign successfully", async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
await createPromotions(repositoryManager)
|
||||
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
},
|
||||
])
|
||||
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
promotions: [
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
promotions: [
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-2",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateCampaigns", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
.updateCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toContain('Campaign with id "" not found')
|
||||
})
|
||||
|
||||
it("should update the attributes of a campaign successfully", async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-2",
|
||||
}),
|
||||
],
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should remove promotions of the campaign successfully", async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
await createPromotions(repositoryManager)
|
||||
it("should update the attributes of a campaign budget successfully", async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
})
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
budget: {
|
||||
limit: 100,
|
||||
used: 100,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const updatedCampaign = await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
promotions: [{ id: "promotion-id-1" }],
|
||||
})
|
||||
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
promotions: [
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
],
|
||||
budget: expect.objectContaining({
|
||||
limit: 100,
|
||||
used: 100,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveCampaign", () => {
|
||||
beforeEach(async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
})
|
||||
it("should update promotions of a campaign successfully", async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
await createPromotions(MikroOrmWrapper.forkManager())
|
||||
|
||||
const id = "campaign-id-1"
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
},
|
||||
])
|
||||
|
||||
it("should return campaign for the given id", async () => {
|
||||
const campaign = await service.retrieveCampaign(id)
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
promotions: [
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-2",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when campaign with id does not exist", async () => {
|
||||
let error
|
||||
it("should remove promotions of the campaign successfully", async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
await createPromotions(MikroOrmWrapper.forkManager())
|
||||
|
||||
try {
|
||||
await service.retrieveCampaign("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
})
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"Campaign with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
const updatedCampaign = await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
promotions: [{ id: "promotion-id-1" }],
|
||||
})
|
||||
|
||||
it("should throw an error when a id is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveCampaign(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("campaign - id must be defined")
|
||||
})
|
||||
|
||||
it("should return campaign based on config select param", async () => {
|
||||
const campaign = await service.retrieveCampaign(id, {
|
||||
select: ["id"],
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
promotions: [
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(campaign))
|
||||
describe("retrieveCampaign", () => {
|
||||
beforeEach(async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
const id = "campaign-id-1"
|
||||
|
||||
describe("deleteCampaigns", () => {
|
||||
it("should delete the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
it("should return campaign for the given id", async () => {
|
||||
const campaign = await service.retrieveCampaign(id)
|
||||
|
||||
await service.deleteCampaigns([createdCampaign.id])
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
const campaigns = await service.listCampaigns(
|
||||
{
|
||||
id: [createdCampaign.id],
|
||||
},
|
||||
{ withDeleted: true }
|
||||
)
|
||||
it("should throw an error when campaign with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
try {
|
||||
await service.retrieveCampaign("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
describe("softDeleteCampaigns", () => {
|
||||
it("should soft delete the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
expect(error.message).toEqual(
|
||||
"Campaign with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
await service.softDeleteCampaigns([createdCampaign.id])
|
||||
it("should throw an error when a id is not provided", async () => {
|
||||
let error
|
||||
|
||||
const campaigns = await service.listCampaigns({
|
||||
id: [createdCampaign.id],
|
||||
try {
|
||||
await service.retrieveCampaign(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("campaign - id must be defined")
|
||||
})
|
||||
|
||||
it("should return campaign based on config select param", async () => {
|
||||
const campaign = await service.retrieveCampaign(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(campaign))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
describe("deleteCampaigns", () => {
|
||||
it("should delete the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
await service.deleteCampaigns([createdCampaign.id])
|
||||
|
||||
const campaigns = await service.listCampaigns(
|
||||
{
|
||||
id: [createdCampaign.id],
|
||||
},
|
||||
{ withDeleted: true }
|
||||
)
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("softDeleteCampaigns", () => {
|
||||
it("should soft delete the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
await service.softDeleteCampaigns([createdCampaign.id])
|
||||
|
||||
const campaigns = await service.listCampaigns({
|
||||
id: [createdCampaign.id],
|
||||
})
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("restoreCampaigns", () => {
|
||||
it("should restore the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
await service.softDeleteCampaigns([createdCampaign.id])
|
||||
|
||||
let campaigns = await service.listCampaigns({
|
||||
id: [createdCampaign.id],
|
||||
})
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
await service.restoreCampaigns([createdCampaign.id])
|
||||
|
||||
campaigns = await service.listCampaigns({ id: [createdCampaign.id] })
|
||||
expect(campaigns).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("restoreCampaigns", () => {
|
||||
it("should restore the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
await service.softDeleteCampaigns([createdCampaign.id])
|
||||
|
||||
let campaigns = await service.listCampaigns({ id: [createdCampaign.id] })
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
await service.restoreCampaigns([createdCampaign.id])
|
||||
|
||||
campaigns = await service.listCampaigns({ id: [createdCampaign.id] })
|
||||
expect(campaigns).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
+4712
-4722
File diff suppressed because it is too large
Load Diff
+1407
-1413
File diff suppressed because it is too large
Load Diff
+162
-182
@@ -1,209 +1,189 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { createCampaigns } from "../../../__fixtures__/campaigns"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Promotion Service: campaign usage", () => {
|
||||
let service: IPromotionModuleService
|
||||
let repositoryManager: SqlEntityManager
|
||||
let shutdownFunc: () => void
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.PROMOTION]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
repositoryManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
await createCampaigns(repositoryManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("registerUsage", () => {
|
||||
it("should register usage for type spend", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_SPEND",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-1",
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PROMOTION,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPromotionModuleService>) => {
|
||||
describe("Promotion Service: campaign usage", () => {
|
||||
beforeEach(async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
describe("registerUsage", () => {
|
||||
it("should register usage for type spend", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_SPEND",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-1",
|
||||
})
|
||||
|
||||
const campaign = await service.retrieveCampaign("campaign-id-1", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
|
||||
expect(campaign.budget).toEqual(
|
||||
expect.objectContaining({
|
||||
type: "spend",
|
||||
limit: 1000,
|
||||
used: 700,
|
||||
const campaign = await service.retrieveCampaign("campaign-id-1", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign.budget).toEqual(
|
||||
expect.objectContaining({
|
||||
type: "spend",
|
||||
limit: 1000,
|
||||
used: 700,
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should register usage for type usage", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_USAGE",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-2",
|
||||
})
|
||||
it("should register usage for type usage", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_USAGE",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-2",
|
||||
})
|
||||
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign("campaign-id-2", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
const campaign = await service.retrieveCampaign("campaign-id-2", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign.budget).toEqual(
|
||||
expect.objectContaining({
|
||||
type: "usage",
|
||||
limit: 1000,
|
||||
used: 1,
|
||||
expect(campaign.budget).toEqual(
|
||||
expect.objectContaining({
|
||||
type: "usage",
|
||||
limit: 1000,
|
||||
used: 1,
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should not throw an error when compute action with code does not exist", async () => {
|
||||
const response = await service
|
||||
.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: "DOESNOTEXIST",
|
||||
},
|
||||
])
|
||||
.catch((e) => e)
|
||||
it("should not throw an error when compute action with code does not exist", async () => {
|
||||
const response = await service
|
||||
.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: "DOESNOTEXIST",
|
||||
},
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response).toEqual(undefined)
|
||||
})
|
||||
|
||||
it("should not register usage when limit is exceed for type usage", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_USAGE",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-2",
|
||||
})
|
||||
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-2",
|
||||
budget: { used: 1000, limit: 1000 },
|
||||
})
|
||||
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign("campaign-id-2", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
used: 1000,
|
||||
}),
|
||||
expect(response).toEqual(undefined)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should not register usage above limit when exceeded for type spend", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_SPEND",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-1",
|
||||
})
|
||||
it("should not register usage when limit is exceed for type usage", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_USAGE",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-2",
|
||||
})
|
||||
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
budget: { used: 900, limit: 1000 },
|
||||
})
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-2",
|
||||
budget: { used: 1000, limit: 1000 },
|
||||
})
|
||||
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 100,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 100,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign("campaign-id-1", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
const campaign = await service.retrieveCampaign("campaign-id-2", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
used: 1000,
|
||||
}),
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
used: 1000,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
|
||||
it("should not register usage above limit when exceeded for type spend", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_SPEND",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-1",
|
||||
})
|
||||
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
budget: { used: 900, limit: 1000 },
|
||||
})
|
||||
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 100,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 100,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign("campaign-id-1", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
used: 1000,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user