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:
Stevche Radevski
2024-03-14 21:01:50 +00:00
committed by GitHub
parent 1eb90f739b
commit 68d869607f
102 changed files with 21692 additions and 25473 deletions
@@ -1,353 +0,0 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { MoneyAmount } from "@models"
import { MoneyAmountService } from "@services"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { MikroOrmWrapper } from "../../../utils"
jest.setTimeout(30000)
describe("MoneyAmount Service", () => {
let service: MoneyAmountService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
let data!: MoneyAmount[]
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("moneyAmountService")
testManager = await MikroOrmWrapper.forkManager()
data = await createMoneyAmounts(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("list", () => {
it("should list all moneyAmounts", async () => {
const moneyAmountsResult = await service.list()
expect(JSON.parse(JSON.stringify(moneyAmountsResult))).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "money-amount-USD",
amount: 500,
}),
expect.objectContaining({
id: "money-amount-EUR",
amount: 400,
}),
expect.objectContaining({
id: "money-amount-CAD",
amount: 600,
}),
])
)
})
it("should list moneyAmounts by id", async () => {
const moneyAmountsResult = await service.list({
id: ["money-amount-USD"],
})
expect(JSON.parse(JSON.stringify(moneyAmountsResult))).toEqual([
expect.objectContaining({
id: "money-amount-USD",
}),
])
})
it("should list moneyAmounts with relations and selects", async () => {
const moneyAmountsResult = await service.list(
{
id: ["money-amount-USD"],
},
{
select: ["id", "min_quantity", "currency_code", "amount"],
}
)
const serialized = JSON.parse(JSON.stringify(moneyAmountsResult))
expect(serialized).toEqual([
{
id: "money-amount-USD",
amount: 500,
min_quantity: "1",
currency_code: "USD",
},
])
})
it("should list moneyAmounts scoped by currency_code", async () => {
const moneyAmountsResult = await service.list(
{
currency_code: ["USD"],
},
{
select: ["id", "min_quantity", "currency_code", "amount"],
}
)
const serialized = JSON.parse(JSON.stringify(moneyAmountsResult))
expect(serialized).toEqual([
{
id: "money-amount-USD",
min_quantity: "1",
currency_code: "USD",
amount: 500,
},
])
})
})
describe("listAndCount", () => {
it("should return moneyAmounts and count", async () => {
const [moneyAmountsResult, count] = await service.listAndCount()
expect(count).toEqual(3)
expect(JSON.parse(JSON.stringify(moneyAmountsResult))).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "money-amount-USD",
}),
expect.objectContaining({
id: "money-amount-EUR",
}),
expect.objectContaining({
id: "money-amount-CAD",
}),
])
)
})
it("should return moneyAmounts and count when filtered", async () => {
const [moneyAmountsResult, count] = await service.listAndCount({
id: ["money-amount-USD"],
})
expect(count).toEqual(1)
expect(moneyAmountsResult).toEqual([
expect.objectContaining({
id: "money-amount-USD",
}),
])
})
it("list moneyAmounts with relations and selects", async () => {
const [moneyAmountsResult, count] = await service.listAndCount(
{
id: ["money-amount-USD"],
},
{
select: ["id", "min_quantity", "currency_code", "amount"],
}
)
const serialized = JSON.parse(JSON.stringify(moneyAmountsResult))
expect(count).toEqual(1)
expect(serialized).toEqual([
{
id: "money-amount-USD",
amount: 500,
min_quantity: "1",
currency_code: "USD",
},
])
})
it("should return moneyAmounts and count when using skip and take", async () => {
const [moneyAmountsResult, count] = await service.listAndCount(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(3)
expect(moneyAmountsResult).toEqual([
expect.objectContaining({
id: "money-amount-EUR",
}),
])
})
it("should return requested fields", async () => {
const [moneyAmountsResult, count] = await service.listAndCount(
{},
{
take: 1,
select: ["id", "amount"],
}
)
const serialized = JSON.parse(JSON.stringify(moneyAmountsResult))
expect(count).toEqual(3)
expect(serialized).toEqual([
{
id: "money-amount-CAD",
amount: 600,
},
])
})
})
describe("retrieve", () => {
const id = "money-amount-USD"
const amount = 500
it("should return moneyAmount for the given id", async () => {
const moneyAmount = await service.retrieve(id)
expect(moneyAmount).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when moneyAmount with id does not exist", async () => {
let error
try {
await service.retrieve("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"MoneyAmount with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrieve(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("moneyAmount - id must be defined")
})
it("should return moneyAmount based on config select param", async () => {
const moneyAmount = await service.retrieve(id, {
select: ["id", "amount"],
})
const serialized = JSON.parse(JSON.stringify(moneyAmount))
expect(serialized).toEqual({
id,
amount,
})
})
})
describe("delete", () => {
const id = "money-amount-USD"
it("should delete the moneyAmounts given an id successfully", async () => {
await service.delete([id])
const moneyAmounts = await service.list({
id: [id],
})
expect(moneyAmounts).toHaveLength(0)
})
})
describe("update", () => {
const id = "money-amount-USD"
it("should update the amount of the moneyAmount successfully", async () => {
await service.update([
{
id,
amount: 700,
},
])
const moneyAmount = JSON.parse(JSON.stringify(await service.retrieve(id)))
expect(moneyAmount.amount).toEqual(700)
})
it("should update the currency of the moneyAmount successfully", async () => {
await service.update([
{
id,
currency_code: "EUR",
},
])
const moneyAmount = await service.retrieve(id)
expect(moneyAmount.currency_code).toEqual("EUR")
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.update([
{
id: "does-not-exist",
amount: 666,
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'MoneyAmount with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should create a moneyAmount successfully", async () => {
await service.create([
{
id: "money-amount-TESM",
currency_code: "USD",
amount: 333,
min_quantity: 1,
max_quantity: 4,
},
])
const [moneyAmount] = await service.list({
id: ["money-amount-TESM"],
})
expect(JSON.parse(JSON.stringify(moneyAmount))).toEqual(
expect.objectContaining({
id: "money-amount-TESM",
currency_code: "USD",
amount: 333,
min_quantity: "1",
max_quantity: "4",
})
)
})
})
})
@@ -1,243 +0,0 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceListRuleService } from "@services"
import { createPriceLists } from "../../../__fixtures__/price-list"
import { createPriceListRules } from "../../../__fixtures__/price-list-rules"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { MikroOrmWrapper } from "../../../utils"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
jest.setTimeout(30000)
describe("PriceListRule Service", () => {
let service: PriceListRuleService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("priceListRuleService")
testManager = await MikroOrmWrapper.forkManager()
await createRuleTypes(testManager)
await createPriceLists(testManager)
await createPriceListRules(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("list", () => {
it("should list all priceListRules", async () => {
const priceListRuleResult = await service.list()
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
it("should list priceListRules scoped by priceListRule id", async () => {
const priceListRuleResult = await service.list({
id: ["price-list-rule-1"],
})
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
])
})
})
describe("listAndCount", () => {
it("should return pricelistrules and count", async () => {
const [priceListRuleResult, count] = await service.listAndCount()
expect(count).toEqual(2)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
it("should return pricelistrules and count when filtered", async () => {
const [priceListRuleResult, count] = await service.listAndCount({
id: ["price-list-rule-1"],
})
expect(count).toEqual(1)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
])
})
it("should return pricelistrules and count when using skip and take", async () => {
const [priceListRuleResult, count] = await service.listAndCount(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(2)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
it("should return requested fields", async () => {
const [priceListRuleResult, count] = await service.listAndCount(
{},
{
take: 1,
select: ["id"],
}
)
const serialized = JSON.parse(JSON.stringify(priceListRuleResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "price-list-rule-1",
},
])
})
})
describe("retrieve", () => {
const id = "price-list-rule-1"
it("should return priceList for the given id", async () => {
const priceListRuleResult = await service.retrieve(id)
expect(priceListRuleResult).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when priceListRule with id does not exist", async () => {
let error
try {
await service.retrieve("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceListRule with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrieve(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("priceListRule - id must be defined")
})
})
describe("delete", () => {
const id = "price-list-rule-1"
it("should delete the pricelists given an id successfully", async () => {
await service.delete([id])
const priceListResult = await service.list({
id: [id],
})
expect(priceListResult).toHaveLength(0)
})
})
describe("update", () => {
const id = "price-list-rule-2"
it("should update the value of the priceListRule successfully", async () => {
await service.update([
{
id,
price_list_id: "price-list-1",
},
])
const priceList = await service.retrieve(id, {
relations: ["price_list"],
})
expect(priceList.price_list.id).toEqual("price-list-1")
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.update([
{
id: "does-not-exist",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceListRule with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should create a priceListRule successfully", async () => {
const [created] = await service.create([
{
price_list_id: "price-list-2",
rule_type_id: "rule-type-1",
},
])
const [priceListRule] = await service.list(
{
id: [created.id],
},
{
relations: ["price_list", "rule_type"],
select: ["price_list.id", "rule_type.id"],
}
)
expect(priceListRule.price_list.id).toEqual("price-list-2")
expect(priceListRule.rule_type.id).toEqual("rule-type-1")
})
})
})
@@ -1,230 +0,0 @@
import { MikroOrmWrapper } from "../../../utils"
import { PriceListService } from "@services"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createPriceLists } from "../../../__fixtures__/price-list"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
jest.setTimeout(30000)
describe("PriceList Service", () => {
let service: PriceListService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("priceListService")
testManager = await MikroOrmWrapper.forkManager()
await createPriceLists(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("list", () => {
it("should return list priceLists", async () => {
const priceListResult = await service.list()
expect(priceListResult).toEqual([
expect.objectContaining({
id: "price-list-1",
}),
expect.objectContaining({
id: "price-list-2",
}),
])
})
it("should list pricelists by id", async () => {
const priceListResult = await service.list({
id: ["price-list-1"],
})
expect(priceListResult).toEqual([
expect.objectContaining({
id: "price-list-1",
}),
])
})
})
describe("listAndCount", () => {
it("should return pricelists and count", async () => {
const [priceListResult, count] = await service.listAndCount()
expect(count).toEqual(2)
expect(priceListResult).toEqual([
expect.objectContaining({
id: "price-list-1",
}),
expect.objectContaining({
id: "price-list-2",
}),
])
})
it("should return pricelists and count when filtered", async () => {
const [priceListResult, count] = await service.listAndCount({
id: ["price-list-1"],
})
expect(count).toEqual(1)
expect(priceListResult).toEqual([
expect.objectContaining({
id: "price-list-1",
}),
])
})
it("should return pricelists and count when using skip and take", async () => {
const [priceListResult, count] = await service.listAndCount(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(2)
expect(priceListResult).toEqual([
expect.objectContaining({
id: "price-list-2",
}),
])
})
it("should return requested fields", async () => {
const [priceListResult, count] = await service.listAndCount(
{},
{
take: 1,
select: ["id"],
}
)
const serialized = JSON.parse(JSON.stringify(priceListResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "price-list-1",
},
])
})
})
describe("retrieve", () => {
const id = "price-list-1"
it("should return priceList for the given id", async () => {
const priceListResult = await service.retrieve(id)
expect(priceListResult).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when priceList with id does not exist", async () => {
let error
try {
await service.retrieve("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceList with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrieve(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("priceList - id must be defined")
})
})
describe("delete", () => {
const id = "price-list-1"
it("should delete the pricelists given an id successfully", async () => {
await service.delete([id])
const priceListResult = await service.list({
id: [id],
})
expect(priceListResult).toHaveLength(0)
})
})
describe("update", () => {
const id = "price-list-2"
it("should update the starts_at date of the priceList successfully", async () => {
const updateDate = new Date()
await service.update([
{
id,
starts_at: updateDate,
},
])
const priceList = await service.retrieve(id)
expect(priceList.starts_at).toEqual(updateDate)
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.update([
{
id: "does-not-exist",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceList with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should create a priceList successfully", async () => {
const [created] = await service.create([
{
title: "test",
description: "test",
},
])
const [priceList] = await service.list({
id: [created.id],
})
expect(priceList.title).toEqual("test")
})
})
})
@@ -1,331 +0,0 @@
import { PriceSetMoneyAmount } from "@models"
import { CreatePriceRuleDTO } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceRuleService } from "@services"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { MikroOrmWrapper } from "../../../utils"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
jest.setTimeout(30000)
describe("PriceRule Service", () => {
let service: PriceRuleService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
testManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("priceRuleService")
await createMoneyAmounts(testManager)
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceSetMoneyAmountRules(testManager)
await createPriceRules(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("list", () => {
it("should list priceRules", async () => {
const priceRuleResult = await service.list()
const serialized = JSON.parse(JSON.stringify(priceRuleResult))
expect(serialized).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
expect.objectContaining({
id: "price-rule-2",
}),
])
})
it("should list priceRules by id", async () => {
const priceRuleResult = await service.list({
id: ["price-rule-1"],
})
expect(priceRuleResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
])
})
it("should list priceRules with relations and selects", async () => {
const priceRulesResult = await service.list(
{
id: ["price-rule-1"],
},
{
select: ["id", "price_set.id"],
relations: ["price_set"],
}
)
const serialized = JSON.parse(JSON.stringify(priceRulesResult))
expect(serialized).toEqual([
{
id: "price-rule-1",
price_set: {
id: "price-set-1",
},
},
])
})
describe("listAndCount", () => {
it("should return priceRules and count", async () => {
const [priceRulesResult, count] = await service.listAndCount()
expect(count).toEqual(2)
expect(priceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
expect.objectContaining({
id: "price-rule-2",
}),
])
})
it("should return priceRules and count when filtered", async () => {
const [priceRulesResult, count] = await service.listAndCount({
id: ["price-rule-1"],
})
expect(count).toEqual(1)
expect(priceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
])
})
it("should list priceRules with relations and selects", async () => {
const [priceRulesResult, count] = await service.listAndCount(
{
id: ["price-rule-1"],
},
{
select: ["id", "price_set.id"],
relations: ["price_set"],
}
)
const serialized = JSON.parse(JSON.stringify(priceRulesResult))
expect(count).toEqual(1)
expect(serialized).toEqual([
{
id: "price-rule-1",
price_set: {
id: "price-set-1",
},
},
])
})
it("should return priceRules and count when using skip and take", async () => {
const [priceRulesResult, count] = await service.listAndCount(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(2)
expect(priceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-2",
}),
])
})
it("should return requested fields", async () => {
const [priceRulesResult, count] = await service.listAndCount(
{},
{
take: 1,
select: ["id"],
}
)
const serialized = JSON.parse(JSON.stringify(priceRulesResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "price-rule-1",
},
])
})
})
describe("retrieve", () => {
const id = "price-rule-1"
it("should return priceRule for the given id", async () => {
const priceRule = await service.retrieve(id)
expect(priceRule).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when priceRule with id does not exist", async () => {
let error
try {
await service.retrieve("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceRule with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrieve(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("priceRule - id must be defined")
})
it("should return priceRule based on config select param", async () => {
const priceRule = await service.retrieve(id, {
select: ["id"],
})
const serialized = JSON.parse(JSON.stringify(priceRule))
expect(serialized).toEqual({
id,
})
})
})
describe("delete", () => {
const id = "price-set-1"
it("should delete the priceRules given an id successfully", async () => {
await service.delete([id])
const priceRules = await service.list({
id: [id],
})
expect(priceRules).toHaveLength(0)
})
})
describe("update", () => {
const id = "price-set-1"
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.update([
{
id: "does-not-exist",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceRule with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.update([
{
random: "does-not-exist",
} as any,
])
} catch (e) {
error = e
}
expect(error.message).toEqual('PriceRule with id "" not found')
})
it("should create a priceRule successfully", async () => {
const [ma] = await createMoneyAmounts(testManager, [
{
amount: 100,
currency_code: "EUR",
},
])
const psma: PriceSetMoneyAmount = testManager.create(
PriceSetMoneyAmount,
{
price_set: "price-set-1",
money_amount: ma.id,
title: "test",
}
)
await testManager.persist(psma).flush()
await service.create([
{
id: "price-rule-new",
price_set_id: "price-set-1",
rule_type_id: "rule-type-1",
value: "region_1",
price_list_id: "test",
price_set_money_amount_id: psma.id,
} as unknown as CreatePriceRuleDTO,
])
const [pricerule] = await service.list({
id: ["price-rule-new"],
})
expect(pricerule).toEqual(
expect.objectContaining({
id: "price-rule-new",
} as unknown as CreatePriceRuleDTO)
)
})
})
})
})
@@ -1,289 +0,0 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceSetMoneyAmountRulesService } from "@services"
import { seedPriceData } from "../../../__fixtures__/seed-price-data"
import { MikroOrmWrapper } from "../../../utils"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
jest.setTimeout(30000)
describe("PriceSetMoneyAmountRules Service", () => {
let service: PriceSetMoneyAmountRulesService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("priceSetMoneyAmountRulesService")
testManager = await MikroOrmWrapper.forkManager()
await seedPriceData(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("list", () => {
it("should list psmar records", async () => {
const priceSetMoneyAmountRulesResult = await service.list()
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
expect.objectContaining({
id: "psmar-2",
}),
expect.objectContaining({
id: "psmar-3",
}),
])
})
it("should list psmar record by id", async () => {
const priceSetMoneyAmountRulesResult = await service.list({
id: ["psmar-1"],
})
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
])
})
})
describe("listAndCount", () => {
it("should return psmar records and count", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCount()
expect(count).toEqual(3)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
expect.objectContaining({
id: "psmar-2",
}),
expect.objectContaining({
id: "psmar-3",
}),
])
})
it("should return psmar records and count when filtered", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCount({
id: ["psmar-1"],
})
expect(count).toEqual(1)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
])
})
it("should return psmar and count when using skip and take", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCount({}, { skip: 1, take: 1 })
expect(count).toEqual(3)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-2",
}),
])
})
it("should return requested fields", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCount(
{},
{
take: 1,
select: ["value"],
}
)
const serialized = JSON.parse(
JSON.stringify(priceSetMoneyAmountRulesResult)
)
expect(count).toEqual(3)
expect(serialized).toEqual([
{
id: "psmar-1",
value: "EUR",
},
])
})
})
describe("retrieve", () => {
it("should return priceSetMoneyAmountRules for the given id", async () => {
const priceSetMoneyAmountRules = await service.retrieve("psmar-1")
expect(priceSetMoneyAmountRules).toEqual(
expect.objectContaining({
id: "psmar-1",
})
)
})
it("should throw an error when priceSetMoneyAmountRules with id does not exist", async () => {
let error
try {
await service.retrieve("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceSetMoneyAmountRules with id: does-not-exist was not found"
)
})
it("should throw an error when an id is not provided", async () => {
let error
try {
await service.retrieve(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual(
"priceSetMoneyAmountRules - id must be defined"
)
})
it("should return priceSetMoneyAmountRules based on config select param", async () => {
const priceSetMoneyAmountRulesResult = await service.retrieve("psmar-1", {
select: ["value"],
})
const serialized = JSON.parse(
JSON.stringify(priceSetMoneyAmountRulesResult)
)
expect(serialized).toEqual({
value: "EUR",
id: "psmar-1",
})
})
})
describe("delete", () => {
const id = "psmar-1"
it("should delete the priceSetMoneyAmountRules given an id successfully", async () => {
await service.delete([id])
const priceSetMoneyAmountRules = await service.list({
id: [id],
})
expect(priceSetMoneyAmountRules).toHaveLength(0)
})
})
describe("update", () => {
const id = "psmar-1"
it("should update the value of the priceSetMoneyAmountRules successfully", async () => {
await service.update([
{
id,
value: "New value",
price_set_money_amount: "price-set-money-amount-CAD",
rule_type: "rule-type-2",
},
])
const psmar = await service.retrieve(id, {
relations: ["price_set_money_amount", "rule_type"],
})
expect(psmar).toEqual(
expect.objectContaining({
id,
value: "New value",
price_set_money_amount: expect.objectContaining({
id: "price-set-money-amount-CAD",
}),
rule_type: expect.objectContaining({
id: "rule-type-2",
}),
})
)
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.update([
{
id: "does-not-exist",
value: "random value",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceSetMoneyAmountRules with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should create a priceSetMoneyAmountRules successfully", async () => {
const created = await service.create([
{
price_set_money_amount: "price-set-money-amount-EUR",
rule_type: "rule-type-2",
value: "New priceSetMoneyAmountRule",
},
])
const [priceSetMoneyAmountRules] = await service.list(
{
id: [created[0]?.id],
},
{
relations: ["price_set_money_amount", "rule_type"],
}
)
expect(priceSetMoneyAmountRules).toEqual(
expect.objectContaining({
id: created[0]?.id,
value: "New priceSetMoneyAmountRule",
price_set_money_amount: expect.objectContaining({
id: "price-set-money-amount-EUR",
}),
rule_type: expect.objectContaining({
id: "rule-type-2",
}),
})
)
})
})
})
@@ -1,392 +0,0 @@
import { CreatePriceSetDTO } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { MoneyAmount, PriceSet } from "@models"
import { PriceSetService } from "@services"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { MikroOrmWrapper } from "../../../utils"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
jest.setTimeout(30000)
describe("PriceSet Service", () => {
let service: PriceSetService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
let data!: PriceSet[]
let moneyAmountsData!: MoneyAmount[]
const moneyAmountsInputData = [
{
id: "money-amount-USD",
currency_code: "USD",
amount: 500,
min_quantity: 1,
max_quantity: 10,
},
]
const priceSetInputData = [
{
id: "price-set-1",
prices: [
{
id: "money-amount-USD",
currency_code: "EUR",
amount: 100,
},
],
},
{
id: "price-set-2",
prices: [],
},
{
id: "price-set-3",
prices: [],
},
]
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
testManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("priceSetService")
moneyAmountsData = await createMoneyAmounts(
testManager,
moneyAmountsInputData
)
data = await createPriceSets(testManager, priceSetInputData)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("list", () => {
it("should list priceSets", async () => {
const priceSetsResult = await service.list()
const serialized = JSON.parse(JSON.stringify(priceSetsResult))
expect(serialized).toEqual([
expect.objectContaining({
id: "price-set-1",
}),
expect.objectContaining({
id: "price-set-2",
}),
expect.objectContaining({
id: "price-set-3",
}),
])
})
it("should list priceSets by id", async () => {
const priceSetsResult = await service.list({
id: ["price-set-1"],
})
expect(priceSetsResult).toEqual([
expect.objectContaining({
id: "price-set-1",
}),
])
})
it("should list priceSets with relations and selects", async () => {
const priceSetsResult = await service.list(
{
id: ["price-set-1"],
},
{
select: ["id", "money_amounts.id"],
relations: ["money_amounts"],
}
)
const serialized = JSON.parse(JSON.stringify(priceSetsResult))
expect(serialized).toEqual([
{
id: "price-set-1",
money_amounts: [
expect.objectContaining({
id: "money-amount-USD",
}),
],
},
])
})
it("should scope priceSets with currency_code of money amounts", async () => {
const priceSetsResult = await service.list(
{
money_amounts: {
currency_code: ["USD"],
},
},
{
select: ["id", "money_amounts.id"],
relations: ["money_amounts"],
}
)
const serialized = JSON.parse(JSON.stringify(priceSetsResult))
expect(serialized).toEqual([
{
id: "price-set-1",
money_amounts: [
expect.objectContaining({
id: "money-amount-USD",
}),
],
},
])
})
})
it("should not return price sets if money amounts with a currency code dont exist", async () => {
const priceSetsResult = await service.list(
{
money_amounts: {
currency_code: ["DOESNOTEXIST"],
},
},
{
select: ["id", "money_amounts.id"],
relations: ["money_amounts"],
}
)
const serialized = JSON.parse(JSON.stringify(priceSetsResult))
expect(serialized).toEqual([])
})
describe("listAndCount", () => {
it("should return priceSets and count", async () => {
const [priceSetsResult, count] = await service.listAndCount()
expect(count).toEqual(3)
expect(priceSetsResult).toEqual([
expect.objectContaining({
id: "price-set-1",
}),
expect.objectContaining({
id: "price-set-2",
}),
expect.objectContaining({
id: "price-set-3",
}),
])
})
it("should return priceSets and count when filtered", async () => {
const [priceSetsResult, count] = await service.listAndCount({
id: ["price-set-1"],
})
expect(count).toEqual(1)
expect(priceSetsResult).toEqual([
expect.objectContaining({
id: "price-set-1",
}),
])
})
it("should list priceSets with relations and selects", async () => {
const [priceSetsResult, count] = await service.listAndCount(
{
id: ["price-set-1"],
},
{
select: ["id", "min_quantity", "money_amounts.id"],
relations: ["money_amounts"],
}
)
const serialized = JSON.parse(JSON.stringify(priceSetsResult))
expect(count).toEqual(1)
expect(serialized).toEqual([
{
id: "price-set-1",
money_amounts: [
expect.objectContaining({
id: "money-amount-USD",
}),
],
},
])
})
it("should return priceSets and count when using skip and take", async () => {
const [priceSetsResult, count] = await service.listAndCount(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(3)
expect(priceSetsResult).toEqual([
expect.objectContaining({
id: "price-set-2",
}),
])
})
it("should return requested fields", async () => {
const [priceSetsResult, count] = await service.listAndCount(
{},
{
take: 1,
select: ["id"],
}
)
const serialized = JSON.parse(JSON.stringify(priceSetsResult))
expect(count).toEqual(3)
expect(serialized).toEqual([
{
id: "price-set-1",
},
])
})
})
describe("retrieve", () => {
const id = "price-set-1"
it("should return priceSet for the given id", async () => {
const priceSet = await service.retrieve(id)
expect(priceSet).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when priceSet with id does not exist", async () => {
let error
try {
await service.retrieve("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceSet with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrieve(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("priceSet - id must be defined")
})
it("should return priceSet based on config select param", async () => {
const priceSet = await service.retrieve(id, {
select: ["id"],
})
const serialized = JSON.parse(JSON.stringify(priceSet))
expect(serialized).toEqual({
id,
})
})
})
describe("delete", () => {
const id = "price-set-1"
it("should delete the priceSets given an id successfully", async () => {
await service.delete([id])
const priceSets = await service.list({
id: [id],
})
expect(priceSets).toHaveLength(0)
})
})
describe("update", () => {
const id = "price-set-1"
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.update([
{
id: "does-not-exist",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceSet with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.update([
{
random: "does-not-exist",
} as any,
])
} catch (e) {
error = e
}
expect(error.message).toEqual('PriceSet with id "" not found')
})
it("should create a priceSet successfully", async () => {
await service.create([
{
id: "price-set-new",
} as unknown as CreatePriceSetDTO,
])
const [priceSet] = await service.list({
id: ["price-set-new"],
})
expect(priceSet).toEqual(
expect.objectContaining({
id: "price-set-new",
} as unknown as CreatePriceSetDTO)
)
})
})
})
@@ -1,436 +1,415 @@
import { Modules } from "@medusajs/modules-sdk"
import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { MoneyAmount } from "@models"
import { initModules } from "medusa-test-utils"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { MikroOrmWrapper } from "../../../utils"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
jest.setTimeout(30000)
describe("PricingModule Service - MoneyAmount", () => {
let service: IPricingModuleService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
let data!: MoneyAmount[]
let shutdownFunc: () => Promise<void>
beforeAll(async () => {
const initModulesConfig = getInitModuleConfig()
const { medusaApp, shutdown } = await initModules(initModulesConfig)
service = medusaApp.modules[Modules.PRICING]
shutdownFunc = shutdown
})
afterAll(async () => {
await shutdownFunc()
})
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = MikroOrmWrapper.forkManager()
testManager = MikroOrmWrapper.forkManager()
data = await createMoneyAmounts(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("listMoneyAmounts", () => {
it("list moneyAmounts", async () => {
const moneyAmountsResult = await service.listMoneyAmounts()
expect(moneyAmountsResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "money-amount-USD",
amount: 500,
}),
expect.objectContaining({
id: "money-amount-EUR",
amount: 400,
}),
expect.objectContaining({
id: "money-amount-CAD",
amount: 600,
}),
])
)
})
it("should list moneyAmounts by id", async () => {
const moneyAmountsResult = await service.listMoneyAmounts({
id: ["money-amount-USD"],
moduleIntegrationTestRunner({
moduleName: Modules.PRICING,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPricingModuleService>) => {
describe("PricingModule Service - MoneyAmount", () => {
let testManager: SqlEntityManager
beforeEach(async () => {
testManager = await MikroOrmWrapper.forkManager()
await createMoneyAmounts(testManager)
})
expect(moneyAmountsResult).toEqual([
expect.objectContaining({
id: "money-amount-USD",
}),
])
})
describe("listMoneyAmounts", () => {
it("list moneyAmounts", async () => {
const moneyAmountsResult = await service.listMoneyAmounts()
it("should list moneyAmounts with relations and selects", async () => {
const moneyAmountsResult = await service.listMoneyAmounts(
{
id: ["money-amount-USD"],
},
{
select: ["id", "min_quantity", "currency_code"],
}
)
const serialized = JSON.parse(JSON.stringify(moneyAmountsResult))
expect(serialized).toEqual([
{
id: "money-amount-USD",
amount: null,
min_quantity: "1",
currency_code: "USD",
},
])
})
})
describe("listAndCountMoneyAmounts", () => {
it("should return moneyAmounts and count", async () => {
const [moneyAmountsResult, count] =
await service.listAndCountMoneyAmounts()
expect(count).toEqual(3)
expect(moneyAmountsResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "money-amount-USD",
}),
expect.objectContaining({
id: "money-amount-EUR",
}),
expect.objectContaining({
id: "money-amount-CAD",
}),
])
)
})
it("should return moneyAmounts and count when filtered", async () => {
const [moneyAmountsResult, count] =
await service.listAndCountMoneyAmounts({
id: ["money-amount-USD"],
expect(moneyAmountsResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "money-amount-USD",
amount: 500,
}),
expect.objectContaining({
id: "money-amount-EUR",
amount: 400,
}),
expect.objectContaining({
id: "money-amount-CAD",
amount: 600,
}),
])
)
})
expect(count).toEqual(1)
expect(moneyAmountsResult).toEqual([
expect.objectContaining({
id: "money-amount-USD",
}),
])
})
it("list moneyAmounts with relations and selects", async () => {
const [moneyAmountsResult, count] =
await service.listAndCountMoneyAmounts(
{
it("should list moneyAmounts by id", async () => {
const moneyAmountsResult = await service.listMoneyAmounts({
id: ["money-amount-USD"],
},
{
select: ["id", "min_quantity", "currency_code", "amount"],
}
)
})
const serialized = JSON.parse(JSON.stringify(moneyAmountsResult))
expect(count).toEqual(1)
expect(serialized).toEqual([
{
id: "money-amount-USD",
amount: 500,
min_quantity: "1",
currency_code: "USD",
},
])
})
it("should return moneyAmounts and count when using skip and take", async () => {
const [moneyAmountsResult, count] =
await service.listAndCountMoneyAmounts({}, { skip: 1, take: 1 })
expect(count).toEqual(3)
expect(moneyAmountsResult).toEqual([
expect.objectContaining({
id: "money-amount-EUR",
}),
])
})
it("should return requested fields", async () => {
const [moneyAmountsResult, count] =
await service.listAndCountMoneyAmounts(
{},
{
take: 1,
select: ["id"],
}
)
const serialized = JSON.parse(JSON.stringify(moneyAmountsResult))
expect(count).toEqual(3)
expect(serialized).toEqual([
{
id: "money-amount-CAD",
amount: null,
},
])
})
})
describe("retrieveMoneyAmount", () => {
const id = "money-amount-USD"
const amount = 500
it("should return moneyAmount for the given id", async () => {
const moneyAmount = await service.retrieveMoneyAmount(id)
expect(moneyAmount).toEqual(
expect.objectContaining({
id,
expect(moneyAmountsResult).toEqual([
expect.objectContaining({
id: "money-amount-USD",
}),
])
})
)
})
it("should throw an error when moneyAmount with id does not exist", async () => {
let error
it("should list moneyAmounts with relations and selects", async () => {
const moneyAmountsResult = await service.listMoneyAmounts(
{
id: ["money-amount-USD"],
},
{
select: ["id", "min_quantity", "currency_code"],
}
)
try {
await service.retrieveMoneyAmount("does-not-exist")
} catch (e) {
error = e
}
const serialized = JSON.parse(JSON.stringify(moneyAmountsResult))
expect(error.message).toEqual(
"MoneyAmount with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrieveMoneyAmount(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("moneyAmount - id must be defined")
})
it("should return moneyAmount based on config select param", async () => {
const moneyAmount = await service.retrieveMoneyAmount(id, {
select: ["id", "amount"],
expect(serialized).toEqual([
{
id: "money-amount-USD",
amount: null,
min_quantity: "1",
currency_code: "USD",
},
])
})
})
const serialized = JSON.parse(JSON.stringify(moneyAmount))
describe("listAndCountMoneyAmounts", () => {
it("should return moneyAmounts and count", async () => {
const [moneyAmountsResult, count] =
await service.listAndCountMoneyAmounts()
expect(serialized).toEqual({
id,
amount,
})
})
})
describe("deleteMoneyAmounts", () => {
const id = "money-amount-USD"
it("should delete the moneyAmounts given an id successfully", async () => {
await service.deleteMoneyAmounts([id])
const moneyAmounts = await service.listMoneyAmounts({
id: [id],
})
expect(moneyAmounts).toHaveLength(0)
})
})
describe("softDeleteMoneyAmounts", () => {
const id = "money-amount-USD"
it("should softDelete priceSetMoneyAmount and PriceRule when soft-deleting money amount", async () => {
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceRules(testManager)
await createPriceSetMoneyAmountRules(testManager)
await service.softDeleteMoneyAmounts([id])
const [moneyAmount] = await service.listMoneyAmounts(
{
id: [id],
},
{
relations: [
"price_set_money_amount",
"price_set_money_amount.price_rules",
],
withDeleted: true,
}
)
expect(moneyAmount).toBeTruthy()
const deletedAt = moneyAmount.deleted_at
expect(moneyAmount).toEqual(
expect.objectContaining({
deleted_at: deletedAt,
price_set_money_amount: expect.objectContaining({
deleted_at: deletedAt,
price_rules: [
expect(count).toEqual(3)
expect(moneyAmountsResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
deleted_at: deletedAt,
id: "money-amount-USD",
}),
],
}),
})
)
})
})
describe("restoreMoneyAmounts", () => {
const id = "money-amount-USD"
it("should restore softDeleted priceSetMoneyAmount and PriceRule when restoring soft-deleting money amount", async () => {
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceRules(testManager)
await createPriceSetMoneyAmountRules(testManager)
await service.softDeleteMoneyAmounts([id])
await service.restoreMoneyAmounts([id])
const [moneyAmount] = await service.listMoneyAmounts(
{
id: [id],
},
{
relations: [
"price_set_money_amount",
"price_set_money_amount.price_rules",
],
}
)
expect(moneyAmount).toBeTruthy()
const deletedAt = null
expect(moneyAmount).toEqual(
expect.objectContaining({
deleted_at: deletedAt,
price_set_money_amount: expect.objectContaining({
deleted_at: deletedAt,
price_rules: [
expect.objectContaining({
deleted_at: deletedAt,
id: "money-amount-EUR",
}),
],
}),
expect.objectContaining({
id: "money-amount-CAD",
}),
])
)
})
)
})
})
describe("updateMoneyAmounts", () => {
const id = "money-amount-USD"
it("should return moneyAmounts and count when filtered", async () => {
const [moneyAmountsResult, count] =
await service.listAndCountMoneyAmounts({
id: ["money-amount-USD"],
})
it("should update the amount of the moneyAmount successfully", async () => {
await service.updateMoneyAmounts([
{
id,
amount: 700,
},
])
expect(count).toEqual(1)
expect(moneyAmountsResult).toEqual([
expect.objectContaining({
id: "money-amount-USD",
}),
])
})
const moneyAmount = JSON.parse(
JSON.stringify(
await service.retrieveMoneyAmount(id, { select: ["amount"] })
)
)
it("list moneyAmounts with relations and selects", async () => {
const [moneyAmountsResult, count] =
await service.listAndCountMoneyAmounts(
{
id: ["money-amount-USD"],
},
{
select: ["id", "min_quantity", "currency_code", "amount"],
}
)
expect(moneyAmount.amount).toEqual(700)
})
const serialized = JSON.parse(JSON.stringify(moneyAmountsResult))
it("should update the currency of the moneyAmount successfully", async () => {
await service.updateMoneyAmounts([
{
id,
currency_code: "EUR",
},
])
expect(count).toEqual(1)
expect(serialized).toEqual([
{
id: "money-amount-USD",
amount: 500,
min_quantity: "1",
currency_code: "USD",
},
])
})
const moneyAmount = await service.retrieveMoneyAmount(id, {})
it("should return moneyAmounts and count when using skip and take", async () => {
const [moneyAmountsResult, count] =
await service.listAndCountMoneyAmounts({}, { skip: 1, take: 1 })
expect(moneyAmount.currency_code).toEqual("EUR")
})
expect(count).toEqual(3)
expect(moneyAmountsResult).toEqual([
expect.objectContaining({
id: "money-amount-EUR",
}),
])
})
it("should throw an error when a id does not exist", async () => {
let error
it("should return requested fields", async () => {
const [moneyAmountsResult, count] =
await service.listAndCountMoneyAmounts(
{},
{
take: 1,
select: ["id"],
}
)
try {
await service.updateMoneyAmounts([
{
id: "does-not-exist",
amount: 666,
},
])
} catch (e) {
error = e
}
const serialized = JSON.parse(JSON.stringify(moneyAmountsResult))
expect(error.message).toEqual(
'MoneyAmount with id "does-not-exist" not found'
)
})
})
describe("createMoneyAmounts", () => {
it("should create a moneyAmount successfully", async () => {
await service.createMoneyAmounts([
{
id: "money-amount-TESM",
currency_code: "USD",
amount: 333,
min_quantity: 1,
max_quantity: 4,
},
])
const [moneyAmount] = await service.listMoneyAmounts({
id: ["money-amount-TESM"],
expect(count).toEqual(3)
expect(serialized).toEqual([
{
id: "money-amount-CAD",
amount: null,
},
])
})
})
expect(moneyAmount).toEqual(
expect.objectContaining({
id: "money-amount-TESM",
currency_code: "USD",
amount: 333,
min_quantity: "1",
max_quantity: "4",
describe("retrieveMoneyAmount", () => {
const id = "money-amount-USD"
const amount = 500
it("should return moneyAmount for the given id", async () => {
const moneyAmount = await service.retrieveMoneyAmount(id)
expect(moneyAmount).toEqual(
expect.objectContaining({
id,
})
)
})
)
it("should throw an error when moneyAmount with id does not exist", async () => {
let error
try {
await service.retrieveMoneyAmount("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"MoneyAmount with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrieveMoneyAmount(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("moneyAmount - id must be defined")
})
it("should return moneyAmount based on config select param", async () => {
const moneyAmount = await service.retrieveMoneyAmount(id, {
select: ["id", "amount"],
})
const serialized = JSON.parse(JSON.stringify(moneyAmount))
expect(serialized).toEqual({
id,
amount,
})
})
})
describe("deleteMoneyAmounts", () => {
const id = "money-amount-USD"
it("should delete the moneyAmounts given an id successfully", async () => {
await service.deleteMoneyAmounts([id])
const moneyAmounts = await service.listMoneyAmounts({
id: [id],
})
expect(moneyAmounts).toHaveLength(0)
})
})
describe("softDeleteMoneyAmounts", () => {
const id = "money-amount-USD"
it("should softDelete priceSetMoneyAmount and PriceRule when soft-deleting money amount", async () => {
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceRules(testManager)
await createPriceSetMoneyAmountRules(testManager)
await service.softDeleteMoneyAmounts([id])
const [moneyAmount] = await service.listMoneyAmounts(
{
id: [id],
},
{
relations: [
"price_set_money_amount",
"price_set_money_amount.price_rules",
],
withDeleted: true,
}
)
expect(moneyAmount).toBeTruthy()
const deletedAt = moneyAmount.deleted_at
expect(moneyAmount).toEqual(
expect.objectContaining({
deleted_at: deletedAt,
price_set_money_amount: expect.objectContaining({
deleted_at: deletedAt,
price_rules: [
expect.objectContaining({
deleted_at: deletedAt,
}),
],
}),
})
)
})
})
describe("restoreMoneyAmounts", () => {
const id = "money-amount-USD"
it("should restore softDeleted priceSetMoneyAmount and PriceRule when restoring soft-deleting money amount", async () => {
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceRules(testManager)
await createPriceSetMoneyAmountRules(testManager)
await service.softDeleteMoneyAmounts([id])
await service.restoreMoneyAmounts([id])
const [moneyAmount] = await service.listMoneyAmounts(
{
id: [id],
},
{
relations: [
"price_set_money_amount",
"price_set_money_amount.price_rules",
],
}
)
expect(moneyAmount).toBeTruthy()
const deletedAt = null
expect(moneyAmount).toEqual(
expect.objectContaining({
deleted_at: deletedAt,
price_set_money_amount: expect.objectContaining({
deleted_at: deletedAt,
price_rules: [
expect.objectContaining({
deleted_at: deletedAt,
}),
],
}),
})
)
})
})
describe("updateMoneyAmounts", () => {
const id = "money-amount-USD"
it("should update the amount of the moneyAmount successfully", async () => {
await service.updateMoneyAmounts([
{
id,
amount: 700,
},
])
const moneyAmount = JSON.parse(
JSON.stringify(
await service.retrieveMoneyAmount(id, { select: ["amount"] })
)
)
expect(moneyAmount.amount).toEqual(700)
})
it("should update the currency of the moneyAmount successfully", async () => {
await service.updateMoneyAmounts([
{
id,
currency_code: "EUR",
},
])
const moneyAmount = await service.retrieveMoneyAmount(id, {})
expect(moneyAmount.currency_code).toEqual("EUR")
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updateMoneyAmounts([
{
id: "does-not-exist",
amount: 666,
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'MoneyAmount with id "does-not-exist" not found'
)
})
})
describe("createMoneyAmounts", () => {
it("should create a moneyAmount successfully", async () => {
await service.createMoneyAmounts([
{
id: "money-amount-TESM",
currency_code: "USD",
amount: 333,
min_quantity: 1,
max_quantity: 4,
},
])
const [moneyAmount] = await service.listMoneyAmounts({
id: ["money-amount-TESM"],
})
expect(moneyAmount).toEqual(
expect.objectContaining({
id: "money-amount-TESM",
currency_code: "USD",
amount: 333,
min_quantity: "1",
max_quantity: "4",
})
)
})
})
})
})
},
})
@@ -1,358 +1,338 @@
import { MikroOrmWrapper } from "../../../utils"
import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createPriceLists } from "../../../__fixtures__/price-list"
import { createPriceListRules } from "../../../__fixtures__/price-list-rules"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { Modules } from "@medusajs/modules-sdk"
import { initModules } from "medusa-test-utils"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
jest.setTimeout(30000)
describe("PriceListRule Service", () => {
let service: IPricingModuleService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
let shutdownFunc: () => Promise<void>
beforeAll(async () => {
const initModulesConfig = getInitModuleConfig()
const { medusaApp, shutdown } = await initModules(initModulesConfig)
service = medusaApp.modules[Modules.PRICING]
shutdownFunc = shutdown
})
afterAll(async () => {
await shutdownFunc()
})
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
testManager = await MikroOrmWrapper.forkManager()
await createRuleTypes(testManager)
await createPriceLists(testManager)
await createPriceListRules(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("list", () => {
it("should list priceListRules", async () => {
const priceListRuleResult = await service.listPriceListRules()
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
it("should list priceListRules by pricelist id", async () => {
const priceListRuleResult = await service.listPriceListRules({
id: ["price-list-rule-1"],
moduleIntegrationTestRunner({
moduleName: Modules.PRICING,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPricingModuleService>) => {
describe("PriceListRule Service", () => {
let testManager: SqlEntityManager
beforeEach(async () => {
testManager = await MikroOrmWrapper.forkManager()
await createRuleTypes(testManager)
await createPriceLists(testManager)
await createPriceListRules(testManager)
})
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
])
})
})
describe("list", () => {
it("should list priceListRules", async () => {
const priceListRuleResult = await service.listPriceListRules()
describe("listAndCount", () => {
it("should return pricelistrules and count", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules()
expect(count).toEqual(2)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
it("should return pricelistrules and count when filtered", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules({
id: ["price-list-rule-1"],
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
expect(count).toEqual(1)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
])
})
it("should list priceListRules by pricelist id", async () => {
const priceListRuleResult = await service.listPriceListRules({
id: ["price-list-rule-1"],
})
it("should return pricelistrules and count when using skip and take", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules({}, { skip: 1, take: 1 })
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
])
})
})
expect(count).toEqual(2)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
describe("listAndCount", () => {
it("should return pricelistrules and count", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules()
it("should return requested fields", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules(
{},
{
take: 1,
select: ["id"],
expect(count).toEqual(2)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
it("should return pricelistrules and count when filtered", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules({
id: ["price-list-rule-1"],
})
expect(count).toEqual(1)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
])
})
it("should return pricelistrules and count when using skip and take", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules({}, { skip: 1, take: 1 })
expect(count).toEqual(2)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
it("should return requested fields", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules(
{},
{
take: 1,
select: ["id"],
}
)
const serialized = JSON.parse(JSON.stringify(priceListRuleResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "price-list-rule-1",
},
])
})
})
describe("retrieve", () => {
const id = "price-list-rule-1"
it("should return priceList for the given id", async () => {
const priceListRuleResult = await service.retrievePriceListRule(id)
expect(priceListRuleResult).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when priceListRule with id does not exist", async () => {
let error
try {
await service.retrievePriceListRule("does-not-exist")
} catch (e) {
error = e
}
)
const serialized = JSON.parse(JSON.stringify(priceListRuleResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "price-list-rule-1",
},
])
})
})
describe("retrieve", () => {
const id = "price-list-rule-1"
it("should return priceList for the given id", async () => {
const priceListRuleResult = await service.retrievePriceListRule(id)
expect(priceListRuleResult).toEqual(
expect.objectContaining({
id,
expect(error.message).toEqual(
"PriceListRule with id: does-not-exist was not found"
)
})
)
})
it("should throw an error when priceListRule with id does not exist", async () => {
let error
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrievePriceListRule("does-not-exist")
} catch (e) {
error = e
}
try {
await service.retrievePriceListRule(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceListRule with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrievePriceListRule(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("priceListRule - id must be defined")
})
})
describe("delete", () => {
const id = "price-list-rule-1"
it("should delete the pricelists given an id successfully", async () => {
await service.deletePriceListRules([id])
const priceListResult = await service.listPriceListRules({
id: [id],
expect(error.message).toEqual("priceListRule - id must be defined")
})
})
expect(priceListResult).toHaveLength(0)
})
})
describe("delete", () => {
const id = "price-list-rule-1"
describe("update", () => {
const id = "price-list-rule-2"
it("should delete the pricelists given an id successfully", async () => {
await service.deletePriceListRules([id])
it("should update the value of the priceListRule successfully", async () => {
await service.updatePriceListRules([
{
id,
price_list_id: "price-list-2",
rule_type_id: "rule-type-2",
},
])
const priceListResult = await service.listPriceListRules({
id: [id],
})
const priceList = await service.retrievePriceListRule(id, {
relations: ["price_list", "rule_type"],
expect(priceListResult).toHaveLength(0)
})
})
expect(priceList.price_list.id).toEqual("price-list-2")
expect(priceList.rule_type.id).toEqual("rule-type-2")
})
describe("update", () => {
const id = "price-list-rule-2"
it("should throw an error when a id does not exist", async () => {
let error
it("should update the value of the priceListRule successfully", async () => {
await service.updatePriceListRules([
{
id,
price_list_id: "price-list-2",
rule_type_id: "rule-type-2",
},
])
try {
await service.updatePriceListRules([
{
id: "does-not-exist",
},
])
} catch (e) {
error = e
}
const priceList = await service.retrievePriceListRule(id, {
relations: ["price_list", "rule_type"],
})
expect(error.message).toEqual(
'PriceListRule with id "does-not-exist" not found'
)
})
})
expect(priceList.price_list.id).toEqual("price-list-2")
expect(priceList.rule_type.id).toEqual("rule-type-2")
})
describe("create", () => {
it("should create a priceListRule successfully", async () => {
const [created] = await service.createPriceListRules([
{
price_list_id: "price-list-2",
rule_type_id: "rule-type-2",
},
])
it("should throw an error when a id does not exist", async () => {
let error
const [priceListRule] = await service.listPriceListRules(
{
id: [created.id],
},
{
relations: ["price_list", "rule_type"],
}
)
try {
await service.updatePriceListRules([
{
id: "does-not-exist",
},
])
} catch (e) {
error = e
}
expect(priceListRule.price_list.id).toEqual("price-list-2")
expect(priceListRule.rule_type.id).toEqual("rule-type-2")
})
})
describe("setPriceListRules", () => {
it("should add a priceListRule to a priceList", async () => {
await createRuleTypes(testManager, [
{
id: "rule-type-3",
name: "test",
rule_attribute: "sales_channel",
},
])
await service.setPriceListRules({
priceListId: "price-list-1",
rules: {
sales_channel: "sc-1",
},
expect(error.message).toEqual(
'PriceListRule with id "does-not-exist" not found'
)
})
})
const [priceList] = await service.listPriceLists(
{
id: ["price-list-1"],
},
{
relations: [
"price_list_rules",
"price_list_rules.price_list_rule_values",
],
}
)
describe("create", () => {
it("should create a priceListRule successfully", async () => {
const [created] = await service.createPriceListRules([
{
price_list_id: "price-list-2",
rule_type_id: "rule-type-2",
},
])
expect(priceList.price_list_rules).toEqual(
expect.arrayContaining([
expect.objectContaining({
rule_type: { id: "rule-type-3" },
price_list_rule_values: [
expect.objectContaining({ value: "sc-1" }),
],
}),
])
)
})
const [priceListRule] = await service.listPriceListRules(
{
id: [created.id],
},
{
relations: ["price_list", "rule_type"],
}
)
it("should multiple priceListRules to a priceList", async () => {
await createRuleTypes(testManager, [
{
id: "rule-type-3",
name: "test",
rule_attribute: "sales_channel",
},
])
await service.setPriceListRules({
priceListId: "price-list-1",
rules: {
sales_channel: ["sc-1", "sc-2"],
},
expect(priceListRule.price_list.id).toEqual("price-list-2")
expect(priceListRule.rule_type.id).toEqual("rule-type-2")
})
})
const [priceList] = await service.listPriceLists(
{
id: ["price-list-1"],
},
{
relations: [
"price_list_rules",
"price_list_rules.price_list_rule_values",
],
}
)
describe("setPriceListRules", () => {
it("should add a priceListRule to a priceList", async () => {
await createRuleTypes(testManager, [
{
id: "rule-type-3",
name: "test",
rule_attribute: "sales_channel",
},
])
expect(priceList.price_list_rules).toEqual(
expect.arrayContaining([
expect.objectContaining({
rule_type: { id: "rule-type-3" },
price_list_rule_values: expect.arrayContaining([
expect.objectContaining({ value: "sc-1" }),
expect.objectContaining({ value: "sc-2" }),
]),
}),
])
)
})
})
await service.setPriceListRules({
priceListId: "price-list-1",
rules: {
sales_channel: "sc-1",
},
})
describe("removePriceListRules", () => {
it("should remove a priceListRule from a priceList", async () => {
await service.removePriceListRules({
priceListId: "price-list-1",
rules: ["currency_code"],
const [priceList] = await service.listPriceLists(
{
id: ["price-list-1"],
},
{
relations: [
"price_list_rules",
"price_list_rules.price_list_rule_values",
],
}
)
expect(priceList.price_list_rules).toEqual(
expect.arrayContaining([
expect.objectContaining({
rule_type: { id: "rule-type-3" },
price_list_rule_values: [
expect.objectContaining({ value: "sc-1" }),
],
}),
])
)
})
it("should multiple priceListRules to a priceList", async () => {
await createRuleTypes(testManager, [
{
id: "rule-type-3",
name: "test",
rule_attribute: "sales_channel",
},
])
await service.setPriceListRules({
priceListId: "price-list-1",
rules: {
sales_channel: ["sc-1", "sc-2"],
},
})
const [priceList] = await service.listPriceLists(
{
id: ["price-list-1"],
},
{
relations: [
"price_list_rules",
"price_list_rules.price_list_rule_values",
],
}
)
expect(priceList.price_list_rules).toEqual(
expect.arrayContaining([
expect.objectContaining({
rule_type: { id: "rule-type-3" },
price_list_rule_values: expect.arrayContaining([
expect.objectContaining({ value: "sc-1" }),
expect.objectContaining({ value: "sc-2" }),
]),
}),
])
)
})
})
const [priceList] = await service.listPriceLists(
{
id: ["price-list-1"],
},
{
relations: ["price_list_rules"],
}
)
describe("removePriceListRules", () => {
it("should remove a priceListRule from a priceList", async () => {
await service.removePriceListRules({
priceListId: "price-list-1",
rules: ["currency_code"],
})
expect(priceList.price_list_rules).toEqual([
expect.objectContaining({ rule_type: { id: "rule-type-2" } }),
])
const [priceList] = await service.listPriceLists(
{
id: ["price-list-1"],
},
{
relations: ["price_list_rules"],
}
)
expect(priceList.price_list_rules).toEqual([
expect.objectContaining({ rule_type: { id: "rule-type-2" } }),
])
})
})
})
})
},
})
@@ -8,330 +8,316 @@ import { createPriceSets } from "../../../__fixtures__/price-set"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { MikroOrmWrapper } from "../../../utils"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
import { Modules } from "@medusajs/modules-sdk"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
jest.setTimeout(30000)
describe("PricingModule Service - PriceRule", () => {
let service: IPricingModuleService
let testManager: SqlEntityManager
let shutdownFunc: () => Promise<void>
moduleIntegrationTestRunner({
moduleName: Modules.PRICING,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPricingModuleService>) => {
describe("PricingModule Service - PriceRule", () => {
let testManager: SqlEntityManager
beforeEach(async () => {
testManager = await MikroOrmWrapper.forkManager()
beforeAll(async () => {
const initModulesConfig = getInitModuleConfig()
const { medusaApp, shutdown } = await initModules(initModulesConfig)
service = medusaApp.modules[Modules.PRICING]
shutdownFunc = shutdown
})
afterAll(async () => {
await shutdownFunc()
})
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
testManager = MikroOrmWrapper.forkManager()
await createMoneyAmounts(testManager)
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceSetMoneyAmountRules(testManager)
await createPriceRules(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("list", () => {
it("should list priceRules", async () => {
const PriceRulesResult = await service.listPriceRules()
const serialized = JSON.parse(JSON.stringify(PriceRulesResult))
expect(serialized).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
expect.objectContaining({
id: "price-rule-2",
}),
])
})
it("should list priceRules by id", async () => {
const priceRuleResult = await service.listPriceRules({
id: ["price-rule-1"],
await createMoneyAmounts(testManager)
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceSetMoneyAmountRules(testManager)
await createPriceRules(testManager)
})
expect(priceRuleResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
])
})
describe("list", () => {
it("should list priceRules", async () => {
const PriceRulesResult = await service.listPriceRules()
const serialized = JSON.parse(JSON.stringify(PriceRulesResult))
it("should list priceRules with relations and selects", async () => {
const priceRulesResult = await service.listPriceRules(
{
id: ["price-rule-1"],
},
{
select: ["id", "price_set.id"],
relations: ["price_set"],
}
)
const serialized = JSON.parse(JSON.stringify(priceRulesResult))
expect(serialized).toEqual([
{
id: "price-rule-1",
price_set: {
id: "price-set-1",
},
},
])
})
describe("listAndCount", () => {
it("should return priceRules and count", async () => {
const [priceRulesResult, count] = await service.listAndCountPriceRules()
expect(count).toEqual(2)
expect(priceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
expect.objectContaining({
id: "price-rule-2",
}),
])
})
it("should return priceRules and count when filtered", async () => {
const [priceRulesResult, count] = await service.listAndCountPriceRules({
id: ["price-rule-1"],
expect(serialized).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
expect.objectContaining({
id: "price-rule-2",
}),
])
})
expect(count).toEqual(1)
expect(priceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
])
})
it("should list PriceRules with relations and selects", async () => {
const [PriceRulesResult, count] = await service.listAndCountPriceRules(
{
it("should list priceRules by id", async () => {
const priceRuleResult = await service.listPriceRules({
id: ["price-rule-1"],
},
{
select: ["id", "price_set.id"],
relations: ["price_set"],
}
)
const serialized = JSON.parse(JSON.stringify(PriceRulesResult))
expect(count).toEqual(1)
expect(serialized).toEqual([
{
id: "price-rule-1",
price_set: {
id: "price-set-1",
},
},
])
})
it("should return PriceRules and count when using skip and take", async () => {
const [PriceRulesResult, count] = await service.listAndCountPriceRules(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(2)
expect(PriceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-2",
}),
])
})
it("should return requested fields", async () => {
const [PriceRulesResult, count] = await service.listAndCountPriceRules(
{},
{
take: 1,
select: ["id"],
}
)
const serialized = JSON.parse(JSON.stringify(PriceRulesResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "price-rule-1",
},
])
})
})
describe("retrieve", () => {
const id = "price-rule-1"
it("should return PriceRule for the given id", async () => {
const PriceRule = await service.retrievePriceRule(id)
expect(PriceRule).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when PriceRule with id does not exist", async () => {
let error
try {
await service.retrievePriceRule("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceRule with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrievePriceRule(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("priceRule - id must be defined")
})
it("should return PriceRule based on config select param", async () => {
const PriceRule = await service.retrievePriceRule(id, {
select: ["id"],
expect(priceRuleResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
])
})
const serialized = JSON.parse(JSON.stringify(PriceRule))
expect(serialized).toEqual({
id,
})
})
})
describe("delete", () => {
const id = "price-set-1"
it("should delete the PriceRules given an id successfully", async () => {
await service.deletePriceRules([id])
const PriceRules = await service.listPriceRules({
id: [id],
})
expect(PriceRules).toHaveLength(0)
})
})
describe("update", () => {
const id = "price-set-1"
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updatePriceRules([
it("should list priceRules with relations and selects", async () => {
const priceRulesResult = await service.listPriceRules(
{
id: "does-not-exist",
id: ["price-rule-1"],
},
{
select: ["id", "price_set.id"],
relations: ["price_set"],
}
)
const serialized = JSON.parse(JSON.stringify(priceRulesResult))
expect(serialized).toEqual([
{
id: "price-rule-1",
price_set: {
id: "price-set-1",
},
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceRule with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updatePriceRules([
{
random: "does-not-exist",
} as any,
])
} catch (e) {
error = e
}
expect(error.message).toEqual('PriceRule with id "" not found')
})
it("should create a PriceRule successfully", async () => {
const [ma] = await createMoneyAmounts(testManager, [
{
amount: 100,
currency_code: "EUR",
},
])
const psma: PriceSetMoneyAmount = testManager.create(
PriceSetMoneyAmount,
{
price_set: "price-set-1",
money_amount: ma.id,
title: "test",
rules_count: 0,
}
)
await testManager.persist(psma).flush()
await service.createPriceRules([
{
id: "price-rule-new",
price_set_id: "price-set-1",
rule_type_id: "rule-type-1",
value: "region_1",
price_list_id: "test",
price_set_money_amount_id: psma.id,
} as unknown as CreatePriceRuleDTO,
])
const [pricerule] = await service.listPriceRules({
id: ["price-rule-new"],
})
expect(pricerule).toEqual(
expect.objectContaining({
id: "price-rule-new",
} as unknown as CreatePriceRuleDTO)
)
describe("listAndCount", () => {
it("should return priceRules and count", async () => {
const [priceRulesResult, count] =
await service.listAndCountPriceRules()
expect(count).toEqual(2)
expect(priceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
expect.objectContaining({
id: "price-rule-2",
}),
])
})
it("should return priceRules and count when filtered", async () => {
const [priceRulesResult, count] =
await service.listAndCountPriceRules({
id: ["price-rule-1"],
})
expect(count).toEqual(1)
expect(priceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
])
})
it("should list PriceRules with relations and selects", async () => {
const [PriceRulesResult, count] =
await service.listAndCountPriceRules(
{
id: ["price-rule-1"],
},
{
select: ["id", "price_set.id"],
relations: ["price_set"],
}
)
const serialized = JSON.parse(JSON.stringify(PriceRulesResult))
expect(count).toEqual(1)
expect(serialized).toEqual([
{
id: "price-rule-1",
price_set: {
id: "price-set-1",
},
},
])
})
it("should return PriceRules and count when using skip and take", async () => {
const [PriceRulesResult, count] =
await service.listAndCountPriceRules({}, { skip: 1, take: 1 })
expect(count).toEqual(2)
expect(PriceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-2",
}),
])
})
it("should return requested fields", async () => {
const [PriceRulesResult, count] =
await service.listAndCountPriceRules(
{},
{
take: 1,
select: ["id"],
}
)
const serialized = JSON.parse(JSON.stringify(PriceRulesResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "price-rule-1",
},
])
})
})
describe("retrieve", () => {
const id = "price-rule-1"
it("should return PriceRule for the given id", async () => {
const PriceRule = await service.retrievePriceRule(id)
expect(PriceRule).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when PriceRule with id does not exist", async () => {
let error
try {
await service.retrievePriceRule("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceRule with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrievePriceRule(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("priceRule - id must be defined")
})
it("should return PriceRule based on config select param", async () => {
const PriceRule = await service.retrievePriceRule(id, {
select: ["id"],
})
const serialized = JSON.parse(JSON.stringify(PriceRule))
expect(serialized).toEqual({
id,
})
})
})
describe("delete", () => {
const id = "price-set-1"
it("should delete the PriceRules given an id successfully", async () => {
await service.deletePriceRules([id])
const PriceRules = await service.listPriceRules({
id: [id],
})
expect(PriceRules).toHaveLength(0)
})
})
describe("update", () => {
const id = "price-set-1"
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updatePriceRules([
{
id: "does-not-exist",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceRule with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updatePriceRules([
{
random: "does-not-exist",
} as any,
])
} catch (e) {
error = e
}
expect(error.message).toEqual('PriceRule with id "" not found')
})
it("should create a PriceRule successfully", async () => {
const [ma] = await createMoneyAmounts(testManager, [
{
amount: 100,
currency_code: "EUR",
},
])
const psma: PriceSetMoneyAmount = testManager.create(
PriceSetMoneyAmount,
{
price_set: "price-set-1",
money_amount: ma.id,
title: "test",
rules_count: 0,
}
)
await testManager.persist(psma).flush()
await service.createPriceRules([
{
id: "price-rule-new",
price_set_id: "price-set-1",
rule_type_id: "rule-type-1",
value: "region_1",
price_list_id: "test",
price_set_money_amount_id: psma.id,
} as unknown as CreatePriceRuleDTO,
])
const [pricerule] = await service.listPriceRules({
id: ["price-rule-new"],
})
expect(pricerule).toEqual(
expect.objectContaining({
id: "price-rule-new",
} as unknown as CreatePriceRuleDTO)
)
})
})
})
})
})
},
})
@@ -5,293 +5,274 @@ import { createPriceSets } from "../../../__fixtures__/price-set"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { MikroOrmWrapper } from "../../../utils"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
import { Modules } from "@medusajs/modules-sdk"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
jest.setTimeout(30000)
describe("PricingModule Service - PriceSetMoneyAmountRules", () => {
let service: IPricingModuleService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
let shutdownFunc: () => Promise<void>
moduleIntegrationTestRunner({
moduleName: Modules.PRICING,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPricingModuleService>) => {
describe("PricingModule Service - PriceSetMoneyAmountRules", () => {
beforeEach(async () => {
const testManager = await MikroOrmWrapper.forkManager()
beforeAll(async () => {
const initModulesConfig = getInitModuleConfig()
await createMoneyAmounts(testManager)
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceSetMoneyAmountRules(testManager)
})
const { medusaApp, shutdown } = await initModules(initModulesConfig)
describe("listPriceSetMoneyAmountRules", () => {
it("should list psmar records", async () => {
const priceSetMoneyAmountRulesResult =
await service.listPriceSetMoneyAmountRules()
service = medusaApp.modules[Modules.PRICING]
shutdownFunc = shutdown
})
afterAll(async () => {
await shutdownFunc()
})
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
testManager = await MikroOrmWrapper.forkManager()
await createMoneyAmounts(testManager)
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceSetMoneyAmountRules(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("listPriceSetMoneyAmountRules", () => {
it("should list psmar records", async () => {
const priceSetMoneyAmountRulesResult =
await service.listPriceSetMoneyAmountRules()
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
expect.objectContaining({
id: "psmar-2",
}),
expect.objectContaining({
id: "psmar-3",
}),
])
})
it("should list psmar record by id", async () => {
const priceSetMoneyAmountRulesResult =
await service.listPriceSetMoneyAmountRules({
id: ["psmar-1"],
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
expect.objectContaining({
id: "psmar-2",
}),
expect.objectContaining({
id: "psmar-3",
}),
])
})
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
])
})
})
it("should list psmar record by id", async () => {
const priceSetMoneyAmountRulesResult =
await service.listPriceSetMoneyAmountRules({
id: ["psmar-1"],
})
describe("listAndCount", () => {
it("should return psmar records and count", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules()
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
])
})
})
expect(count).toEqual(3)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
expect.objectContaining({
id: "psmar-2",
}),
expect.objectContaining({
id: "psmar-3",
}),
])
})
describe("listAndCount", () => {
it("should return psmar records and count", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules()
it("should return psmar records and count when filtered", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules({
id: ["psmar-1"],
expect(count).toEqual(3)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
expect.objectContaining({
id: "psmar-2",
}),
expect.objectContaining({
id: "psmar-3",
}),
])
})
expect(count).toEqual(1)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
])
})
it("should return psmar records and count when filtered", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules({
id: ["psmar-1"],
})
it("should return psmar and count when using skip and take", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(1)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
])
})
expect(count).toEqual(3)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-2",
}),
])
})
it("should return psmar and count when using skip and take", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules(
{},
{ skip: 1, take: 1 }
)
it("should return requested fields", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules(
{},
{
take: 1,
select: ["value"],
expect(count).toEqual(3)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-2",
}),
])
})
it("should return requested fields", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules(
{},
{
take: 1,
select: ["value"],
}
)
const serialized = JSON.parse(
JSON.stringify(priceSetMoneyAmountRulesResult)
)
expect(count).toEqual(3)
expect(serialized).toEqual([
{
id: "psmar-1",
value: "EUR",
},
])
})
})
describe("retrievePriceSetMoneyAmountRules", () => {
it("should return priceSetMoneyAmountRules for the given id", async () => {
const priceSetMoneyAmountRules =
await service.retrievePriceSetMoneyAmountRules("psmar-1")
expect(priceSetMoneyAmountRules).toEqual(
expect.objectContaining({
id: "psmar-1",
})
)
})
it("should throw an error when priceSetMoneyAmountRules with id does not exist", async () => {
let error
try {
await service.retrievePriceSetMoneyAmountRules("does-not-exist")
} catch (e) {
error = e
}
)
const serialized = JSON.parse(
JSON.stringify(priceSetMoneyAmountRulesResult)
)
expect(count).toEqual(3)
expect(serialized).toEqual([
{
id: "psmar-1",
value: "EUR",
},
])
})
})
describe("retrievePriceSetMoneyAmountRules", () => {
it("should return priceSetMoneyAmountRules for the given id", async () => {
const priceSetMoneyAmountRules =
await service.retrievePriceSetMoneyAmountRules("psmar-1")
expect(priceSetMoneyAmountRules).toEqual(
expect.objectContaining({
id: "psmar-1",
})
)
})
it("should throw an error when priceSetMoneyAmountRules with id does not exist", async () => {
let error
try {
await service.retrievePriceSetMoneyAmountRules("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceSetMoneyAmountRules with id: does-not-exist was not found"
)
})
it("should throw an error when an id is not provided", async () => {
let error
try {
await service.retrievePriceSetMoneyAmountRules(
undefined as unknown as string
)
} catch (e) {
error = e
}
expect(error.message).toEqual(
"priceSetMoneyAmountRules - id must be defined"
)
})
it("should return priceSetMoneyAmountRules based on config select param", async () => {
const priceSetMoneyAmountRulesResult =
await service.retrievePriceSetMoneyAmountRules("psmar-1", {
select: ["value"],
expect(error.message).toEqual(
"PriceSetMoneyAmountRules with id: does-not-exist was not found"
)
})
const serialized = JSON.parse(
JSON.stringify(priceSetMoneyAmountRulesResult)
)
it("should throw an error when an id is not provided", async () => {
let error
expect(serialized).toEqual({
value: "EUR",
id: "psmar-1",
})
})
})
try {
await service.retrievePriceSetMoneyAmountRules(
undefined as unknown as string
)
} catch (e) {
error = e
}
describe("deletePriceSetMoneyAmountRules", () => {
const id = "psmar-1"
expect(error.message).toEqual(
"priceSetMoneyAmountRules - id must be defined"
)
})
it("should delete the priceSetMoneyAmountRuless given an id successfully", async () => {
await service.deletePriceSetMoneyAmountRules([id])
it("should return priceSetMoneyAmountRules based on config select param", async () => {
const priceSetMoneyAmountRulesResult =
await service.retrievePriceSetMoneyAmountRules("psmar-1", {
select: ["value"],
})
const currencies = await service.listPriceSetMoneyAmountRules({
id: [id],
const serialized = JSON.parse(
JSON.stringify(priceSetMoneyAmountRulesResult)
)
expect(serialized).toEqual({
value: "EUR",
id: "psmar-1",
})
})
})
expect(currencies).toHaveLength(0)
})
})
describe("deletePriceSetMoneyAmountRules", () => {
const id = "psmar-1"
describe("updatePriceSetMoneyAmountRules", () => {
const id = "psmar-1"
it("should delete the priceSetMoneyAmountRuless given an id successfully", async () => {
await service.deletePriceSetMoneyAmountRules([id])
it("should update the value of the priceSetMoneyAmountRules successfully", async () => {
await service.updatePriceSetMoneyAmountRules([
{
id,
value: "New value",
},
])
const currencies = await service.listPriceSetMoneyAmountRules({
id: [id],
})
const psmar = await service.retrievePriceSetMoneyAmountRules(id)
expect(psmar.value).toEqual("New value")
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updatePriceSetMoneyAmountRules([
{
id: "does-not-exist",
value: "random value",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceSetMoneyAmountRules with id "does-not-exist" not found'
)
})
})
describe("createPriceSetMoneyAmountRules", () => {
it("should create a priceSetMoneyAmountRules successfully", async () => {
await service.createPriceSetMoneyAmountRules([
{
price_set_money_amount: "price-set-money-amount-EUR",
rule_type: "rule-type-2",
value: "New priceSetMoneyAmountRule",
},
])
const [created] = await service.listPriceSetMoneyAmountRules(
{
value: ["New priceSetMoneyAmountRule"],
},
{
relations: ["price_set_money_amount", "rule_type"],
}
)
expect(created).toEqual(
expect.objectContaining({
id: expect.any(String),
value: "New priceSetMoneyAmountRule",
price_set_money_amount: expect.objectContaining({
id: "price-set-money-amount-EUR",
}),
rule_type: expect.objectContaining({
id: "rule-type-2",
}),
expect(currencies).toHaveLength(0)
})
)
})
describe("updatePriceSetMoneyAmountRules", () => {
const id = "psmar-1"
it("should update the value of the priceSetMoneyAmountRules successfully", async () => {
await service.updatePriceSetMoneyAmountRules([
{
id,
value: "New value",
},
])
const psmar = await service.retrievePriceSetMoneyAmountRules(id)
expect(psmar.value).toEqual("New value")
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updatePriceSetMoneyAmountRules([
{
id: "does-not-exist",
value: "random value",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceSetMoneyAmountRules with id "does-not-exist" not found'
)
})
})
describe("createPriceSetMoneyAmountRules", () => {
it("should create a priceSetMoneyAmountRules successfully", async () => {
await service.createPriceSetMoneyAmountRules([
{
price_set_money_amount: "price-set-money-amount-EUR",
rule_type: "rule-type-2",
value: "New priceSetMoneyAmountRule",
},
])
const [created] = await service.listPriceSetMoneyAmountRules(
{
value: ["New priceSetMoneyAmountRule"],
},
{
relations: ["price_set_money_amount", "rule_type"],
}
)
expect(created).toEqual(
expect.objectContaining({
id: expect.any(String),
value: "New priceSetMoneyAmountRule",
price_set_money_amount: expect.objectContaining({
id: "price-set-money-amount-EUR",
}),
rule_type: expect.objectContaining({
id: "rule-type-2",
}),
})
)
})
})
})
})
},
})
File diff suppressed because it is too large Load Diff
@@ -1,261 +1,241 @@
import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { MikroOrmWrapper } from "../../../utils"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { Modules } from "@medusajs/modules-sdk"
import { IPricingModuleService } from "@medusajs/types"
describe("PricingModuleService ruleType", () => {
let service: IPricingModuleService
let testManager: SqlEntityManager
let shutdownFunc: () => Promise<void>
beforeAll(async () => {
const initModulesConfig = getInitModuleConfig()
const { medusaApp, shutdown } = await initModules(initModulesConfig)
service = medusaApp.modules[Modules.PRICING]
shutdownFunc = shutdown
})
afterAll(async () => {
await shutdownFunc()
})
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
MikroOrmWrapper.forkManager()
testManager = MikroOrmWrapper.forkManager()
await createRuleTypes(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("listRuleTypes", () => {
it("should list rule types", async () => {
const ruleTypeResult = await service.listRuleTypes()
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
expect.objectContaining({
id: "rule-type-2",
name: "rule 2",
}),
])
})
it("should list rule types by id", async () => {
const ruleTypeResult = await service.listRuleTypes({
id: ["rule-type-1"],
moduleIntegrationTestRunner({
moduleName: Modules.PRICING,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPricingModuleService>) => {
describe("PricingModuleService ruleType", () => {
beforeEach(async () => {
const testManager = MikroOrmWrapper.forkManager()
await createRuleTypes(testManager)
})
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
])
})
})
describe("listRuleTypes", () => {
it("should list rule types", async () => {
const ruleTypeResult = await service.listRuleTypes()
describe("listAndCountRuleTypes", () => {
it("should return rule types and count", async () => {
const [ruleTypeResult, count] = await service.listAndCountRuleTypes()
expect(count).toEqual(2)
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
expect.objectContaining({
id: "rule-type-2",
name: "rule 2",
}),
])
})
it("should return rule types and count when filtered", async () => {
const [ruleTypeResult, count] = await service.listAndCountRuleTypes({
id: ["rule-type-1"],
})
expect(count).toEqual(1)
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
])
})
it("should return rule types and count when using skip and take", async () => {
const [ruleTypeResult, count] = await service.listAndCountRuleTypes(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(2)
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-2",
name: "rule 2",
}),
])
})
it("should return requested fields", async () => {
const [ruleTypeResult, count] = await service.listAndCountRuleTypes(
{},
{
take: 1,
select: ["name"],
}
)
const serialized = JSON.parse(JSON.stringify(ruleTypeResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "rule-type-1",
name: "rule 1",
},
])
})
})
describe("retrieveRuleType", () => {
it("should return ruleType for the given id", async () => {
const ruleType = await service.retrieveRuleType("rule-type-1")
expect(ruleType).toEqual(
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
expect.objectContaining({
id: "rule-type-2",
name: "rule 2",
}),
])
})
)
})
it("should throw an error when ruleType with id does not exist", async () => {
let error
it("should list rule types by id", async () => {
const ruleTypeResult = await service.listRuleTypes({
id: ["rule-type-1"],
})
try {
await service.retrieveRuleType("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"RuleType with id: does-not-exist was not found"
)
})
it("should throw an error when an id is not provided", async () => {
let error
try {
await service.retrieveRuleType(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("ruleType - id must be defined")
})
it("should return ruleType based on config select param", async () => {
const ruleTypeResult = await service.retrieveRuleType("rule-type-1", {
select: ["name"],
})
const serialized = JSON.parse(JSON.stringify(ruleTypeResult))
expect(serialized).toEqual({
name: "rule 1",
id: "rule-type-1",
})
})
})
describe("deleteRuleTypes", () => {
const id = "rule-type-1"
it("should delete the ruleTypes given an id successfully", async () => {
await service.deleteRuleTypes([id])
const currencies = await service.listRuleTypes({
id: [id],
})
expect(currencies).toHaveLength(0)
})
})
describe("updateRuleTypes", () => {
const id = "rule-type-1"
it("should update the name of the ruleType successfully", async () => {
await service.updateRuleTypes([
{
id,
name: "rule 3",
},
])
const ruletype = await service.retrieveRuleType(id)
expect(ruletype.name).toEqual("rule 3")
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updateRuleTypes([
{
id: "does-not-exist",
name: "rule 3",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'RuleType with id "does-not-exist" not found'
)
})
})
describe("createRuleTypes", () => {
it("should create a ruleType successfully", async () => {
await service.createRuleTypes([
{
name: "Test Rule",
rule_attribute: "region_id",
},
])
const [ruleType] = await service.listRuleTypes({
name: ["Test Rule"],
})
expect(ruleType).toEqual(
expect.objectContaining({
name: "Test Rule",
rule_attribute: "region_id",
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
])
})
)
})
describe("listAndCountRuleTypes", () => {
it("should return rule types and count", async () => {
const [ruleTypeResult, count] = await service.listAndCountRuleTypes()
expect(count).toEqual(2)
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
expect.objectContaining({
id: "rule-type-2",
name: "rule 2",
}),
])
})
it("should return rule types and count when filtered", async () => {
const [ruleTypeResult, count] = await service.listAndCountRuleTypes({
id: ["rule-type-1"],
})
expect(count).toEqual(1)
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
])
})
it("should return rule types and count when using skip and take", async () => {
const [ruleTypeResult, count] = await service.listAndCountRuleTypes(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(2)
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-2",
name: "rule 2",
}),
])
})
it("should return requested fields", async () => {
const [ruleTypeResult, count] = await service.listAndCountRuleTypes(
{},
{
take: 1,
select: ["name"],
}
)
const serialized = JSON.parse(JSON.stringify(ruleTypeResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "rule-type-1",
name: "rule 1",
},
])
})
})
describe("retrieveRuleType", () => {
it("should return ruleType for the given id", async () => {
const ruleType = await service.retrieveRuleType("rule-type-1")
expect(ruleType).toEqual(
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
})
)
})
it("should throw an error when ruleType with id does not exist", async () => {
let error
try {
await service.retrieveRuleType("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"RuleType with id: does-not-exist was not found"
)
})
it("should throw an error when an id is not provided", async () => {
let error
try {
await service.retrieveRuleType(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("ruleType - id must be defined")
})
it("should return ruleType based on config select param", async () => {
const ruleTypeResult = await service.retrieveRuleType("rule-type-1", {
select: ["name"],
})
const serialized = JSON.parse(JSON.stringify(ruleTypeResult))
expect(serialized).toEqual({
name: "rule 1",
id: "rule-type-1",
})
})
})
describe("deleteRuleTypes", () => {
const id = "rule-type-1"
it("should delete the ruleTypes given an id successfully", async () => {
await service.deleteRuleTypes([id])
const currencies = await service.listRuleTypes({
id: [id],
})
expect(currencies).toHaveLength(0)
})
})
describe("updateRuleTypes", () => {
const id = "rule-type-1"
it("should update the name of the ruleType successfully", async () => {
await service.updateRuleTypes([
{
id,
name: "rule 3",
},
])
const ruletype = await service.retrieveRuleType(id)
expect(ruletype.name).toEqual("rule 3")
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updateRuleTypes([
{
id: "does-not-exist",
name: "rule 3",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'RuleType with id "does-not-exist" not found'
)
})
})
describe("createRuleTypes", () => {
it("should create a ruleType successfully", async () => {
await service.createRuleTypes([
{
name: "Test Rule",
rule_attribute: "region_id",
},
])
const [ruleType] = await service.listRuleTypes({
name: ["Test Rule"],
})
expect(ruleType).toEqual(
expect.objectContaining({
name: "Test Rule",
rule_attribute: "region_id",
})
)
})
})
})
})
},
})
@@ -1,274 +0,0 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { RuleTypeService } from "@services"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { MikroOrmWrapper } from "../../../utils"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
jest.setTimeout(30000)
describe("RuleType Service", () => {
let service: RuleTypeService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("ruleTypeService")
testManager = await MikroOrmWrapper.forkManager()
await createRuleTypes(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("list", () => {
it("list rule types", async () => {
const ruleTypeResult = await service.list()
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
expect.objectContaining({
id: "rule-type-2",
name: "rule 2",
}),
])
})
it("list rule types by id", async () => {
const ruleTypeResult = await service.list({ id: ["rule-type-1"] })
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
])
})
})
describe("listAndCount", () => {
it("should return rule types and count", async () => {
const [ruleTypeResult, count] = await service.listAndCount()
expect(count).toEqual(2)
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
expect.objectContaining({
id: "rule-type-2",
name: "rule 2",
}),
])
})
it("should return rule types and count when filtered", async () => {
const [ruleTypeResult, count] = await service.listAndCount({
id: ["rule-type-1"],
})
expect(count).toEqual(1)
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
}),
])
})
it("should return rule types and count when using skip and take", async () => {
const [ruleTypeResult, count] = await service.listAndCount(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(2)
expect(ruleTypeResult).toEqual([
expect.objectContaining({
id: "rule-type-2",
name: "rule 2",
}),
])
})
it("should return requested fields", async () => {
const [ruleTypeResult, count] = await service.listAndCount(
{},
{
take: 1,
select: ["name"],
}
)
const serialized = JSON.parse(JSON.stringify(ruleTypeResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "rule-type-1",
name: "rule 1",
},
])
})
})
describe("retrieve", () => {
it("should return ruleType for the given id", async () => {
const ruleType = await service.retrieve("rule-type-1")
expect(ruleType).toEqual(
expect.objectContaining({
id: "rule-type-1",
name: "rule 1",
})
)
})
it("should throw an error when ruleType with id does not exist", async () => {
let error
try {
await service.retrieve("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"RuleType with id: does-not-exist was not found"
)
})
it("should throw an error when an id is not provided", async () => {
let error
try {
await service.retrieve(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("ruleType - id must be defined")
})
it("should return ruleType based on config select param", async () => {
const ruleTypeResult = await service.retrieve("rule-type-1", {
select: ["name"],
})
const serialized = JSON.parse(JSON.stringify(ruleTypeResult))
expect(serialized).toEqual({
name: "rule 1",
id: "rule-type-1",
})
})
})
describe("delete", () => {
const id = "rule-type-1"
it("should delete the ruleTypes given an id successfully", async () => {
await service.delete([id])
const ruleTypes = await service.list({
id: [id],
})
expect(ruleTypes).toHaveLength(0)
})
})
describe("update", () => {
const id = "rule-type-1"
it("should update the name of the ruleType successfully", async () => {
await service.update([
{
id,
name: "rule 3",
},
])
const ruletype = await service.retrieve(id)
expect(ruletype.name).toEqual("rule 3")
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.update([
{
id: "does-not-exist",
name: "rule 3",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'RuleType with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should create a ruleType successfully", async () => {
await service.create([
{
name: "Test Rule",
rule_attribute: "region_id",
},
])
const [ruleType] = await service.list({
name: ["Test Rule"],
})
expect(ruleType).toEqual(
expect.objectContaining({
name: "Test Rule",
rule_attribute: "region_id",
})
)
})
it("should throw an error when using one of the reserved keywords", async () => {
let error
try {
await service.create([
{
name: "Test Rule",
rule_attribute: "currency_code",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
"Can't create rule_attribute with reserved keywords [quantity, currency_code, price_list_id] - currency_code"
)
})
})
})
@@ -1,6 +0,0 @@
if (typeof process.env.DB_TEMP_NAME === "undefined") {
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
process.env.DB_TEMP_NAME = `medusa-pricing-integration-${tempName}`
}
process.env.MEDUSA_PRICING_DB_SCHEMA = "public"
@@ -1,3 +0,0 @@
import { JestUtils } from "medusa-test-utils"
JestUtils.afterAllHookDropDatabase()
@@ -1,6 +0,0 @@
import { ModuleServiceInitializeOptions } from "@medusajs/types"
export const databaseOptions: ModuleServiceInitializeOptions["database"] = {
schema: "public",
clientUrl: "medusa-pricing-test",
}
@@ -1,18 +0,0 @@
import { TestDatabaseUtils } from "medusa-test-utils"
import * as PricingModels from "@models"
const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = PricingModels as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations,
})
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()
@@ -1,31 +0,0 @@
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
import { DB_URL } from "./database"
export function getInitModuleConfig() {
const moduleOptions = {
defaultAdapterOptions: {
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRICING_DB_SCHEMA,
},
},
}
const modulesConfig_ = {
[Modules.PRODUCT]: true,
[Modules.PRICING]: {
definition: ModulesDefinition[Modules.PRICING],
options: moduleOptions,
},
}
return {
modulesConfig: modulesConfig_,
databaseConfig: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRICING_DB_SCHEMA,
},
joinerConfig: [],
}
}
@@ -1 +0,0 @@
export * from "./database"