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:
@@ -1,245 +1,231 @@
|
||||
import { AuthUserService } from "@services"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { asValue } from "awilix"
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthUser Service", () => {
|
||||
let service: AuthUserService
|
||||
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("authUserService")
|
||||
|
||||
await createAuthUsers(testManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const authUsers = await service.list()
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authUsers by id", async () => {
|
||||
const authUsers = await service.list({
|
||||
id: ["test-id"],
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.AUTH,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IAuthModuleService>) => {
|
||||
describe("AuthUser Service", () => {
|
||||
beforeEach(async () => {
|
||||
await createAuthUsers(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
])
|
||||
})
|
||||
describe("list", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const authUsers = await service.list()
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
it("should list authUsers by provider_id", async () => {
|
||||
const authUsers = await service.list({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCount", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const [authUsers, count] = await service.listAndCount()
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should listAndCount authUsers by provider_id", async () => {
|
||||
const [authUsers, count] = await service.listAndCount({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should return an authUser for the given id", async () => {
|
||||
const authUser = await service.retrieve(id)
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return authUser based on config select param", async () => {
|
||||
const authUser = await service.retrieve(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
it("should list authUsers by id", async () => {
|
||||
const authUsers = await service.list({
|
||||
id: ["test-id"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authUser))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw an error when an authUser with the given id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"AuthUser with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a authUserId is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("authUser - id must be defined")
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
it("should delete the authUsers given an id successfully", async () => {
|
||||
const id = "test-id"
|
||||
|
||||
await service.delete([id])
|
||||
|
||||
const authUsers = await service.list({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(authUsers).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
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(
|
||||
'AuthUser with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should update authUser", async () => {
|
||||
const id = "test-id"
|
||||
|
||||
await service.update([
|
||||
{
|
||||
id,
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser] = await service.list({ id: [id] })
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a authUser successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
id: "test",
|
||||
provider: "manual",
|
||||
entity_id: "test",
|
||||
scope: "store",
|
||||
},
|
||||
])
|
||||
it("should list authUsers by provider_id", async () => {
|
||||
const authUsers = await service.list({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
const [authUser] = await service.list({
|
||||
id: ["test"],
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test",
|
||||
describe("listAndCount", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const [authUsers, count] = await service.listAndCount()
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
|
||||
it("should listAndCount authUsers by provider_id", async () => {
|
||||
const [authUsers, count] = await service.listAndCount({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should return an authUser for the given id", async () => {
|
||||
const authUser = await service.retrieve(id)
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return authUser based on config select param", async () => {
|
||||
const authUser = await service.retrieve(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authUser))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw an error when an authUser with the given id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"AuthUser with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a authUserId is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("authUser - id must be defined")
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
it("should delete the authUsers given an id successfully", async () => {
|
||||
const id = "test-id"
|
||||
|
||||
await service.delete([id])
|
||||
|
||||
const authUsers = await service.list({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(authUsers).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
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(
|
||||
'AuthUser with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should update authUser", async () => {
|
||||
const id = "test-id"
|
||||
|
||||
await service.update([
|
||||
{
|
||||
id,
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser] = await service.list({ id: [id] })
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a authUser successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
id: "test",
|
||||
provider: "manual",
|
||||
entity_id: "test",
|
||||
scope: "store",
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser] = await service.list({
|
||||
id: ["test"],
|
||||
})
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,257 +1,237 @@
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthModuleService - AuthUser", () => {
|
||||
let service: IAuthModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.AUTH]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
testManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
await createAuthUsers(testManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
describe("listAuthUsers", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const authUsers = await service.list()
|
||||
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authUsers by id", async () => {
|
||||
const authUsers = await service.list({
|
||||
id: ["test-id"],
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.AUTH,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IAuthModuleService>) => {
|
||||
describe("AuthModuleService - AuthUser", () => {
|
||||
beforeEach(async () => {
|
||||
await createAuthUsers(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
])
|
||||
})
|
||||
describe("listAuthUsers", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const authUsers = await service.list()
|
||||
|
||||
it("should list authUsers by provider", async () => {
|
||||
const authUsers = await service.list({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCountAuthUsers", () => {
|
||||
it("should list and count authUsers", async () => {
|
||||
const [authUsers, count] = await service.listAndCount()
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should listAndCount authUsers by provider_id", async () => {
|
||||
const [authUsers, count] = await service.listAndCount({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should return an authUser for the given id", async () => {
|
||||
const authUser = await service.retrieve(id)
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an authUser with the given id does not exist", async () => {
|
||||
let error
|
||||
it("should list authUsers by id", async () => {
|
||||
const authUsers = await service.list({
|
||||
id: ["test-id"],
|
||||
})
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"AuthUser with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should not return an authUser with password hash", async () => {
|
||||
const authUser = await service.retrieve("test-id-1")
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
expect(authUser["password_hash"]).toEqual(undefined)
|
||||
})
|
||||
|
||||
it("should throw an error when a authUserId is not provided", async () => {
|
||||
let error
|
||||
it("should list authUsers by provider", async () => {
|
||||
const authUsers = await service.list({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("authUser - id must be defined")
|
||||
})
|
||||
|
||||
it("should return authUser based on config select param", async () => {
|
||||
const authUser = await service.retrieve(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
expect(authUser).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should delete the authUsers given an id successfully", async () => {
|
||||
await service.delete([id])
|
||||
|
||||
const authUsers = await service.list({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(authUsers).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
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(
|
||||
'AuthUser with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should update authUser", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id,
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser] = await service.list({ id: [id] })
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createAuthUser", () => {
|
||||
it("should create a authUser successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
id: "test",
|
||||
provider: "manual",
|
||||
entity_id: "test",
|
||||
scope: "store",
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser, count] = await service.listAndCount({
|
||||
id: ["test"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(authUser[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test",
|
||||
describe("listAndCountAuthUsers", () => {
|
||||
it("should list and count authUsers", async () => {
|
||||
const [authUsers, count] = await service.listAndCount()
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
|
||||
it("should listAndCount authUsers by provider_id", async () => {
|
||||
const [authUsers, count] = await service.listAndCount({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should return an authUser for the given id", async () => {
|
||||
const authUser = await service.retrieve(id)
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an authUser with the given id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"AuthUser with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should not return an authUser with password hash", async () => {
|
||||
const authUser = await service.retrieve("test-id-1")
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
})
|
||||
)
|
||||
expect(authUser["password_hash"]).toEqual(undefined)
|
||||
})
|
||||
|
||||
it("should throw an error when a authUserId is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("authUser - id must be defined")
|
||||
})
|
||||
|
||||
it("should return authUser based on config select param", async () => {
|
||||
const authUser = await service.retrieve(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
expect(authUser).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should delete the authUsers given an id successfully", async () => {
|
||||
await service.delete([id])
|
||||
|
||||
const authUsers = await service.list({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(authUsers).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
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(
|
||||
'AuthUser with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should update authUser", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id,
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser] = await service.list({ id: [id] })
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createAuthUser", () => {
|
||||
it("should create a authUser successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
id: "test",
|
||||
provider: "manual",
|
||||
entity_id: "test",
|
||||
scope: "store",
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser, count] = await service.listAndCount({
|
||||
id: ["test"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(authUser[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,68 +1,41 @@
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthModuleService - AuthProvider", () => {
|
||||
let service: IAuthModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.AUTH,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IAuthModuleService>) => {
|
||||
describe("AuthModuleService - AuthProvider", () => {
|
||||
describe("authenticate", () => {
|
||||
it("authenticate validates that a provider is registered in container", async () => {
|
||||
const { success, error } = await service.authenticate(
|
||||
"notRegistered",
|
||||
{} as any
|
||||
)
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
expect(success).toBe(false)
|
||||
expect(error).toEqual(
|
||||
"AuthenticationProvider: notRegistered wasn't registered in the module. Have you configured your options correctly?"
|
||||
)
|
||||
})
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
it("fails to authenticate using a valid provider with an invalid scope", async () => {
|
||||
const { success, error } = await service.authenticate("emailpass", {
|
||||
authScope: "non-existing",
|
||||
} as any)
|
||||
|
||||
service = medusaApp.modules[Modules.AUTH]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
testManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
if (service.__hooks?.onApplicationStart) {
|
||||
await service.__hooks.onApplicationStart()
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
MedusaModule.clearInstances()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
describe("authenticate", () => {
|
||||
it("authenticate validates that a provider is registered in container", async () => {
|
||||
const { success, error } = await service.authenticate(
|
||||
"notRegistered",
|
||||
{} as any
|
||||
)
|
||||
|
||||
expect(success).toBe(false)
|
||||
expect(error).toEqual(
|
||||
"AuthenticationProvider: notRegistered wasn't registered in the module. Have you configured your options correctly?"
|
||||
)
|
||||
expect(success).toBe(false)
|
||||
expect(error).toEqual(
|
||||
`Scope "non-existing" is not valid for provider emailpass`
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("fails to authenticate using a valid provider with an invalid scope", async () => {
|
||||
const { success, error } = await service.authenticate("emailpass", {
|
||||
authScope: "non-existing",
|
||||
} as any)
|
||||
|
||||
expect(success).toBe(false)
|
||||
expect(error).toEqual(
|
||||
`Scope "non-existing" is not valid for provider emailpass`
|
||||
)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,149 +1,133 @@
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import Scrypt from "scrypt-kdf"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
const seedDefaultData = async (testManager) => {
|
||||
await createAuthUsers(testManager)
|
||||
const seedDefaultData = async (manager) => {
|
||||
await createAuthUsers(manager)
|
||||
}
|
||||
|
||||
describe("AuthModuleService - AuthProvider", () => {
|
||||
let service: IAuthModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.AUTH]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
testManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
if (service.__hooks?.onApplicationStart) {
|
||||
await service.__hooks.onApplicationStart()
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
MedusaModule.clearInstances()
|
||||
})
|
||||
|
||||
describe("authenticate", () => {
|
||||
it("authenticate validates that a provider is registered in container", async () => {
|
||||
const password = "supersecret"
|
||||
const email = "test@test.com"
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
|
||||
await seedDefaultData(testManager)
|
||||
await createAuthUsers(testManager, [
|
||||
// Add authenticated user
|
||||
{
|
||||
provider: "emailpass",
|
||||
entity_id: email,
|
||||
scope: "store",
|
||||
provider_metadata: {
|
||||
password: passwordHash,
|
||||
},
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.AUTH,
|
||||
moduleOptions: {
|
||||
providers: [
|
||||
{
|
||||
name: "emailpass",
|
||||
scopes: {
|
||||
admin: {},
|
||||
store: {},
|
||||
},
|
||||
])
|
||||
},
|
||||
],
|
||||
},
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IAuthModuleService>) => {
|
||||
describe("AuthModuleService - AuthProvider", () => {
|
||||
describe("authenticate", () => {
|
||||
it("authenticate validates that a provider is registered in container", async () => {
|
||||
const password = "supersecret"
|
||||
const email = "test@test.com"
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: {
|
||||
email: "test@test.com",
|
||||
password: password,
|
||||
},
|
||||
authScope: "store",
|
||||
} as any)
|
||||
await seedDefaultData(MikroOrmWrapper.forkManager())
|
||||
await createAuthUsers(MikroOrmWrapper.forkManager(), [
|
||||
// Add authenticated user
|
||||
{
|
||||
provider: "emailpass",
|
||||
entity_id: email,
|
||||
scope: "store",
|
||||
provider_metadata: {
|
||||
password: passwordHash,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
expect(res).toEqual({
|
||||
success: true,
|
||||
authUser: expect.objectContaining({
|
||||
entity_id: email,
|
||||
provider_metadata: {},
|
||||
}),
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: {
|
||||
email: "test@test.com",
|
||||
password: password,
|
||||
},
|
||||
authScope: "store",
|
||||
} as any)
|
||||
|
||||
expect(res).toEqual({
|
||||
success: true,
|
||||
authUser: expect.objectContaining({
|
||||
entity_id: email,
|
||||
provider_metadata: {},
|
||||
}),
|
||||
})
|
||||
})
|
||||
|
||||
it("fails when no password is given", async () => {
|
||||
await seedDefaultData(MikroOrmWrapper.forkManager())
|
||||
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: { email: "test@test.com" },
|
||||
authScope: "store",
|
||||
} as any)
|
||||
|
||||
expect(res).toEqual({
|
||||
success: false,
|
||||
error: "Password should be a string",
|
||||
})
|
||||
})
|
||||
|
||||
it("fails when no email is given", async () => {
|
||||
await seedDefaultData(MikroOrmWrapper.forkManager())
|
||||
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: { password: "supersecret" },
|
||||
authScope: "store",
|
||||
} as any)
|
||||
|
||||
expect(res).toEqual({
|
||||
success: false,
|
||||
error: "Email should be a string",
|
||||
})
|
||||
})
|
||||
|
||||
it("fails with an invalid password", async () => {
|
||||
const password = "supersecret"
|
||||
const email = "test@test.com"
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
|
||||
await seedDefaultData(MikroOrmWrapper.forkManager())
|
||||
await createAuthUsers(MikroOrmWrapper.forkManager(), [
|
||||
// Add authenticated user
|
||||
{
|
||||
provider: "emailpass",
|
||||
scope: "store",
|
||||
entity_id: email,
|
||||
provider_metadata: {
|
||||
password_hash: passwordHash,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: {
|
||||
email: "test@test.com",
|
||||
password: "password",
|
||||
},
|
||||
authScope: "store",
|
||||
} as any)
|
||||
|
||||
expect(res).toEqual({
|
||||
success: false,
|
||||
error: "Invalid email or password",
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("fails when no password is given", async () => {
|
||||
await seedDefaultData(testManager)
|
||||
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: { email: "test@test.com" },
|
||||
authScope: "store",
|
||||
} as any)
|
||||
|
||||
expect(res).toEqual({
|
||||
success: false,
|
||||
error: "Password should be a string",
|
||||
})
|
||||
})
|
||||
|
||||
it("fails when no email is given", async () => {
|
||||
await seedDefaultData(testManager)
|
||||
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: { password: "supersecret" },
|
||||
authScope: "store",
|
||||
} as any)
|
||||
|
||||
expect(res).toEqual({
|
||||
success: false,
|
||||
error: "Email should be a string",
|
||||
})
|
||||
})
|
||||
|
||||
it("fails with an invalid password", async () => {
|
||||
const password = "supersecret"
|
||||
const email = "test@test.com"
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
|
||||
await seedDefaultData(testManager)
|
||||
await createAuthUsers(testManager, [
|
||||
// Add authenticated user
|
||||
{
|
||||
provider: "emailpass",
|
||||
scope: "store",
|
||||
entity_id: email,
|
||||
provider_metadata: {
|
||||
password_hash: passwordHash,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: {
|
||||
email: "test@test.com",
|
||||
password: "password",
|
||||
},
|
||||
authScope: "store",
|
||||
} as any)
|
||||
|
||||
expect(res).toEqual({
|
||||
success: false,
|
||||
error: "Invalid email or password",
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -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-auth-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_AUTH_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-auth-test",
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import * as AuthModels from "@models"
|
||||
|
||||
import { TestDatabaseUtils } from "medusa-test-utils"
|
||||
|
||||
const pathToMigrations = "../../src/migrations"
|
||||
const mikroOrmEntities = AuthModels 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,42 +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_AUTH_DB_SCHEMA,
|
||||
},
|
||||
},
|
||||
providers: [
|
||||
{
|
||||
name: "emailpass",
|
||||
scopes: {
|
||||
admin: {},
|
||||
store: {},
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const injectedDependencies = {}
|
||||
|
||||
const modulesConfig_ = {
|
||||
[Modules.AUTH]: {
|
||||
definition: ModulesDefinition[Modules.AUTH],
|
||||
options: moduleOptions,
|
||||
},
|
||||
}
|
||||
|
||||
return {
|
||||
injectedDependencies,
|
||||
modulesConfig: modulesConfig_,
|
||||
databaseConfig: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_AUTH_DB_SCHEMA,
|
||||
},
|
||||
joinerConfig: [],
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from "./config"
|
||||
export * from "./database"
|
||||
@@ -18,6 +18,4 @@ module.exports = {
|
||||
testEnvironment: `node`,
|
||||
moduleFileExtensions: [`js`, `ts`],
|
||||
modulePathIgnorePatterns: ["dist/"],
|
||||
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
|
||||
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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-cart-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_CART_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-cart-test",
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import { TestDatabaseUtils } from "medusa-test-utils"
|
||||
|
||||
import * as CartModels from "@models"
|
||||
|
||||
const pathToMigrations = "../../src/migrations"
|
||||
const mikroOrmEntities = CartModels 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,33 +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_CART_DB_SCHEMA,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const injectedDependencies = {}
|
||||
|
||||
const modulesConfig_ = {
|
||||
[Modules.CART]: {
|
||||
definition: ModulesDefinition[Modules.CART],
|
||||
options: moduleOptions,
|
||||
},
|
||||
}
|
||||
|
||||
return {
|
||||
injectedDependencies,
|
||||
modulesConfig: modulesConfig_,
|
||||
databaseConfig: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_CART_DB_SCHEMA,
|
||||
},
|
||||
joinerConfig: [],
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from "./config"
|
||||
export * from "./database"
|
||||
@@ -18,6 +18,4 @@ module.exports = {
|
||||
testEnvironment: `node`,
|
||||
moduleFileExtensions: [`js`, `ts`],
|
||||
modulePathIgnorePatterns: ["dist/"],
|
||||
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
|
||||
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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-customer-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_CUSTOMER_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-customer-test",
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import { TestDatabaseUtils } from "medusa-test-utils"
|
||||
|
||||
import * as Models from "@models"
|
||||
|
||||
const pathToMigrations = "../../src/migrations"
|
||||
const mikroOrmEntities = Models 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,33 +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_CUSTOMER_DB_SCHEMA,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const injectedDependencies = {}
|
||||
|
||||
const modulesConfig_ = {
|
||||
[Modules.CUSTOMER]: {
|
||||
definition: ModulesDefinition[Modules.CUSTOMER],
|
||||
options: moduleOptions,
|
||||
},
|
||||
}
|
||||
|
||||
return {
|
||||
injectedDependencies,
|
||||
modulesConfig: modulesConfig_,
|
||||
databaseConfig: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_CUSTOMER_DB_SCHEMA,
|
||||
},
|
||||
joinerConfig: [],
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from "./config"
|
||||
export * from "./database"
|
||||
@@ -16,6 +16,4 @@ module.exports = {
|
||||
testEnvironment: `node`,
|
||||
moduleFileExtensions: [`js`, `ts`],
|
||||
modulePathIgnorePatterns: ["dist/"],
|
||||
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
|
||||
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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" } }),
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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"
|
||||
@@ -18,6 +18,4 @@ module.exports = {
|
||||
testEnvironment: `node`,
|
||||
moduleFileExtensions: [`js`, `ts`],
|
||||
modulePathIgnorePatterns: ["dist/"],
|
||||
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
|
||||
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
import {
|
||||
EmitData,
|
||||
EventBusTypes,
|
||||
IEventBusModuleService,
|
||||
Message,
|
||||
Subscriber,
|
||||
} from "@medusajs/types"
|
||||
|
||||
export class EventBusService implements IEventBusModuleService {
|
||||
emit<T>(
|
||||
eventName: string,
|
||||
data: T,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<void>
|
||||
emit<T>(data: EmitData<T>[]): Promise<void>
|
||||
emit<T>(data: Message<T>[]): Promise<void>
|
||||
|
||||
async emit<
|
||||
T,
|
||||
TInput extends
|
||||
| string
|
||||
| EventBusTypes.EmitData<T>[]
|
||||
| EventBusTypes.Message<T>[] = string
|
||||
>(
|
||||
eventOrData: TInput,
|
||||
data?: T,
|
||||
options: Record<string, unknown> = {}
|
||||
): Promise<void> {}
|
||||
|
||||
subscribe(event: string | symbol, subscriber: Subscriber): this {
|
||||
return this
|
||||
}
|
||||
|
||||
unsubscribe(
|
||||
event: string | symbol,
|
||||
subscriber: Subscriber,
|
||||
context?: EventBusTypes.SubscriberContext
|
||||
): this {
|
||||
return this
|
||||
}
|
||||
|
||||
withTransaction() {}
|
||||
}
|
||||
@@ -1,482 +0,0 @@
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { kebabCase } from "@medusajs/utils"
|
||||
import { knex } from "@mikro-orm/knex"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import * as CustomRepositories from "../__fixtures__/module"
|
||||
import {
|
||||
buildProductAndRelationsData,
|
||||
createProductAndTags,
|
||||
} from "../__fixtures__/product"
|
||||
import { productsData } from "../__fixtures__/product/data"
|
||||
import { DB_URL, TestDatabase } from "../utils"
|
||||
import { getInitModuleConfig } from "../utils/get-init-module-config"
|
||||
|
||||
const sharedPgConnection = knex<any, any>({
|
||||
client: "pg",
|
||||
searchPath: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
|
||||
connection: {
|
||||
connectionString: DB_URL,
|
||||
},
|
||||
})
|
||||
|
||||
const beforeEach_ = async () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
return await TestDatabase.forkManager()
|
||||
}
|
||||
|
||||
const afterEach_ = async () => {
|
||||
await TestDatabase.clearDatabase()
|
||||
}
|
||||
|
||||
describe("Product module", function () {
|
||||
describe("Using built-in data access layer", function () {
|
||||
let module: IProductModuleService
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
beforeAll(async () => {
|
||||
MedusaModule.clearInstances()
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
module = medusaApp.modules[Modules.PRODUCT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
const testManager = await beforeEach_()
|
||||
await createProductAndTags(testManager, productsData)
|
||||
})
|
||||
|
||||
afterEach(afterEach_)
|
||||
|
||||
it("should initialize", async () => {
|
||||
expect(module).toBeDefined()
|
||||
})
|
||||
|
||||
it("should have a connection that is not the shared connection", async () => {
|
||||
expect(
|
||||
(module as any).baseRepository_.manager_.getConnection().client
|
||||
).not.toEqual(sharedPgConnection)
|
||||
})
|
||||
|
||||
it("should return a list of product", async () => {
|
||||
const products = await module.list()
|
||||
expect(products).toHaveLength(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Using custom data access layer", function () {
|
||||
let module
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
beforeAll(async () => {
|
||||
MedusaModule.clearInstances()
|
||||
const initModulesConfig = getInitModuleConfig({
|
||||
repositories: CustomRepositories,
|
||||
})
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
module = medusaApp.modules[Modules.PRODUCT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
const testManager = await beforeEach_()
|
||||
await createProductAndTags(testManager, productsData)
|
||||
})
|
||||
|
||||
afterEach(afterEach_)
|
||||
|
||||
it("should initialize", async () => {
|
||||
expect(module).toBeDefined()
|
||||
})
|
||||
|
||||
it("should have a connection that is not the shared connection", async () => {
|
||||
expect(
|
||||
(module as any).baseRepository_.manager_.getConnection().client
|
||||
).not.toEqual(sharedPgConnection)
|
||||
})
|
||||
|
||||
it("should return a list of product", async () => {
|
||||
const products = await module.list()
|
||||
|
||||
expect(module.productService_.productRepository_.find).toHaveBeenCalled()
|
||||
expect(products).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Using custom data access layer and manager", function () {
|
||||
let module
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
afterEach(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
const testManager = await beforeEach_()
|
||||
await createProductAndTags(testManager, productsData)
|
||||
|
||||
MedusaModule.clearInstances()
|
||||
const initModulesConfig = getInitModuleConfig({
|
||||
manager: testManager,
|
||||
repositories: CustomRepositories,
|
||||
})
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
module = medusaApp.modules[Modules.PRODUCT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterEach(afterEach_)
|
||||
|
||||
it("should initialize and return a list of product", async () => {
|
||||
expect(module).toBeDefined()
|
||||
})
|
||||
|
||||
it("should have a connection that is not the shared connection", async () => {
|
||||
expect(
|
||||
(module as any).baseRepository_.manager_.getConnection().client
|
||||
).not.toEqual(sharedPgConnection)
|
||||
})
|
||||
|
||||
it("should return a list of product", async () => {
|
||||
const products = await module.list()
|
||||
|
||||
expect(module.productService_.productRepository_.find).toHaveBeenCalled()
|
||||
expect(products).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Using an existing connection", function () {
|
||||
let module: IProductModuleService
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
afterEach(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
const testManager = await beforeEach_()
|
||||
await createProductAndTags(testManager, productsData)
|
||||
|
||||
MedusaModule.clearInstances()
|
||||
const initModulesConfig = getInitModuleConfig({
|
||||
database: {
|
||||
connection: sharedPgConnection,
|
||||
},
|
||||
})
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
module = medusaApp.modules[Modules.PRODUCT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterEach(afterEach_)
|
||||
|
||||
it("should initialize and return a list of product", async () => {
|
||||
expect(module).toBeDefined()
|
||||
})
|
||||
|
||||
it("should have a connection that is the shared connection", async () => {
|
||||
expect(
|
||||
JSON.stringify(
|
||||
(module as any).baseRepository_.manager_.getConnection().client
|
||||
)
|
||||
).toEqual(JSON.stringify(sharedPgConnection))
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", function () {
|
||||
let module: IProductModuleService
|
||||
let images = ["image-1"]
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
afterEach(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await beforeEach_()
|
||||
|
||||
MedusaModule.clearInstances()
|
||||
const initModulesConfig = getInitModuleConfig({
|
||||
database: {
|
||||
connection: sharedPgConnection,
|
||||
},
|
||||
})
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
module = medusaApp.modules[Modules.PRODUCT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterEach(afterEach_)
|
||||
|
||||
it("should create a product", async () => {
|
||||
const data = buildProductAndRelationsData({
|
||||
images,
|
||||
thumbnail: images[0],
|
||||
})
|
||||
|
||||
const productsCreated = await module.create([data])
|
||||
|
||||
const products = await module.list(
|
||||
{ id: productsCreated[0].id },
|
||||
{
|
||||
relations: [
|
||||
"images",
|
||||
"categories",
|
||||
"variants",
|
||||
"variants.options",
|
||||
"options",
|
||||
"options.values",
|
||||
"tags",
|
||||
"type",
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
expect(products).toHaveLength(1)
|
||||
|
||||
expect(products[0].images).toHaveLength(1)
|
||||
expect(products[0].options).toHaveLength(1)
|
||||
expect(products[0].tags).toHaveLength(1)
|
||||
expect(products[0].categories).toHaveLength(0)
|
||||
expect(products[0].variants).toHaveLength(1)
|
||||
|
||||
expect(products[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
title: data.title,
|
||||
handle: kebabCase(data.title),
|
||||
description: data.description,
|
||||
subtitle: data.subtitle,
|
||||
is_giftcard: data.is_giftcard,
|
||||
discountable: data.discountable,
|
||||
thumbnail: images[0],
|
||||
status: data.status,
|
||||
images: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
url: images[0],
|
||||
}),
|
||||
]),
|
||||
options: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
title: data.options[0].title,
|
||||
values: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: data.variants[0].options?.[0].value,
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
]),
|
||||
tags: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: data.tags[0].value,
|
||||
}),
|
||||
]),
|
||||
type: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: data.type.value,
|
||||
}),
|
||||
variants: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
title: data.variants[0].title,
|
||||
sku: data.variants[0].sku,
|
||||
allow_backorder: false,
|
||||
manage_inventory: true,
|
||||
inventory_quantity: 100,
|
||||
variant_rank: 0,
|
||||
options: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: data.variants[0].options?.[0].value,
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("softDelete", function () {
|
||||
let module: IProductModuleService
|
||||
let images = ["image-1"]
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
afterEach(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await beforeEach_()
|
||||
|
||||
MedusaModule.clearInstances()
|
||||
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
module = medusaApp.modules[Modules.PRODUCT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterEach(afterEach_)
|
||||
|
||||
it("should soft delete a product and its cascaded relations", async () => {
|
||||
const data = buildProductAndRelationsData({
|
||||
images,
|
||||
thumbnail: images[0],
|
||||
})
|
||||
|
||||
const products = await module.create([data])
|
||||
|
||||
await module.softDelete([products[0].id])
|
||||
const deletedProducts = await module.list(
|
||||
{ id: products[0].id },
|
||||
{
|
||||
relations: [
|
||||
"variants",
|
||||
"variants.options",
|
||||
"options",
|
||||
"options.values",
|
||||
],
|
||||
withDeleted: true,
|
||||
}
|
||||
)
|
||||
|
||||
expect(deletedProducts).toHaveLength(1)
|
||||
expect(deletedProducts[0].deleted_at).not.toBeNull()
|
||||
|
||||
for (const option of deletedProducts[0].options) {
|
||||
expect(option.deleted_at).not.toBeNull()
|
||||
}
|
||||
|
||||
const productOptionsValues = deletedProducts[0].options
|
||||
.map((o) => o.values)
|
||||
.flat()
|
||||
|
||||
for (const optionValue of productOptionsValues) {
|
||||
expect(optionValue.deleted_at).not.toBeNull()
|
||||
}
|
||||
|
||||
for (const variant of deletedProducts[0].variants) {
|
||||
expect(variant.deleted_at).not.toBeNull()
|
||||
}
|
||||
|
||||
const variantsOptions = deletedProducts[0].options
|
||||
.map((o) => o.values)
|
||||
.flat()
|
||||
|
||||
for (const option of variantsOptions) {
|
||||
expect(option.deleted_at).not.toBeNull()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe("restore", function () {
|
||||
let module: IProductModuleService
|
||||
let images = ["image-1"]
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
afterEach(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await beforeEach_()
|
||||
|
||||
MedusaModule.clearInstances()
|
||||
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
module = medusaApp.modules[Modules.PRODUCT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterEach(afterEach_)
|
||||
|
||||
it("should restore a soft deleted product and its cascaded relations", async () => {
|
||||
const data = buildProductAndRelationsData({
|
||||
images,
|
||||
thumbnail: images[0],
|
||||
})
|
||||
|
||||
const products = await module.create([data])
|
||||
|
||||
await module.softDelete([products[0].id])
|
||||
await module.restore([products[0].id])
|
||||
|
||||
const deletedProducts = await module.list(
|
||||
{ id: products[0].id },
|
||||
{
|
||||
relations: [
|
||||
"variants",
|
||||
"variants.options",
|
||||
"variants.options",
|
||||
"options",
|
||||
"options.values",
|
||||
],
|
||||
withDeleted: true,
|
||||
}
|
||||
)
|
||||
|
||||
expect(deletedProducts).toHaveLength(1)
|
||||
expect(deletedProducts[0].deleted_at).toBeNull()
|
||||
|
||||
for (const option of deletedProducts[0].options) {
|
||||
expect(option.deleted_at).toBeNull()
|
||||
}
|
||||
|
||||
const productOptionsValues = deletedProducts[0].options
|
||||
.map((o) => o.values)
|
||||
.flat()
|
||||
|
||||
for (const optionValue of productOptionsValues) {
|
||||
expect(optionValue.deleted_at).toBeNull()
|
||||
}
|
||||
|
||||
for (const variant of deletedProducts[0].variants) {
|
||||
expect(variant.deleted_at).toBeNull()
|
||||
}
|
||||
|
||||
const variantsOptions = deletedProducts[0].options
|
||||
.map((o) => o.values)
|
||||
.flat()
|
||||
|
||||
for (const option of variantsOptions) {
|
||||
expect(option.deleted_at).toBeNull()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,366 +1,367 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { ProductCollection } from "@models"
|
||||
import { ProductCollectionService } from "@services"
|
||||
|
||||
import { createCollections } from "../../../__fixtures__/product"
|
||||
import { TestDatabase } from "../../../utils"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Product collection Service", () => {
|
||||
let service: ProductCollectionService
|
||||
let testManager: SqlEntityManager
|
||||
let repositoryManager: SqlEntityManager
|
||||
let collectionsData: ProductCollection[] = []
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PRODUCT,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
medusaApp,
|
||||
}: SuiteOptions<IProductModuleService>) => {
|
||||
describe("Product collection Service", () => {
|
||||
let service: ProductCollectionService
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productCollectionService")
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await TestDatabase.clearDatabase()
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
const data = [
|
||||
{
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
},
|
||||
{
|
||||
id: "test-2",
|
||||
title: "col 2",
|
||||
},
|
||||
{
|
||||
id: "test-3",
|
||||
title: "col 3 extra",
|
||||
},
|
||||
{
|
||||
id: "test-4",
|
||||
title: "col 4 extra",
|
||||
},
|
||||
]
|
||||
|
||||
beforeEach(async () => {
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
collectionsData = await createCollections(testManager, data)
|
||||
})
|
||||
|
||||
it("list product collections", async () => {
|
||||
const productCollectionResults = await service.list()
|
||||
|
||||
expect(productCollectionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-2",
|
||||
title: "col 2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-3",
|
||||
title: "col 3 extra",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-4",
|
||||
title: "col 4 extra",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product collections by id", async () => {
|
||||
const productCollectionResults = await service.list({ id: data![0].id })
|
||||
|
||||
expect(productCollectionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product collections by title matching string", async () => {
|
||||
const productCollectionResults = await service.list({
|
||||
title: "col 3 extra",
|
||||
beforeEach(() => {
|
||||
service = medusaApp.modules["productService"].productCollectionService_
|
||||
})
|
||||
|
||||
expect(productCollectionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-3",
|
||||
title: "col 3 extra",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCount", () => {
|
||||
const data = [
|
||||
{
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
},
|
||||
{
|
||||
id: "test-2",
|
||||
title: "col 2",
|
||||
},
|
||||
{
|
||||
id: "test-3",
|
||||
title: "col 3 extra",
|
||||
},
|
||||
{
|
||||
id: "test-4",
|
||||
title: "col 4 extra",
|
||||
},
|
||||
]
|
||||
|
||||
beforeEach(async () => {
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
collectionsData = await createCollections(testManager, data)
|
||||
})
|
||||
|
||||
it("should return all collections and count", async () => {
|
||||
const [productCollectionResults, count] = await service.listAndCount()
|
||||
const serialized = JSON.parse(JSON.stringify(productCollectionResults))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-2",
|
||||
title: "col 2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-3",
|
||||
title: "col 3 extra",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-4",
|
||||
title: "col 4 extra",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return count and collections based on filter data", async () => {
|
||||
const [productCollectionResults, count] = await service.listAndCount({
|
||||
id: data![0].id,
|
||||
})
|
||||
const serialized = JSON.parse(JSON.stringify(productCollectionResults))
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return count and collections based on config data", async () => {
|
||||
const [productCollectionResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
relations: ["products"],
|
||||
select: ["title"],
|
||||
take: 1,
|
||||
skip: 1,
|
||||
}
|
||||
)
|
||||
const serialized = JSON.parse(JSON.stringify(productCollectionResults))
|
||||
|
||||
expect(count).toEqual(4)
|
||||
expect(serialized).toEqual([
|
||||
{
|
||||
id: "test-2",
|
||||
title: "col 2",
|
||||
products: [],
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const collectionData = {
|
||||
id: "collection-1",
|
||||
title: "collection 1",
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
await createCollections(testManager, [collectionData])
|
||||
})
|
||||
|
||||
it("should return collection for the given id", async () => {
|
||||
const productCollectionResults = await service.retrieve(collectionData.id)
|
||||
|
||||
expect(productCollectionResults).toEqual(
|
||||
expect.objectContaining({
|
||||
id: collectionData.id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when collection with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductCollection 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("productCollection - id must be defined")
|
||||
})
|
||||
|
||||
it("should return collection based on config select param", async () => {
|
||||
const productCollectionResults = await service.retrieve(
|
||||
collectionData.id,
|
||||
{
|
||||
select: ["id", "title"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(productCollectionResults))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: collectionData.id,
|
||||
title: collectionData.title,
|
||||
})
|
||||
})
|
||||
|
||||
it("should return collection based on config relation param", async () => {
|
||||
const productCollectionResults = await service.retrieve(
|
||||
collectionData.id,
|
||||
{
|
||||
select: ["id", "title"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(productCollectionResults))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: collectionData.id,
|
||||
title: collectionData.title,
|
||||
products: [],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
const collectionId = "collection-1"
|
||||
const collectionData = {
|
||||
id: collectionId,
|
||||
title: "collection 1",
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
await createCollections(testManager, [collectionData])
|
||||
})
|
||||
|
||||
it("should delete the product collection given an ID successfully", async () => {
|
||||
await service.delete([collectionId])
|
||||
|
||||
const collections = await service.list({
|
||||
id: collectionId,
|
||||
})
|
||||
|
||||
expect(collections).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
const collectionId = "collection-1"
|
||||
const collectionData = {
|
||||
id: collectionId,
|
||||
title: "collection 1",
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
await createCollections(testManager, [collectionData])
|
||||
})
|
||||
|
||||
it("should update the value of the collection successfully", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id: collectionId,
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
const productCollection = await service.retrieve(collectionId)
|
||||
|
||||
expect(productCollection.title).toEqual("New Collection")
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
describe("list", () => {
|
||||
const data = [
|
||||
{
|
||||
id: "does-not-exist",
|
||||
title: "New Collection",
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
{
|
||||
id: "test-2",
|
||||
title: "col 2",
|
||||
},
|
||||
{
|
||||
id: "test-3",
|
||||
title: "col 3 extra",
|
||||
},
|
||||
{
|
||||
id: "test-4",
|
||||
title: "col 4 extra",
|
||||
},
|
||||
]
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductCollection with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
beforeEach(async () => {
|
||||
await createCollections(MikroOrmWrapper.forkManager(), data)
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a collection successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
it("list product collections", async () => {
|
||||
const productCollectionResults = await service.list()
|
||||
|
||||
const [productCollection] = await service.list({
|
||||
title: "New Collection",
|
||||
expect(productCollectionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-2",
|
||||
title: "col 2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-3",
|
||||
title: "col 3 extra",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-4",
|
||||
title: "col 4 extra",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product collections by id", async () => {
|
||||
const productCollectionResults = await service.list({
|
||||
id: data![0].id,
|
||||
})
|
||||
|
||||
expect(productCollectionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product collections by title matching string", async () => {
|
||||
const productCollectionResults = await service.list({
|
||||
title: "col 3 extra",
|
||||
})
|
||||
|
||||
expect(productCollectionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-3",
|
||||
title: "col 3 extra",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
expect(productCollection.title).toEqual("New Collection")
|
||||
describe("listAndCount", () => {
|
||||
const data = [
|
||||
{
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
},
|
||||
{
|
||||
id: "test-2",
|
||||
title: "col 2",
|
||||
},
|
||||
{
|
||||
id: "test-3",
|
||||
title: "col 3 extra",
|
||||
},
|
||||
{
|
||||
id: "test-4",
|
||||
title: "col 4 extra",
|
||||
},
|
||||
]
|
||||
|
||||
beforeEach(async () => {
|
||||
await createCollections(MikroOrmWrapper.forkManager(), data)
|
||||
})
|
||||
|
||||
it("should return all collections and count", async () => {
|
||||
const [productCollectionResults, count] = await service.listAndCount()
|
||||
const serialized = JSON.parse(
|
||||
JSON.stringify(productCollectionResults)
|
||||
)
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-2",
|
||||
title: "col 2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-3",
|
||||
title: "col 3 extra",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-4",
|
||||
title: "col 4 extra",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return count and collections based on filter data", async () => {
|
||||
const [productCollectionResults, count] = await service.listAndCount({
|
||||
id: data![0].id,
|
||||
})
|
||||
const serialized = JSON.parse(
|
||||
JSON.stringify(productCollectionResults)
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "col 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return count and collections based on config data", async () => {
|
||||
const [productCollectionResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
relations: ["products"],
|
||||
select: ["title"],
|
||||
take: 1,
|
||||
skip: 1,
|
||||
}
|
||||
)
|
||||
const serialized = JSON.parse(
|
||||
JSON.stringify(productCollectionResults)
|
||||
)
|
||||
|
||||
expect(count).toEqual(4)
|
||||
expect(serialized).toEqual([
|
||||
{
|
||||
id: "test-2",
|
||||
title: "col 2",
|
||||
products: [],
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const collectionData = {
|
||||
id: "collection-1",
|
||||
title: "collection 1",
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await createCollections(MikroOrmWrapper.forkManager(), [
|
||||
collectionData,
|
||||
])
|
||||
})
|
||||
|
||||
it("should return collection for the given id", async () => {
|
||||
const productCollectionResults = await service.retrieve(
|
||||
collectionData.id
|
||||
)
|
||||
|
||||
expect(productCollectionResults).toEqual(
|
||||
expect.objectContaining({
|
||||
id: collectionData.id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when collection with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductCollection 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(
|
||||
"productCollection - id must be defined"
|
||||
)
|
||||
})
|
||||
|
||||
it("should return collection based on config select param", async () => {
|
||||
const productCollectionResults = await service.retrieve(
|
||||
collectionData.id,
|
||||
{
|
||||
select: ["id", "title"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(
|
||||
JSON.stringify(productCollectionResults)
|
||||
)
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: collectionData.id,
|
||||
title: collectionData.title,
|
||||
})
|
||||
})
|
||||
|
||||
it("should return collection based on config relation param", async () => {
|
||||
const productCollectionResults = await service.retrieve(
|
||||
collectionData.id,
|
||||
{
|
||||
select: ["id", "title"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(
|
||||
JSON.stringify(productCollectionResults)
|
||||
)
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: collectionData.id,
|
||||
title: collectionData.title,
|
||||
products: [],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
const collectionId = "collection-1"
|
||||
const collectionData = {
|
||||
id: collectionId,
|
||||
title: "collection 1",
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await createCollections(MikroOrmWrapper.forkManager(), [
|
||||
collectionData,
|
||||
])
|
||||
})
|
||||
|
||||
it("should delete the product collection given an ID successfully", async () => {
|
||||
await service.delete([collectionId])
|
||||
|
||||
const collections = await service.list({
|
||||
id: collectionId,
|
||||
})
|
||||
|
||||
expect(collections).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
const collectionId = "collection-1"
|
||||
const collectionData = {
|
||||
id: collectionId,
|
||||
title: "collection 1",
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await createCollections(MikroOrmWrapper.forkManager(), [
|
||||
collectionData,
|
||||
])
|
||||
})
|
||||
|
||||
it("should update the value of the collection successfully", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id: collectionId,
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
const productCollection = await service.retrieve(collectionId)
|
||||
|
||||
expect(productCollection.title).toEqual("New Collection")
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductCollection with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a collection successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
const [productCollection] = await service.list({
|
||||
title: "New Collection",
|
||||
})
|
||||
|
||||
expect(productCollection.title).toEqual("New Collection")
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,442 +1,443 @@
|
||||
import { IProductModuleService, ProductTypes } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { Product, ProductCollection } from "@models"
|
||||
import { EventBusService } from "../../../__fixtures__/event-bus"
|
||||
import { MockEventBusService } from "medusa-test-utils"
|
||||
import { createCollections } from "../../../__fixtures__/product"
|
||||
import { getInitModuleConfig, TestDatabase } from "../../../utils"
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
describe("ProductModuleService product collections", () => {
|
||||
let service: IProductModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let productOne: Product
|
||||
let productTwo: Product
|
||||
let productCollectionOne: ProductCollection
|
||||
let productCollectionTwo: ProductCollection
|
||||
let productCollections: ProductCollection[]
|
||||
jest.setTimeout(30000)
|
||||
|
||||
let shutdownFunc: () => Promise<void>
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PRODUCT,
|
||||
injectedDependencies: {
|
||||
eventBusModuleService: new MockEventBusService(),
|
||||
},
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IProductModuleService>) => {
|
||||
describe("ProductModuleService product collections", () => {
|
||||
let productOne: Product
|
||||
let productTwo: Product
|
||||
let productCollectionOne: ProductCollection
|
||||
let productCollectionTwo: ProductCollection
|
||||
let productCollections: ProductCollection[]
|
||||
|
||||
beforeAll(async () => {
|
||||
MedusaModule.clearInstances()
|
||||
beforeEach(async () => {
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.PRODUCT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
productTwo = testManager.create(Product, {
|
||||
id: "product-2",
|
||||
title: "product 2",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
const productCollectionsData = [
|
||||
{
|
||||
id: "test-1",
|
||||
title: "collection 1",
|
||||
products: [productOne],
|
||||
},
|
||||
{
|
||||
id: "test-2",
|
||||
title: "collection",
|
||||
products: [productTwo],
|
||||
},
|
||||
]
|
||||
|
||||
productCollections = await createCollections(
|
||||
testManager,
|
||||
productCollectionsData
|
||||
)
|
||||
|
||||
productCollectionOne = productCollections[0]
|
||||
productCollectionTwo = productCollections[1]
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await TestDatabase.clearDatabase()
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
describe("listCollections", () => {
|
||||
it("should return collections queried by ID", async () => {
|
||||
const results = await service.listCollections({
|
||||
id: productCollectionOne.id,
|
||||
})
|
||||
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCollectionOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return collections based on the options and filter parameter", async () => {
|
||||
let results = await service.listCollections(
|
||||
{
|
||||
id: productCollectionOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCollectionOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
results = await service.listCollections({}, { take: 1, skip: 1 })
|
||||
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCollectionTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields and relations for collections", async () => {
|
||||
const results = await service.listCollections(
|
||||
{
|
||||
id: productCollectionOne.id,
|
||||
},
|
||||
{
|
||||
select: ["id", "title", "products.title"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "collection 1",
|
||||
products: [
|
||||
expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCountCollections", () => {
|
||||
it("should return collections and count queried by ID", async () => {
|
||||
const results = await service.listAndCountCollections({
|
||||
id: productCollectionOne.id,
|
||||
})
|
||||
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCollectionOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return collections and count based on the options and filter parameter", async () => {
|
||||
let results = await service.listAndCountCollections(
|
||||
{
|
||||
id: productCollectionOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCollectionOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
results = await service.listAndCountCollections({}, { take: 1 })
|
||||
|
||||
expect(results[1]).toEqual(2)
|
||||
|
||||
results = await service.listAndCountCollections({}, { take: 1, skip: 1 })
|
||||
|
||||
expect(results[1]).toEqual(2)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCollectionTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields and relations for collections", async () => {
|
||||
const results = await service.listAndCountCollections(
|
||||
{
|
||||
id: productCollectionOne.id,
|
||||
},
|
||||
{
|
||||
select: ["id", "title", "products.title"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "collection 1",
|
||||
products: [
|
||||
expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveCollection", () => {
|
||||
it("should return the requested collection", async () => {
|
||||
const result = await service.retrieveCollection(productCollectionOne.id)
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "collection 1",
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const result = await service.retrieveCollection(productCollectionOne.id, {
|
||||
select: ["id", "title", "products.title"],
|
||||
relations: ["products"],
|
||||
})
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "collection 1",
|
||||
products: [
|
||||
expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
],
|
||||
productTwo = testManager.create(Product, {
|
||||
id: "product-2",
|
||||
title: "product 2",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a collection with ID does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveCollection("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductCollection with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteCollections", () => {
|
||||
const collectionId = "test-1"
|
||||
|
||||
it("should delete the product collection given an ID successfully", async () => {
|
||||
await service.deleteCollections([collectionId])
|
||||
|
||||
const collections = await service.listCollections({
|
||||
id: collectionId,
|
||||
})
|
||||
|
||||
expect(collections).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should emit events through event bus", async () => {
|
||||
const eventBusSpy = jest.spyOn(EventBusService.prototype, "emit")
|
||||
await service.deleteCollections([collectionId])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
eventName: "product-collection.deleted",
|
||||
data: { id: collectionId },
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateCollections", () => {
|
||||
const collectionId = "test-1"
|
||||
|
||||
it("should emit events through event bus", async () => {
|
||||
const eventBusSpy = jest.spyOn(EventBusService.prototype, "emit")
|
||||
|
||||
await service.upsertCollections([
|
||||
{
|
||||
id: collectionId,
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
eventName: "product-collection.updated",
|
||||
data: { id: collectionId },
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should update the value of the collection successfully", async () => {
|
||||
await service.upsertCollections([
|
||||
{
|
||||
id: collectionId,
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
const productCollection = await service.retrieveCollection(collectionId)
|
||||
|
||||
expect(productCollection.title).toEqual("New Collection")
|
||||
})
|
||||
|
||||
it("should add products to a collection successfully", async () => {
|
||||
await service.upsertCollections([
|
||||
{
|
||||
id: collectionId,
|
||||
product_ids: [productOne.id, productTwo.id],
|
||||
},
|
||||
])
|
||||
|
||||
const productCollection = await service.retrieveCollection(collectionId, {
|
||||
select: ["products.id"],
|
||||
relations: ["products"],
|
||||
})
|
||||
|
||||
expect(productCollection.products).toHaveLength(2)
|
||||
expect(productCollection).toEqual(
|
||||
expect.objectContaining({
|
||||
products: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: productOne.id,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productTwo.id,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.upsertCollections([
|
||||
const productCollectionsData = [
|
||||
{
|
||||
id: "does-not-exist",
|
||||
title: "New Collection",
|
||||
id: "test-1",
|
||||
title: "collection 1",
|
||||
products: [productOne],
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
{
|
||||
id: "test-2",
|
||||
title: "collection",
|
||||
products: [productTwo],
|
||||
},
|
||||
]
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductCollection with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
productCollections = await createCollections(
|
||||
testManager,
|
||||
productCollectionsData
|
||||
)
|
||||
|
||||
describe("createCollections", () => {
|
||||
it("should create a collection successfully", async () => {
|
||||
const res = await service.createCollections([
|
||||
{
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
const [productCollection] = await service.listCollections({
|
||||
title: "New Collection",
|
||||
productCollectionOne = productCollections[0]
|
||||
productCollectionTwo = productCollections[1]
|
||||
})
|
||||
|
||||
expect(productCollection.title).toEqual("New Collection")
|
||||
})
|
||||
afterEach(async () => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("should create collection with products successfully", async () => {
|
||||
await service.createCollections([
|
||||
{
|
||||
title: "New Collection with products",
|
||||
handle: "new-collection-with-products",
|
||||
product_ids: [productOne.id, productTwo.id],
|
||||
},
|
||||
])
|
||||
describe("listCollections", () => {
|
||||
it("should return collections queried by ID", async () => {
|
||||
const results = await service.listCollections({
|
||||
id: productCollectionOne.id,
|
||||
})
|
||||
|
||||
const [productCollection] = await service.listCollections(
|
||||
{
|
||||
handle: "new-collection-with-products",
|
||||
},
|
||||
{
|
||||
select: ["title", "handle", "products.id"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productCollection).toEqual(
|
||||
expect.objectContaining({
|
||||
title: "New Collection with products",
|
||||
handle: "new-collection-with-products",
|
||||
products: [
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productOne.id,
|
||||
id: productCollectionOne.id,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productTwo.id,
|
||||
}),
|
||||
],
|
||||
])
|
||||
})
|
||||
)
|
||||
|
||||
it("should return collections based on the options and filter parameter", async () => {
|
||||
let results = await service.listCollections(
|
||||
{
|
||||
id: productCollectionOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCollectionOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
results = await service.listCollections({}, { take: 1, skip: 1 })
|
||||
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCollectionTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields and relations for collections", async () => {
|
||||
const results = await service.listCollections(
|
||||
{
|
||||
id: productCollectionOne.id,
|
||||
},
|
||||
{
|
||||
select: ["id", "title", "products.title"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "collection 1",
|
||||
products: [
|
||||
expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCountCollections", () => {
|
||||
it("should return collections and count queried by ID", async () => {
|
||||
const results = await service.listAndCountCollections({
|
||||
id: productCollectionOne.id,
|
||||
})
|
||||
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCollectionOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return collections and count based on the options and filter parameter", async () => {
|
||||
let results = await service.listAndCountCollections(
|
||||
{
|
||||
id: productCollectionOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCollectionOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
results = await service.listAndCountCollections({}, { take: 1 })
|
||||
|
||||
expect(results[1]).toEqual(2)
|
||||
|
||||
results = await service.listAndCountCollections(
|
||||
{},
|
||||
{ take: 1, skip: 1 }
|
||||
)
|
||||
|
||||
expect(results[1]).toEqual(2)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCollectionTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields and relations for collections", async () => {
|
||||
const results = await service.listAndCountCollections(
|
||||
{
|
||||
id: productCollectionOne.id,
|
||||
},
|
||||
{
|
||||
select: ["id", "title", "products.title"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "collection 1",
|
||||
products: [
|
||||
expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveCollection", () => {
|
||||
it("should return the requested collection", async () => {
|
||||
const result = await service.retrieveCollection(
|
||||
productCollectionOne.id
|
||||
)
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "collection 1",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const result = await service.retrieveCollection(
|
||||
productCollectionOne.id,
|
||||
{
|
||||
select: ["id", "title", "products.title"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "collection 1",
|
||||
products: [
|
||||
expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a collection with ID does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveCollection("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductCollection with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteCollections", () => {
|
||||
const collectionId = "test-1"
|
||||
|
||||
it("should delete the product collection given an ID successfully", async () => {
|
||||
await service.deleteCollections([collectionId])
|
||||
|
||||
const collections = await service.listCollections({
|
||||
id: collectionId,
|
||||
})
|
||||
|
||||
expect(collections).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should emit events through event bus", async () => {
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
await service.deleteCollections([collectionId])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
eventName: "product-collection.deleted",
|
||||
data: { id: collectionId },
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateCollections", () => {
|
||||
const collectionId = "test-1"
|
||||
|
||||
it("should emit events through event bus", async () => {
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
|
||||
await service.upsertCollections([
|
||||
{
|
||||
id: collectionId,
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
eventName: "product-collection.updated",
|
||||
data: { id: collectionId },
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should update the value of the collection successfully", async () => {
|
||||
await service.upsertCollections([
|
||||
{
|
||||
id: collectionId,
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
const productCollection = await service.retrieveCollection(
|
||||
collectionId
|
||||
)
|
||||
|
||||
expect(productCollection.title).toEqual("New Collection")
|
||||
})
|
||||
|
||||
it("should add products to a collection successfully", async () => {
|
||||
await service.upsertCollections([
|
||||
{
|
||||
id: collectionId,
|
||||
product_ids: [productOne.id, productTwo.id],
|
||||
},
|
||||
])
|
||||
|
||||
const productCollection = await service.retrieveCollection(
|
||||
collectionId,
|
||||
{
|
||||
select: ["products.id"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productCollection.products).toHaveLength(2)
|
||||
expect(productCollection).toEqual(
|
||||
expect.objectContaining({
|
||||
products: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: productOne.id,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productTwo.id,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.upsertCollections([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductCollection with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createCollections", () => {
|
||||
it("should create a collection successfully", async () => {
|
||||
const res = await service.createCollections([
|
||||
{
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
const [productCollection] = await service.listCollections({
|
||||
title: "New Collection",
|
||||
})
|
||||
|
||||
expect(productCollection.title).toEqual("New Collection")
|
||||
})
|
||||
|
||||
it("should create collection with products successfully", async () => {
|
||||
await service.createCollections([
|
||||
{
|
||||
title: "New Collection with products",
|
||||
handle: "new-collection-with-products",
|
||||
product_ids: [productOne.id, productTwo.id],
|
||||
},
|
||||
])
|
||||
|
||||
const [productCollection] = await service.listCollections(
|
||||
{
|
||||
handle: "new-collection-with-products",
|
||||
},
|
||||
{
|
||||
select: ["title", "handle", "products.id"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productCollection).toEqual(
|
||||
expect.objectContaining({
|
||||
title: "New Collection with products",
|
||||
handle: "new-collection-with-products",
|
||||
products: [
|
||||
expect.objectContaining({
|
||||
id: productOne.id,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productTwo.id,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should emit events through event bus", async () => {
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
|
||||
const collections = await service.createCollections([
|
||||
{
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
eventName: "product-collection.created",
|
||||
data: { id: collections[0].id },
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("should emit events through event bus", async () => {
|
||||
const eventBusSpy = jest.spyOn(EventBusService.prototype, "emit")
|
||||
|
||||
const collections = await service.createCollections([
|
||||
{
|
||||
title: "New Collection",
|
||||
},
|
||||
])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
eventName: "product-collection.created",
|
||||
data: { id: collections[0].id },
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,325 +1,307 @@
|
||||
import { IProductModuleService, ProductTypes } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { Product, ProductOption } from "@models"
|
||||
import { getInitModuleConfig, TestDatabase } from "../../../utils"
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
describe("ProductModuleService product options", () => {
|
||||
let service: IProductModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let optionOne: ProductOption
|
||||
let optionTwo: ProductOption
|
||||
let productOne: Product
|
||||
let productTwo: Product
|
||||
jest.setTimeout(30000)
|
||||
|
||||
let shutdownFunc: () => Promise<void>
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PRODUCT,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IProductModuleService>) => {
|
||||
describe("ProductModuleService product options", () => {
|
||||
let optionOne: ProductOption
|
||||
let optionTwo: ProductOption
|
||||
let productOne: Product
|
||||
let productTwo: Product
|
||||
|
||||
beforeAll(async () => {
|
||||
MedusaModule.clearInstances()
|
||||
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.PRODUCT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
|
||||
testManager = await TestDatabase.forkManager()
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
productTwo = testManager.create(Product, {
|
||||
id: "product-2",
|
||||
title: "product 2",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
optionOne = testManager.create(ProductOption, {
|
||||
id: "option-1",
|
||||
title: "option 1",
|
||||
product: productOne,
|
||||
})
|
||||
|
||||
optionTwo = testManager.create(ProductOption, {
|
||||
id: "option-2",
|
||||
title: "option 1",
|
||||
product: productTwo,
|
||||
})
|
||||
|
||||
await testManager.persistAndFlush([optionOne, optionTwo])
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await TestDatabase.clearDatabase()
|
||||
})
|
||||
|
||||
describe("listOptions", () => {
|
||||
it("should return options and count queried by ID", async () => {
|
||||
const options = await service.listOptions({
|
||||
id: optionOne.id,
|
||||
})
|
||||
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return options and count based on the options and filter parameter", async () => {
|
||||
let options = await service.listOptions(
|
||||
{
|
||||
id: optionOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
options = await service.listOptions({}, { take: 1, skip: 1 })
|
||||
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields and relations for options", async () => {
|
||||
const options = await service.listOptions(
|
||||
{
|
||||
id: optionOne.id,
|
||||
},
|
||||
{
|
||||
select: ["title", "product.id"],
|
||||
relations: ["product"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(options).toEqual([
|
||||
{
|
||||
id: optionOne.id,
|
||||
title: optionOne.title,
|
||||
product_id: productOne.id,
|
||||
product: {
|
||||
id: productOne.id,
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCountOptions", () => {
|
||||
it("should return options and count queried by ID", async () => {
|
||||
const [options, count] = await service.listAndCountOptions({
|
||||
id: optionOne.id,
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return options and count based on the options and filter parameter", async () => {
|
||||
let [options, count] = await service.listAndCountOptions(
|
||||
{
|
||||
id: optionOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
}),
|
||||
])
|
||||
;[options, count] = await service.listAndCountOptions({}, { take: 1 })
|
||||
|
||||
expect(count).toEqual(2)
|
||||
;[options, count] = await service.listAndCountOptions(
|
||||
{},
|
||||
{ take: 1, skip: 1 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields and relations for options", async () => {
|
||||
const [options, count] = await service.listAndCountOptions(
|
||||
{
|
||||
id: optionOne.id,
|
||||
},
|
||||
{
|
||||
select: ["title", "product.id"],
|
||||
relations: ["product"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(options).toEqual([
|
||||
{
|
||||
id: optionOne.id,
|
||||
title: optionOne.title,
|
||||
product_id: productOne.id,
|
||||
product: {
|
||||
id: productOne.id,
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveOption", () => {
|
||||
it("should return the requested option", async () => {
|
||||
const option = await service.retrieveOption(optionOne.id)
|
||||
|
||||
expect(option).toEqual(
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
beforeEach(async () => {
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const option = await service.retrieveOption(optionOne.id, {
|
||||
select: ["id", "product.title"],
|
||||
relations: ["product"],
|
||||
productTwo = testManager.create(Product, {
|
||||
id: "product-2",
|
||||
title: "product 2",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
optionOne = testManager.create(ProductOption, {
|
||||
id: "option-1",
|
||||
title: "option 1",
|
||||
product: productOne,
|
||||
})
|
||||
|
||||
optionTwo = testManager.create(ProductOption, {
|
||||
id: "option-2",
|
||||
title: "option 1",
|
||||
product: productTwo,
|
||||
})
|
||||
|
||||
await testManager.persistAndFlush([optionOne, optionTwo])
|
||||
})
|
||||
|
||||
expect(option).toEqual(
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
product: {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
},
|
||||
describe("listOptions", () => {
|
||||
it("should return options and count queried by ID", async () => {
|
||||
const options = await service.listOptions({
|
||||
id: optionOne.id,
|
||||
})
|
||||
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a option with ID does not exist", async () => {
|
||||
let error
|
||||
it("should return options and count based on the options and filter parameter", async () => {
|
||||
let options = await service.listOptions(
|
||||
{
|
||||
id: optionOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
try {
|
||||
await service.retrieveOption("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductOption with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
options = await service.listOptions({}, { take: 1, skip: 1 })
|
||||
|
||||
describe("deleteOptions", () => {
|
||||
const optionId = "option-1"
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should delete the product option given an ID successfully", async () => {
|
||||
await service.deleteOptions([optionId])
|
||||
it("should return only requested fields and relations for options", async () => {
|
||||
const options = await service.listOptions(
|
||||
{
|
||||
id: optionOne.id,
|
||||
},
|
||||
{
|
||||
select: ["title", "product.id"],
|
||||
relations: ["product"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
const options = await service.listOptions({
|
||||
id: optionId,
|
||||
expect(options).toEqual([
|
||||
{
|
||||
id: optionOne.id,
|
||||
title: optionOne.title,
|
||||
product_id: productOne.id,
|
||||
product: {
|
||||
id: productOne.id,
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
expect(options).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
describe("listAndCountOptions", () => {
|
||||
it("should return options and count queried by ID", async () => {
|
||||
const [options, count] = await service.listAndCountOptions({
|
||||
id: optionOne.id,
|
||||
})
|
||||
|
||||
describe("updateOptions", () => {
|
||||
const optionId = "option-1"
|
||||
|
||||
it("should update the title of the option successfully", async () => {
|
||||
await service.updateOptions([
|
||||
{
|
||||
id: optionId,
|
||||
title: "new test",
|
||||
},
|
||||
])
|
||||
|
||||
const productOption = await service.retrieveOption(optionId)
|
||||
|
||||
expect(productOption.title).toEqual("new test")
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.updateOptions([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductOption with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createOptions", () => {
|
||||
it("should create a option successfully", async () => {
|
||||
const res = await service.createOptions([
|
||||
{
|
||||
title: "test",
|
||||
product_id: productOne.id,
|
||||
},
|
||||
])
|
||||
|
||||
const [productOption] = await service.listOptions(
|
||||
{
|
||||
title: "test",
|
||||
},
|
||||
{
|
||||
select: ["id", "title", "product.id"],
|
||||
relations: ["product"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productOption).toEqual(
|
||||
expect.objectContaining({
|
||||
title: "test",
|
||||
product: expect.objectContaining({
|
||||
id: productOne.id,
|
||||
}),
|
||||
expect(count).toEqual(1)
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
|
||||
it("should return options and count based on the options and filter parameter", async () => {
|
||||
let [options, count] = await service.listAndCountOptions(
|
||||
{
|
||||
id: optionOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
}),
|
||||
])
|
||||
;[options, count] = await service.listAndCountOptions({}, { take: 1 })
|
||||
|
||||
expect(count).toEqual(2)
|
||||
;[options, count] = await service.listAndCountOptions(
|
||||
{},
|
||||
{ take: 1, skip: 1 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(options).toEqual([
|
||||
expect.objectContaining({
|
||||
id: optionTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields and relations for options", async () => {
|
||||
const [options, count] = await service.listAndCountOptions(
|
||||
{
|
||||
id: optionOne.id,
|
||||
},
|
||||
{
|
||||
select: ["title", "product.id"],
|
||||
relations: ["product"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(options).toEqual([
|
||||
{
|
||||
id: optionOne.id,
|
||||
title: optionOne.title,
|
||||
product_id: productOne.id,
|
||||
product: {
|
||||
id: productOne.id,
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveOption", () => {
|
||||
it("should return the requested option", async () => {
|
||||
const option = await service.retrieveOption(optionOne.id)
|
||||
|
||||
expect(option).toEqual(
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const option = await service.retrieveOption(optionOne.id, {
|
||||
select: ["id", "product.title"],
|
||||
relations: ["product"],
|
||||
})
|
||||
|
||||
expect(option).toEqual(
|
||||
expect.objectContaining({
|
||||
id: optionOne.id,
|
||||
product: {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a option with ID does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveOption("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductOption with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteOptions", () => {
|
||||
const optionId = "option-1"
|
||||
|
||||
it("should delete the product option given an ID successfully", async () => {
|
||||
await service.deleteOptions([optionId])
|
||||
|
||||
const options = await service.listOptions({
|
||||
id: optionId,
|
||||
})
|
||||
|
||||
expect(options).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateOptions", () => {
|
||||
const optionId = "option-1"
|
||||
|
||||
it("should update the title of the option successfully", async () => {
|
||||
await service.updateOptions([
|
||||
{
|
||||
id: optionId,
|
||||
title: "new test",
|
||||
},
|
||||
])
|
||||
|
||||
const productOption = await service.retrieveOption(optionId)
|
||||
|
||||
expect(productOption.title).toEqual("new test")
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.updateOptions([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductOption with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createOptions", () => {
|
||||
it("should create a option successfully", async () => {
|
||||
const res = await service.createOptions([
|
||||
{
|
||||
title: "test",
|
||||
product_id: productOne.id,
|
||||
},
|
||||
])
|
||||
|
||||
const [productOption] = await service.listOptions(
|
||||
{
|
||||
title: "test",
|
||||
},
|
||||
{
|
||||
select: ["id", "title", "product.id"],
|
||||
relations: ["product"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productOption).toEqual(
|
||||
expect.objectContaining({
|
||||
title: "test",
|
||||
product: expect.objectContaining({
|
||||
id: productOne.id,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,313 +1,298 @@
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService, ProductTypes } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { Product, ProductTag } from "@models"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { TestDatabase, getInitModuleConfig } from "../../../utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
describe("ProductModuleService product tags", () => {
|
||||
let service: IProductModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let tagOne: ProductTag
|
||||
let tagTwo: ProductTag
|
||||
let productOne: Product
|
||||
let productTwo: Product
|
||||
jest.setTimeout(30000)
|
||||
|
||||
let shutdownFunc: () => Promise<void>
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PRODUCT,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IProductModuleService>) => {
|
||||
describe("ProductModuleService product tags", () => {
|
||||
let tagOne: ProductTag
|
||||
let tagTwo: ProductTag
|
||||
let productOne: Product
|
||||
let productTwo: Product
|
||||
|
||||
beforeAll(async () => {
|
||||
MedusaModule.clearInstances()
|
||||
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.PRODUCT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
|
||||
testManager = await TestDatabase.forkManager()
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
productTwo = testManager.create(Product, {
|
||||
id: "product-2",
|
||||
title: "product 2",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
tagOne = testManager.create(ProductTag, {
|
||||
id: "tag-1",
|
||||
value: "tag 1",
|
||||
products: [productOne],
|
||||
})
|
||||
|
||||
tagTwo = testManager.create(ProductTag, {
|
||||
id: "tag-2",
|
||||
value: "tag",
|
||||
products: [productTwo],
|
||||
})
|
||||
|
||||
await testManager.persistAndFlush([tagOne, tagTwo])
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await TestDatabase.clearDatabase()
|
||||
})
|
||||
|
||||
describe("listTags", () => {
|
||||
it("should return tags and count queried by ID", async () => {
|
||||
const tags = await service.listTags({
|
||||
id: tagOne.id,
|
||||
})
|
||||
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
id: tagOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return tags and count based on the options and filter parameter", async () => {
|
||||
let tags = await service.listTags(
|
||||
{
|
||||
id: tagOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
id: tagOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
tags = await service.listTags({}, { take: 1, skip: 1 })
|
||||
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
id: tagTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields and relations for tags", async () => {
|
||||
const tags = await service.listTags(
|
||||
{
|
||||
id: tagOne.id,
|
||||
},
|
||||
{
|
||||
select: ["value", "products.id"],
|
||||
relations: ["products"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(tags).toEqual([
|
||||
{
|
||||
id: tagOne.id,
|
||||
value: tagOne.value,
|
||||
products: [
|
||||
{
|
||||
id: productOne.id,
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCountTags", () => {
|
||||
it("should return tags and count queried by ID", async () => {
|
||||
const [tags, count] = await service.listAndCountTags({
|
||||
id: tagOne.id,
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
id: tagOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return tags and count based on the options and filter parameter", async () => {
|
||||
let [tags, count] = await service.listAndCountTags(
|
||||
{
|
||||
id: tagOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
id: tagOne.id,
|
||||
}),
|
||||
])
|
||||
;[tags, count] = await service.listAndCountTags({}, { take: 1 })
|
||||
|
||||
expect(count).toEqual(2)
|
||||
;[tags, count] = await service.listAndCountTags({}, { take: 1, skip: 1 })
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
id: tagTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields and relations for tags", async () => {
|
||||
const [tags, count] = await service.listAndCountTags(
|
||||
{
|
||||
id: tagOne.id,
|
||||
},
|
||||
{
|
||||
select: ["value", "products.id"],
|
||||
relations: ["products"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(tags).toEqual([
|
||||
{
|
||||
id: tagOne.id,
|
||||
value: tagOne.value,
|
||||
products: [
|
||||
{
|
||||
id: productOne.id,
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveTag", () => {
|
||||
it("should return the requested tag", async () => {
|
||||
const tag = await service.retrieveTag(tagOne.id)
|
||||
|
||||
expect(tag).toEqual(
|
||||
expect.objectContaining({
|
||||
id: tagOne.id,
|
||||
beforeEach(async () => {
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const tag = await service.retrieveTag(tagOne.id, {
|
||||
select: ["id", "value", "products.title"],
|
||||
relations: ["products"],
|
||||
productTwo = testManager.create(Product, {
|
||||
id: "product-2",
|
||||
title: "product 2",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
tagOne = testManager.create(ProductTag, {
|
||||
id: "tag-1",
|
||||
value: "tag 1",
|
||||
products: [productOne],
|
||||
})
|
||||
|
||||
tagTwo = testManager.create(ProductTag, {
|
||||
id: "tag-2",
|
||||
value: "tag",
|
||||
products: [productTwo],
|
||||
})
|
||||
|
||||
await testManager.persistAndFlush([tagOne, tagTwo])
|
||||
})
|
||||
|
||||
expect(tag).toEqual(
|
||||
expect.objectContaining({
|
||||
id: tagOne.id,
|
||||
value: tagOne.value,
|
||||
products: [
|
||||
describe("listTags", () => {
|
||||
it("should return tags and count queried by ID", async () => {
|
||||
const tags = await service.listTags({
|
||||
id: tagOne.id,
|
||||
})
|
||||
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
title: "product 1",
|
||||
id: tagOne.id,
|
||||
}),
|
||||
],
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a tag with ID does not exist", async () => {
|
||||
let error
|
||||
it("should return tags and count based on the options and filter parameter", async () => {
|
||||
let tags = await service.listTags(
|
||||
{
|
||||
id: tagOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
try {
|
||||
await service.retrieveTag("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
id: tagOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductTag with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
tags = await service.listTags({}, { take: 1, skip: 1 })
|
||||
|
||||
describe("deleteTags", () => {
|
||||
const tagId = "tag-1"
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
id: tagTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should delete the product tag given an ID successfully", async () => {
|
||||
await service.deleteTags([tagId])
|
||||
it("should return only requested fields and relations for tags", async () => {
|
||||
const tags = await service.listTags(
|
||||
{
|
||||
id: tagOne.id,
|
||||
},
|
||||
{
|
||||
select: ["value", "products.id"],
|
||||
relations: ["products"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
const tags = await service.listTags({
|
||||
id: tagId,
|
||||
expect(tags).toEqual([
|
||||
{
|
||||
id: tagOne.id,
|
||||
value: tagOne.value,
|
||||
products: [
|
||||
{
|
||||
id: productOne.id,
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
expect(tags).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
describe("listAndCountTags", () => {
|
||||
it("should return tags and count queried by ID", async () => {
|
||||
const [tags, count] = await service.listAndCountTags({
|
||||
id: tagOne.id,
|
||||
})
|
||||
|
||||
describe("updateTags", () => {
|
||||
const tagId = "tag-1"
|
||||
expect(count).toEqual(1)
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
id: tagOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should update the value of the tag successfully", async () => {
|
||||
await service.updateTags([
|
||||
{
|
||||
id: tagId,
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
it("should return tags and count based on the options and filter parameter", async () => {
|
||||
let [tags, count] = await service.listAndCountTags(
|
||||
{
|
||||
id: tagOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
const productTag = await service.retrieveTag(tagId)
|
||||
expect(count).toEqual(1)
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
id: tagOne.id,
|
||||
}),
|
||||
])
|
||||
;[tags, count] = await service.listAndCountTags({}, { take: 1 })
|
||||
|
||||
expect(productTag.value).toEqual("UK")
|
||||
})
|
||||
expect(count).toEqual(2)
|
||||
;[tags, count] = await service.listAndCountTags(
|
||||
{},
|
||||
{ take: 1, skip: 1 }
|
||||
)
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
expect(count).toEqual(2)
|
||||
expect(tags).toEqual([
|
||||
expect.objectContaining({
|
||||
id: tagTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
try {
|
||||
await service.updateTags([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
it("should return only requested fields and relations for tags", async () => {
|
||||
const [tags, count] = await service.listAndCountTags(
|
||||
{
|
||||
id: tagOne.id,
|
||||
},
|
||||
{
|
||||
select: ["value", "products.id"],
|
||||
relations: ["products"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(tags).toEqual([
|
||||
{
|
||||
id: tagOne.id,
|
||||
value: tagOne.value,
|
||||
products: [
|
||||
{
|
||||
id: productOne.id,
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveTag", () => {
|
||||
it("should return the requested tag", async () => {
|
||||
const tag = await service.retrieveTag(tagOne.id)
|
||||
|
||||
expect(tag).toEqual(
|
||||
expect.objectContaining({
|
||||
id: tagOne.id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const tag = await service.retrieveTag(tagOne.id, {
|
||||
select: ["id", "value", "products.title"],
|
||||
relations: ["products"],
|
||||
})
|
||||
|
||||
expect(tag).toEqual(
|
||||
expect.objectContaining({
|
||||
id: tagOne.id,
|
||||
value: tagOne.value,
|
||||
products: [
|
||||
expect.objectContaining({
|
||||
title: "product 1",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a tag with ID does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveTag("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductTag with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteTags", () => {
|
||||
const tagId = "tag-1"
|
||||
|
||||
it("should delete the product tag given an ID successfully", async () => {
|
||||
await service.deleteTags([tagId])
|
||||
|
||||
const tags = await service.listTags({
|
||||
id: tagId,
|
||||
})
|
||||
|
||||
expect(tags).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateTags", () => {
|
||||
const tagId = "tag-1"
|
||||
|
||||
it("should update the value of the tag successfully", async () => {
|
||||
await service.updateTags([
|
||||
{
|
||||
id: tagId,
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productTag = await service.retrieveTag(tagId)
|
||||
|
||||
expect(productTag.value).toEqual("UK")
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.updateTags([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductTag with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createTags", () => {
|
||||
it("should create a tag successfully", async () => {
|
||||
const res = await service.createTags([
|
||||
{
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productTag = await service.listTags({
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
})
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductTag with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createTags", () => {
|
||||
it("should create a tag successfully", async () => {
|
||||
const res = await service.createTags([
|
||||
{
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productTag = await service.listTags({
|
||||
value: "UK",
|
||||
expect(productTag[0]?.value).toEqual("UK")
|
||||
})
|
||||
})
|
||||
|
||||
expect(productTag[0]?.value).toEqual("UK")
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,281 +1,263 @@
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { ProductType } from "@models"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { getInitModuleConfig, TestDatabase } from "../../../utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
describe("ProductModuleService product types", () => {
|
||||
let service: IProductModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let typeOne: ProductType
|
||||
let typeTwo: ProductType
|
||||
jest.setTimeout(30000)
|
||||
|
||||
let shutdownFunc: () => Promise<void>
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PRODUCT,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IProductModuleService>) => {
|
||||
describe("ProductModuleService product types", () => {
|
||||
let typeOne: ProductType
|
||||
let typeTwo: ProductType
|
||||
|
||||
beforeAll(async () => {
|
||||
MedusaModule.clearInstances()
|
||||
beforeEach(async () => {
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.PRODUCT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
typeOne = testManager.create(ProductType, {
|
||||
id: "type-1",
|
||||
value: "type 1",
|
||||
})
|
||||
|
||||
typeTwo = testManager.create(ProductType, {
|
||||
id: "type-2",
|
||||
value: "type",
|
||||
})
|
||||
|
||||
await testManager.persistAndFlush([typeOne, typeTwo])
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await TestDatabase.clearDatabase()
|
||||
})
|
||||
|
||||
describe("listTypes", () => {
|
||||
it("should return types and count queried by ID", async () => {
|
||||
const types = await service.listTypes({
|
||||
id: typeOne.id,
|
||||
})
|
||||
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return types and count based on the options and filter parameter", async () => {
|
||||
let types = await service.listTypes(
|
||||
{
|
||||
id: typeOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
types = await service.listTypes({}, { take: 1, skip: 1 })
|
||||
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields for types", async () => {
|
||||
const types = await service.listTypes(
|
||||
{
|
||||
id: typeOne.id,
|
||||
},
|
||||
{
|
||||
select: ["value"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(types).toEqual([
|
||||
{
|
||||
id: typeOne.id,
|
||||
value: typeOne.value,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCountTypes", () => {
|
||||
it("should return types and count queried by ID", async () => {
|
||||
const [types, count] = await service.listAndCountTypes({
|
||||
id: typeOne.id,
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return types and count based on the options and filter parameter", async () => {
|
||||
let [types, count] = await service.listAndCountTypes(
|
||||
{
|
||||
id: typeOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeOne.id,
|
||||
}),
|
||||
])
|
||||
;[types, count] = await service.listAndCountTypes({}, { take: 1 })
|
||||
|
||||
expect(count).toEqual(2)
|
||||
;[types, count] = await service.listAndCountTypes(
|
||||
{},
|
||||
{ take: 1, skip: 1 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields for types", async () => {
|
||||
const [types, count] = await service.listAndCountTypes(
|
||||
{
|
||||
id: typeOne.id,
|
||||
},
|
||||
{
|
||||
select: ["value"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(types).toEqual([
|
||||
{
|
||||
id: typeOne.id,
|
||||
value: typeOne.value,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveType", () => {
|
||||
it("should return the requested type", async () => {
|
||||
const type = await service.retrieveType(typeOne.id)
|
||||
|
||||
expect(type).toEqual(
|
||||
expect.objectContaining({
|
||||
id: typeOne.id,
|
||||
typeOne = testManager.create(ProductType, {
|
||||
id: "type-1",
|
||||
value: "type 1",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const type = await service.retrieveType(typeOne.id, {
|
||||
select: ["id", "value"],
|
||||
typeTwo = testManager.create(ProductType, {
|
||||
id: "type-2",
|
||||
value: "type",
|
||||
})
|
||||
|
||||
await testManager.persistAndFlush([typeOne, typeTwo])
|
||||
})
|
||||
|
||||
expect(type).toEqual({
|
||||
id: typeOne.id,
|
||||
value: typeOne.value,
|
||||
})
|
||||
})
|
||||
describe("listTypes", () => {
|
||||
it("should return types and count queried by ID", async () => {
|
||||
const types = await service.listTypes({
|
||||
id: typeOne.id,
|
||||
})
|
||||
|
||||
it("should throw an error when a type with ID does not exist", async () => {
|
||||
let error
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
try {
|
||||
await service.retrieveType("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
it("should return types and count based on the options and filter parameter", async () => {
|
||||
let types = await service.listTypes(
|
||||
{
|
||||
id: typeOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductType with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
describe("deleteTypes", () => {
|
||||
const typeId = "type-1"
|
||||
types = await service.listTypes({}, { take: 1, skip: 1 })
|
||||
|
||||
it("should delete the product type given an ID successfully", async () => {
|
||||
await service.deleteTypes([typeId])
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
const types = await service.listTypes({
|
||||
id: typeId,
|
||||
it("should return only requested fields for types", async () => {
|
||||
const types = await service.listTypes(
|
||||
{
|
||||
id: typeOne.id,
|
||||
},
|
||||
{
|
||||
select: ["value"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(types).toEqual([
|
||||
{
|
||||
id: typeOne.id,
|
||||
value: typeOne.value,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
expect(types).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
describe("listAndCountTypes", () => {
|
||||
it("should return types and count queried by ID", async () => {
|
||||
const [types, count] = await service.listAndCountTypes({
|
||||
id: typeOne.id,
|
||||
})
|
||||
|
||||
describe("updateTypes", () => {
|
||||
const typeId = "type-1"
|
||||
expect(count).toEqual(1)
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should update the value of the type successfully", async () => {
|
||||
await service.updateTypes([
|
||||
{
|
||||
id: typeId,
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
it("should return types and count based on the options and filter parameter", async () => {
|
||||
let [types, count] = await service.listAndCountTypes(
|
||||
{
|
||||
id: typeOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
const productType = await service.retrieveType(typeId)
|
||||
expect(count).toEqual(1)
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeOne.id,
|
||||
}),
|
||||
])
|
||||
;[types, count] = await service.listAndCountTypes({}, { take: 1 })
|
||||
|
||||
expect(productType.value).toEqual("UK")
|
||||
})
|
||||
expect(count).toEqual(2)
|
||||
;[types, count] = await service.listAndCountTypes(
|
||||
{},
|
||||
{ take: 1, skip: 1 }
|
||||
)
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
expect(count).toEqual(2)
|
||||
expect(types).toEqual([
|
||||
expect.objectContaining({
|
||||
id: typeTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
try {
|
||||
await service.updateTypes([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
it("should return only requested fields for types", async () => {
|
||||
const [types, count] = await service.listAndCountTypes(
|
||||
{
|
||||
id: typeOne.id,
|
||||
},
|
||||
{
|
||||
select: ["value"],
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(types).toEqual([
|
||||
{
|
||||
id: typeOne.id,
|
||||
value: typeOne.value,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveType", () => {
|
||||
it("should return the requested type", async () => {
|
||||
const type = await service.retrieveType(typeOne.id)
|
||||
|
||||
expect(type).toEqual(
|
||||
expect.objectContaining({
|
||||
id: typeOne.id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const type = await service.retrieveType(typeOne.id, {
|
||||
select: ["id", "value"],
|
||||
})
|
||||
|
||||
expect(type).toEqual({
|
||||
id: typeOne.id,
|
||||
value: typeOne.value,
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw an error when a type with ID does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveType("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductType with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteTypes", () => {
|
||||
const typeId = "type-1"
|
||||
|
||||
it("should delete the product type given an ID successfully", async () => {
|
||||
await service.deleteTypes([typeId])
|
||||
|
||||
const types = await service.listTypes({
|
||||
id: typeId,
|
||||
})
|
||||
|
||||
expect(types).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateTypes", () => {
|
||||
const typeId = "type-1"
|
||||
|
||||
it("should update the value of the type successfully", async () => {
|
||||
await service.updateTypes([
|
||||
{
|
||||
id: typeId,
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productType = await service.retrieveType(typeId)
|
||||
|
||||
expect(productType.value).toEqual("UK")
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.updateTypes([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductType with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createTypes", () => {
|
||||
it("should create a type successfully", async () => {
|
||||
const res = await service.createTypes([
|
||||
{
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productType = await service.listTypes({
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
})
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductType with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createTypes", () => {
|
||||
it("should create a type successfully", async () => {
|
||||
const res = await service.createTypes([
|
||||
{
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productType = await service.listTypes({
|
||||
value: "UK",
|
||||
expect(productType[0]?.value).toEqual("UK")
|
||||
})
|
||||
})
|
||||
|
||||
expect(productType[0]?.value).toEqual("UK")
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,186 +1,170 @@
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService, ProductTypes } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { Product, ProductVariant } from "@models"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { TestDatabase, getInitModuleConfig } from "../../../utils"
|
||||
|
||||
describe("ProductModuleService product variants", () => {
|
||||
let service: IProductModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let variantOne: ProductVariant
|
||||
let variantTwo: ProductVariant
|
||||
let productOne: Product
|
||||
let productTwo: Product
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
let shutdownFunc: () => Promise<void>
|
||||
jest.setTimeout(30000)
|
||||
|
||||
beforeAll(async () => {
|
||||
MedusaModule.clearInstances()
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PRODUCT,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IProductModuleService>) => {
|
||||
describe("ProductModuleService product variants", () => {
|
||||
let variantOne: ProductVariant
|
||||
let variantTwo: ProductVariant
|
||||
let productOne: Product
|
||||
let productTwo: Product
|
||||
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
beforeEach(async () => {
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
service = medusaApp.modules[Modules.PRODUCT]
|
||||
productTwo = testManager.create(Product, {
|
||||
id: "product-2",
|
||||
title: "product 2",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
variantOne = testManager.create(ProductVariant, {
|
||||
id: "test-1",
|
||||
title: "variant 1",
|
||||
inventory_quantity: 10,
|
||||
product: productOne,
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
variantTwo = testManager.create(ProductVariant, {
|
||||
id: "test-2",
|
||||
title: "variant",
|
||||
inventory_quantity: 10,
|
||||
product: productTwo,
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
productTwo = testManager.create(Product, {
|
||||
id: "product-2",
|
||||
title: "product 2",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
variantOne = testManager.create(ProductVariant, {
|
||||
id: "test-1",
|
||||
title: "variant 1",
|
||||
inventory_quantity: 10,
|
||||
product: productOne,
|
||||
})
|
||||
|
||||
variantTwo = testManager.create(ProductVariant, {
|
||||
id: "test-2",
|
||||
title: "variant",
|
||||
inventory_quantity: 10,
|
||||
product: productTwo,
|
||||
})
|
||||
|
||||
await testManager.persistAndFlush([variantOne, variantTwo])
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await TestDatabase.clearDatabase()
|
||||
})
|
||||
|
||||
describe("listAndCountVariants", () => {
|
||||
it("should return variants and count queried by ID", async () => {
|
||||
const results = await service.listAndCountVariants({
|
||||
id: variantOne.id,
|
||||
await testManager.persistAndFlush([variantOne, variantTwo])
|
||||
})
|
||||
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: variantOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
describe("listAndCountVariants", () => {
|
||||
it("should return variants and count queried by ID", async () => {
|
||||
const results = await service.listAndCountVariants({
|
||||
id: variantOne.id,
|
||||
})
|
||||
|
||||
it("should return variants and count based on the options and filter parameter", async () => {
|
||||
let results = await service.listAndCountVariants(
|
||||
{
|
||||
id: variantOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: variantOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
results = await service.listAndCountVariants({}, { take: 1 })
|
||||
|
||||
expect(results[1]).toEqual(2)
|
||||
|
||||
results = await service.listAndCountVariants({}, { take: 1, skip: 1 })
|
||||
|
||||
expect(results[1]).toEqual(2)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: variantTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields and relations for variants", async () => {
|
||||
const results = await service.listAndCountVariants(
|
||||
{
|
||||
id: variantOne.id,
|
||||
},
|
||||
{
|
||||
select: ["id", "title", "product.title"] as any,
|
||||
relations: ["product"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "variant 1",
|
||||
// TODO: investigate why this is returning more than the expected results
|
||||
product: expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveVariant", () => {
|
||||
it("should return the requested variant", async () => {
|
||||
const result = await service.retrieveVariant(variantOne.id)
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "variant 1",
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: variantOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const result = await service.retrieveVariant(variantOne.id, {
|
||||
select: ["id", "title", "product.title"] as any,
|
||||
relations: ["product"],
|
||||
it("should return variants and count based on the options and filter parameter", async () => {
|
||||
let results = await service.listAndCountVariants(
|
||||
{
|
||||
id: variantOne.id,
|
||||
},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: variantOne.id,
|
||||
}),
|
||||
])
|
||||
|
||||
results = await service.listAndCountVariants({}, { take: 1 })
|
||||
|
||||
expect(results[1]).toEqual(2)
|
||||
|
||||
results = await service.listAndCountVariants({}, { take: 1, skip: 1 })
|
||||
|
||||
expect(results[1]).toEqual(2)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: variantTwo.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return only requested fields and relations for variants", async () => {
|
||||
const results = await service.listAndCountVariants(
|
||||
{
|
||||
id: variantOne.id,
|
||||
},
|
||||
{
|
||||
select: ["id", "title", "product.title"] as any,
|
||||
relations: ["product"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(results[1]).toEqual(1)
|
||||
expect(results[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "variant 1",
|
||||
// TODO: investigate why this is returning more than the expected results
|
||||
product: expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "variant 1",
|
||||
product: expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
describe("retrieveVariant", () => {
|
||||
it("should return the requested variant", async () => {
|
||||
const result = await service.retrieveVariant(variantOne.id)
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "variant 1",
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const result = await service.retrieveVariant(variantOne.id, {
|
||||
select: ["id", "title", "product.title"] as any,
|
||||
relations: ["product"],
|
||||
})
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-1",
|
||||
title: "variant 1",
|
||||
product: expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a variant with ID does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveVariant("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductVariant with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw an error when a variant with ID does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveVariant("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductVariant with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,376 +1,370 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { ProductOptionService } from "@services"
|
||||
import { Product } from "@models"
|
||||
|
||||
import { TestDatabase } from "../../../utils"
|
||||
import { createOptions } from "../../../__fixtures__/product"
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { asValue } from "awilix"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("ProductOption Service", () => {
|
||||
let service: ProductOptionService
|
||||
let testManager: SqlEntityManager
|
||||
let repositoryManager: SqlEntityManager
|
||||
let productOne: Product
|
||||
let productTwo: Product
|
||||
let data!: Product[]
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PRODUCT,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
medusaApp,
|
||||
}: SuiteOptions<IProductModuleService>) => {
|
||||
describe("ProductOption Service", () => {
|
||||
let service: ProductOptionService
|
||||
|
||||
const productOneData = {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
}
|
||||
|
||||
const productTwoData = {
|
||||
id: "product-2",
|
||||
title: "product 2",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productOptionService")
|
||||
|
||||
testManager = await TestDatabase.forkManager()
|
||||
productOne = testManager.create(Product, productOneData)
|
||||
productTwo = testManager.create(Product, productTwoData)
|
||||
|
||||
data = await createOptions(testManager, [
|
||||
{
|
||||
id: "option-1",
|
||||
title: "Option 1",
|
||||
product: productOne,
|
||||
},
|
||||
{
|
||||
id: "option-2",
|
||||
title: "Option 2",
|
||||
product: productOne,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await TestDatabase.clearDatabase()
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
it("list product option", async () => {
|
||||
const optionResults = await service.list()
|
||||
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-1",
|
||||
title: "Option 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "option-2",
|
||||
title: "Option 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product option by id", async () => {
|
||||
const optionResults = await service.list({ id: "option-2" })
|
||||
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-2",
|
||||
title: "Option 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product option by title matching string", async () => {
|
||||
const optionResults = await service.list({ title: "Option 1" })
|
||||
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-1",
|
||||
title: "Option 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCount", () => {
|
||||
it("should return product option and count", async () => {
|
||||
const [optionResults, count] = await service.listAndCount()
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-1",
|
||||
title: "Option 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "option-2",
|
||||
title: "Option 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return product option and count when filtered", async () => {
|
||||
const [optionResults, count] = await service.listAndCount({
|
||||
id: "option-2",
|
||||
beforeEach(() => {
|
||||
service = medusaApp.modules["productService"].productOptionService_
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
let productOne: Product
|
||||
let productTwo: Product
|
||||
|
||||
it("should return product option and count when using skip and take", async () => {
|
||||
const [optionResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 1, take: 1 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return requested fields", async () => {
|
||||
const [optionResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["title"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(optionResults))
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const optionId = "option-1"
|
||||
const optionValue = "Option 1"
|
||||
|
||||
it("should return option for the given id", async () => {
|
||||
const option = await service.retrieve(optionId)
|
||||
|
||||
expect(option).toEqual(
|
||||
expect.objectContaining({
|
||||
id: optionId,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when option with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
const productOneData = {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductOption 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
|
||||
const productTwoData = {
|
||||
id: "product-2",
|
||||
title: "product 2",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("productOption - id must be defined")
|
||||
})
|
||||
beforeEach(async () => {
|
||||
const testManager = MikroOrmWrapper.forkManager()
|
||||
productOne = testManager.create(Product, productOneData)
|
||||
productTwo = testManager.create(Product, productTwoData)
|
||||
|
||||
it("should return option based on config select param", async () => {
|
||||
const option = await service.retrieve(optionId, {
|
||||
select: ["id", "title"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(option))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: optionId,
|
||||
title: optionValue,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
const optionId = "option-1"
|
||||
|
||||
it("should delete the product option given an ID successfully", async () => {
|
||||
await service.delete([optionId])
|
||||
|
||||
const options = await service.list({
|
||||
id: optionId,
|
||||
})
|
||||
|
||||
expect(options).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
const optionId = "option-1"
|
||||
|
||||
it("should update the title of the option successfully", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id: optionId,
|
||||
title: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productOption = await service.retrieve(optionId)
|
||||
|
||||
expect(productOption.title).toEqual("UK")
|
||||
})
|
||||
|
||||
it("should update the relationship of the option successfully", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id: optionId,
|
||||
product_id: productTwo.id,
|
||||
},
|
||||
])
|
||||
|
||||
const productOption = await service.retrieve(optionId, {
|
||||
relations: ["product"],
|
||||
})
|
||||
|
||||
expect(productOption).toEqual(
|
||||
expect.objectContaining({
|
||||
id: optionId,
|
||||
product: expect.objectContaining({
|
||||
id: productTwo.id,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
await createOptions(testManager, [
|
||||
{
|
||||
id: "does-not-exist",
|
||||
title: "UK",
|
||||
id: "option-1",
|
||||
title: "Option 1",
|
||||
product: productOne,
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductOption with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a option successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
title: "UK",
|
||||
product: productOne,
|
||||
},
|
||||
])
|
||||
|
||||
const [productOption] = await service.list(
|
||||
{
|
||||
title: "UK",
|
||||
},
|
||||
{
|
||||
relations: ["product"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productOption).toEqual(
|
||||
expect.objectContaining({
|
||||
title: "UK",
|
||||
product: expect.objectContaining({
|
||||
id: productOne.id,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("upsert", function () {
|
||||
it("should create an option and update another option successfully", async () => {
|
||||
const productOption = (
|
||||
await service.create([
|
||||
{
|
||||
title: "UK",
|
||||
id: "option-2",
|
||||
title: "Option 2",
|
||||
product: productOne,
|
||||
},
|
||||
])
|
||||
)[0]
|
||||
})
|
||||
|
||||
const optionToUpdate = {
|
||||
id: productOption.id,
|
||||
title: "US",
|
||||
}
|
||||
describe("list", () => {
|
||||
it("list product option", async () => {
|
||||
const optionResults = await service.list()
|
||||
|
||||
const newOption = {
|
||||
title: "US2",
|
||||
product_id: productOne.id,
|
||||
}
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-1",
|
||||
title: "Option 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "option-2",
|
||||
title: "Option 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
await service.upsert([optionToUpdate, newOption])
|
||||
it("list product option by id", async () => {
|
||||
const optionResults = await service.list({ id: "option-2" })
|
||||
|
||||
const productOptions = await service.list(
|
||||
{
|
||||
title: "US%",
|
||||
},
|
||||
{
|
||||
relations: ["product"],
|
||||
}
|
||||
)
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-2",
|
||||
title: "Option 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
expect(JSON.parse(JSON.stringify(productOptions))).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
it("list product option by title matching string", async () => {
|
||||
const optionResults = await service.list({ title: "Option 1" })
|
||||
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-1",
|
||||
title: "Option 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCount", () => {
|
||||
it("should return product option and count", async () => {
|
||||
const [optionResults, count] = await service.listAndCount()
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-1",
|
||||
title: "Option 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "option-2",
|
||||
title: "Option 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return product option and count when filtered", async () => {
|
||||
const [optionResults, count] = await service.listAndCount({
|
||||
id: "option-2",
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return product option and count when using skip and take", async () => {
|
||||
const [optionResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 1, take: 1 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(optionResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return requested fields", async () => {
|
||||
const [optionResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["title"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(optionResults))
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "option-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const optionId = "option-1"
|
||||
const optionValue = "Option 1"
|
||||
|
||||
it("should return option for the given id", async () => {
|
||||
const option = await service.retrieve(optionId)
|
||||
|
||||
expect(option).toEqual(
|
||||
expect.objectContaining({
|
||||
id: optionId,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when option with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductOption 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("productOption - id must be defined")
|
||||
})
|
||||
|
||||
it("should return option based on config select param", async () => {
|
||||
const option = await service.retrieve(optionId, {
|
||||
select: ["id", "title"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(option))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: optionId,
|
||||
title: optionValue,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
const optionId = "option-1"
|
||||
|
||||
it("should delete the product option given an ID successfully", async () => {
|
||||
await service.delete([optionId])
|
||||
|
||||
const options = await service.list({
|
||||
id: optionId,
|
||||
})
|
||||
|
||||
expect(options).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
const optionId = "option-1"
|
||||
|
||||
it("should update the title of the option successfully", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id: optionId,
|
||||
title: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productOption = await service.retrieve(optionId)
|
||||
|
||||
expect(productOption.title).toEqual("UK")
|
||||
})
|
||||
|
||||
it("should update the relationship of the option successfully", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id: optionId,
|
||||
product_id: productTwo.id,
|
||||
},
|
||||
])
|
||||
|
||||
const productOption = await service.retrieve(optionId, {
|
||||
relations: ["product"],
|
||||
})
|
||||
|
||||
expect(productOption).toEqual(
|
||||
expect.objectContaining({
|
||||
id: optionId,
|
||||
product: expect.objectContaining({
|
||||
id: productTwo.id,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
title: "UK",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductOption with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a option successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
title: "UK",
|
||||
product: productOne,
|
||||
},
|
||||
])
|
||||
|
||||
const [productOption] = await service.list(
|
||||
{
|
||||
title: "UK",
|
||||
},
|
||||
{
|
||||
relations: ["product"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(productOption).toEqual(
|
||||
expect.objectContaining({
|
||||
title: "UK",
|
||||
product: expect.objectContaining({
|
||||
id: productOne.id,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("upsert", function () {
|
||||
it("should create an option and update another option successfully", async () => {
|
||||
const productOption = (
|
||||
await service.create([
|
||||
{
|
||||
title: "UK",
|
||||
product: productOne,
|
||||
},
|
||||
])
|
||||
)[0]
|
||||
|
||||
const optionToUpdate = {
|
||||
id: productOption.id,
|
||||
title: "US",
|
||||
product: expect.objectContaining({
|
||||
id: productOne.id,
|
||||
}),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
title: newOption.title,
|
||||
product: expect.objectContaining({
|
||||
id: productOne.id,
|
||||
}),
|
||||
}),
|
||||
])
|
||||
)
|
||||
}
|
||||
|
||||
const newOption = {
|
||||
title: "US2",
|
||||
product_id: productOne.id,
|
||||
}
|
||||
|
||||
await service.upsert([optionToUpdate, newOption])
|
||||
|
||||
const productOptions = await service.list(
|
||||
{
|
||||
title: "US%",
|
||||
},
|
||||
{
|
||||
relations: ["product"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(JSON.parse(JSON.stringify(productOptions))).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
title: "US",
|
||||
product: expect.objectContaining({
|
||||
id: productOne.id,
|
||||
}),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
title: newOption.title,
|
||||
product: expect.objectContaining({
|
||||
id: productOne.id,
|
||||
}),
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,341 +1,335 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { Product } from "@models"
|
||||
import { ProductTagService } from "@services"
|
||||
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
import { createProductAndTags } from "../../../__fixtures__/product"
|
||||
import { TestDatabase } from "../../../utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("ProductTag Service", () => {
|
||||
let service: ProductTagService
|
||||
let testManager: SqlEntityManager
|
||||
let repositoryManager: SqlEntityManager
|
||||
let data!: Product[]
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PRODUCT,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
medusaApp,
|
||||
}: SuiteOptions<IProductModuleService>) => {
|
||||
describe("ProductTag Service", () => {
|
||||
let data!: Product[]
|
||||
let service: ProductTagService
|
||||
|
||||
const productsData = [
|
||||
{
|
||||
id: "test-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
tags: [
|
||||
{
|
||||
id: "tag-1",
|
||||
value: "France",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "test-2",
|
||||
title: "product",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
tags: [
|
||||
{
|
||||
id: "tag-2",
|
||||
value: "Germany",
|
||||
},
|
||||
{
|
||||
id: "tag-3",
|
||||
value: "United States",
|
||||
},
|
||||
{
|
||||
id: "tag-4",
|
||||
value: "United Kingdom",
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productTagService")
|
||||
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
data = await createProductAndTags(testManager, productsData)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await TestDatabase.clearDatabase()
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
it("list product tags", async () => {
|
||||
const tagsResults = await service.list()
|
||||
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-1",
|
||||
value: "France",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-2",
|
||||
value: "Germany",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-3",
|
||||
value: "United States",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-4",
|
||||
value: "United Kingdom",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product tags by id", async () => {
|
||||
const tagsResults = await service.list({ id: data[0].tags![0].id })
|
||||
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-1",
|
||||
value: "France",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product tags by value matching string", async () => {
|
||||
const tagsResults = await service.list({ value: "united kingdom" })
|
||||
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-4",
|
||||
value: "United Kingdom",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCount", () => {
|
||||
it("should return product tags and count", async () => {
|
||||
const [tagsResults, count] = await service.listAndCount()
|
||||
|
||||
expect(count).toEqual(4)
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-1",
|
||||
value: "France",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-2",
|
||||
value: "Germany",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-3",
|
||||
value: "United States",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-4",
|
||||
value: "United Kingdom",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return product tags and count when filtered", async () => {
|
||||
const [tagsResults, count] = await service.listAndCount({
|
||||
id: data[0].tags![0].id,
|
||||
beforeEach(() => {
|
||||
service = medusaApp.modules["productService"].productTagService_
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return product tags and count when using skip and take", async () => {
|
||||
const [tagsResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 1, take: 2 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(4)
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-3",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return requested fields and relations", async () => {
|
||||
const [tagsResults, count] = await service.listAndCount(
|
||||
{},
|
||||
const productsData = [
|
||||
{
|
||||
take: 1,
|
||||
select: ["value", "products.id"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(tagsResults))
|
||||
|
||||
expect(count).toEqual(4)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-1",
|
||||
products: [
|
||||
id: "test-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
tags: [
|
||||
{
|
||||
id: "test-1",
|
||||
id: "tag-1",
|
||||
value: "France",
|
||||
},
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
},
|
||||
{
|
||||
id: "test-2",
|
||||
title: "product",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
tags: [
|
||||
{
|
||||
id: "tag-2",
|
||||
value: "Germany",
|
||||
},
|
||||
{
|
||||
id: "tag-3",
|
||||
value: "United States",
|
||||
},
|
||||
{
|
||||
id: "tag-4",
|
||||
value: "United Kingdom",
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
describe("retrieve", () => {
|
||||
const tagId = "tag-1"
|
||||
const tagValue = "France"
|
||||
const productId = "test-1"
|
||||
beforeEach(async () => {
|
||||
data = await createProductAndTags(
|
||||
MikroOrmWrapper.forkManager(),
|
||||
productsData
|
||||
)
|
||||
})
|
||||
|
||||
it("should return tag for the given id", async () => {
|
||||
const tag = await service.retrieve(tagId)
|
||||
describe("list", () => {
|
||||
it("list product tags", async () => {
|
||||
const tagsResults = await service.list()
|
||||
|
||||
expect(tag).toEqual(
|
||||
expect.objectContaining({
|
||||
id: tagId,
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-1",
|
||||
value: "France",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-2",
|
||||
value: "Germany",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-3",
|
||||
value: "United States",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-4",
|
||||
value: "United Kingdom",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when tag with id does not exist", async () => {
|
||||
let error
|
||||
it("list product tags by id", async () => {
|
||||
const tagsResults = await service.list({ id: data[0].tags![0].id })
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-1",
|
||||
value: "France",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductTag with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
it("list product tags by value matching string", async () => {
|
||||
const tagsResults = await service.list({ value: "united kingdom" })
|
||||
|
||||
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("productTag - id must be defined")
|
||||
})
|
||||
|
||||
it("should return tag based on config select param", async () => {
|
||||
const tag = await service.retrieve(tagId, {
|
||||
select: ["id", "value"],
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-4",
|
||||
value: "United Kingdom",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(tag))
|
||||
describe("listAndCount", () => {
|
||||
it("should return product tags and count", async () => {
|
||||
const [tagsResults, count] = await service.listAndCount()
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: tagId,
|
||||
value: tagValue,
|
||||
})
|
||||
})
|
||||
expect(count).toEqual(4)
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-1",
|
||||
value: "France",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-2",
|
||||
value: "Germany",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-3",
|
||||
value: "United States",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-4",
|
||||
value: "United Kingdom",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return tag based on config relation param", async () => {
|
||||
const tag = await service.retrieve(tagId, {
|
||||
select: ["id", "value", "products.id"],
|
||||
relations: ["products"],
|
||||
it("should return product tags and count when filtered", async () => {
|
||||
const [tagsResults, count] = await service.listAndCount({
|
||||
id: data[0].tags![0].id,
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return product tags and count when using skip and take", async () => {
|
||||
const [tagsResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 1, take: 2 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(4)
|
||||
expect(tagsResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "tag-3",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return requested fields and relations", async () => {
|
||||
const [tagsResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["value", "products.id"],
|
||||
relations: ["products"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(tagsResults))
|
||||
|
||||
expect(count).toEqual(4)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "tag-1",
|
||||
products: [
|
||||
{
|
||||
id: "test-1",
|
||||
},
|
||||
],
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(tag))
|
||||
describe("retrieve", () => {
|
||||
const tagId = "tag-1"
|
||||
const tagValue = "France"
|
||||
const productId = "test-1"
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: tagId,
|
||||
value: tagValue,
|
||||
products: [
|
||||
expect.objectContaining({
|
||||
id: productId,
|
||||
}),
|
||||
],
|
||||
})
|
||||
})
|
||||
})
|
||||
it("should return tag for the given id", async () => {
|
||||
const tag = await service.retrieve(tagId)
|
||||
|
||||
describe("delete", () => {
|
||||
const tagId = "tag-1"
|
||||
expect(tag).toEqual(
|
||||
expect.objectContaining({
|
||||
id: tagId,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should delete the product tag given an ID successfully", async () => {
|
||||
await service.delete([tagId])
|
||||
it("should throw an error when tag with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
const tags = await service.list({
|
||||
id: tagId,
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductTag 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("productTag - id must be defined")
|
||||
})
|
||||
|
||||
it("should return tag based on config select param", async () => {
|
||||
const tag = await service.retrieve(tagId, {
|
||||
select: ["id", "value"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(tag))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: tagId,
|
||||
value: tagValue,
|
||||
})
|
||||
})
|
||||
|
||||
it("should return tag based on config relation param", async () => {
|
||||
const tag = await service.retrieve(tagId, {
|
||||
select: ["id", "value", "products.id"],
|
||||
relations: ["products"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(tag))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: tagId,
|
||||
value: tagValue,
|
||||
products: [
|
||||
expect.objectContaining({
|
||||
id: productId,
|
||||
}),
|
||||
],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
expect(tags).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
describe("delete", () => {
|
||||
const tagId = "tag-1"
|
||||
|
||||
describe("update", () => {
|
||||
const tagId = "tag-1"
|
||||
it("should delete the product tag given an ID successfully", async () => {
|
||||
await service.delete([tagId])
|
||||
|
||||
it("should update the value of the tag successfully", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id: tagId,
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
const tags = await service.list({
|
||||
id: tagId,
|
||||
})
|
||||
|
||||
const productTag = await service.retrieve(tagId)
|
||||
expect(tags).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
expect(productTag.value).toEqual("UK")
|
||||
})
|
||||
describe("update", () => {
|
||||
const tagId = "tag-1"
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
it("should update the value of the tag successfully", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id: tagId,
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
const productTag = await service.retrieve(tagId)
|
||||
|
||||
expect(productTag.value).toEqual("UK")
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductTag with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a tag successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const [productTag] = await service.list({
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
})
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductTag with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a tag successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const [productTag] = await service.list({
|
||||
value: "UK",
|
||||
expect(productTag.value).toEqual("UK")
|
||||
})
|
||||
})
|
||||
|
||||
expect(productTag.value).toEqual("UK")
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,284 +1,277 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
import { ProductTypeService } from "@services"
|
||||
import { Product } from "@models"
|
||||
|
||||
import { TestDatabase } from "../../../utils"
|
||||
import { createProductAndTypes } from "../../../__fixtures__/product"
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("ProductType Service", () => {
|
||||
let service: ProductTypeService
|
||||
let testManager: SqlEntityManager
|
||||
let repositoryManager: SqlEntityManager
|
||||
let data!: Product[]
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PRODUCT,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
medusaApp,
|
||||
}: SuiteOptions<IProductModuleService>) => {
|
||||
describe("ProductType Service", () => {
|
||||
let data!: Product[]
|
||||
let service: ProductTypeService
|
||||
|
||||
const productsData = [
|
||||
{
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
type: {
|
||||
id: "type-1",
|
||||
value: "Type 1",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "product-2",
|
||||
title: "product",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
type: {
|
||||
id: "type-2",
|
||||
value: "Type 2",
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productTypeService")
|
||||
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
data = await createProductAndTypes(testManager, productsData)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await TestDatabase.clearDatabase()
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
it("list product type", async () => {
|
||||
const typeResults = await service.list()
|
||||
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
value: "Type 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "type-2",
|
||||
value: "Type 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product type by id", async () => {
|
||||
const typeResults = await service.list({ id: data[0].type.id })
|
||||
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
value: "Type 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product type by value matching string", async () => {
|
||||
const typeResults = await service.list({ value: "Type 1" })
|
||||
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
value: "Type 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCount", () => {
|
||||
it("should return product type and count", async () => {
|
||||
const [typeResults, count] = await service.listAndCount()
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
value: "Type 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "type-2",
|
||||
value: "Type 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return product type and count when filtered", async () => {
|
||||
const [typeResults, count] = await service.listAndCount({
|
||||
id: data[0].type.id,
|
||||
beforeEach(() => {
|
||||
service = medusaApp.modules["productService"].productTypeService_
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return product type and count when using skip and take", async () => {
|
||||
const [typeResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 1, take: 1 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return requested fields", async () => {
|
||||
const [typeResults, count] = await service.listAndCount(
|
||||
{},
|
||||
const productsData = [
|
||||
{
|
||||
take: 1,
|
||||
select: ["value"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(typeResults))
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const typeId = "type-1"
|
||||
const typeValue = "Type 1"
|
||||
|
||||
it("should return type for the given id", async () => {
|
||||
const type = await service.retrieve(typeId)
|
||||
|
||||
expect(type).toEqual(
|
||||
expect.objectContaining({
|
||||
id: typeId,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when type with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductType 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("productType - id must be defined")
|
||||
})
|
||||
|
||||
it("should return type based on config select param", async () => {
|
||||
const type = await service.retrieve(typeId, {
|
||||
select: ["id", "value"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(type))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: typeId,
|
||||
value: typeValue,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
const typeId = "type-1"
|
||||
|
||||
it("should delete the product type given an ID successfully", async () => {
|
||||
await service.delete([typeId])
|
||||
|
||||
const types = await service.list({
|
||||
id: typeId,
|
||||
})
|
||||
|
||||
expect(types).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
const typeId = "type-1"
|
||||
|
||||
it("should update the value of the type successfully", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id: typeId,
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productType = await service.retrieve(typeId)
|
||||
|
||||
expect(productType.value).toEqual("UK")
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
value: "UK",
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
type: {
|
||||
id: "type-1",
|
||||
value: "Type 1",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductType with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a type successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
{
|
||||
id: "product-2",
|
||||
title: "product",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
type: {
|
||||
id: "type-2",
|
||||
value: "Type 2",
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
const [productType] = await service.list({
|
||||
value: "UK",
|
||||
beforeEach(async () => {
|
||||
data = await createProductAndTypes(
|
||||
MikroOrmWrapper.forkManager(),
|
||||
productsData
|
||||
)
|
||||
})
|
||||
describe("list", () => {
|
||||
it("list product type", async () => {
|
||||
const typeResults = await service.list()
|
||||
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
value: "Type 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "type-2",
|
||||
value: "Type 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product type by id", async () => {
|
||||
const typeResults = await service.list({ id: data[0].type.id })
|
||||
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
value: "Type 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("list product type by value matching string", async () => {
|
||||
const typeResults = await service.list({ value: "Type 1" })
|
||||
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
value: "Type 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
expect(productType.value).toEqual("UK")
|
||||
describe("listAndCount", () => {
|
||||
it("should return product type and count", async () => {
|
||||
const [typeResults, count] = await service.listAndCount()
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
value: "Type 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "type-2",
|
||||
value: "Type 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return product type and count when filtered", async () => {
|
||||
const [typeResults, count] = await service.listAndCount({
|
||||
id: data[0].type.id,
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return product type and count when using skip and take", async () => {
|
||||
const [typeResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 1, take: 1 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(typeResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return requested fields", async () => {
|
||||
const [typeResults, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["value"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(typeResults))
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "type-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const typeId = "type-1"
|
||||
const typeValue = "Type 1"
|
||||
|
||||
it("should return type for the given id", async () => {
|
||||
const type = await service.retrieve(typeId)
|
||||
|
||||
expect(type).toEqual(
|
||||
expect.objectContaining({
|
||||
id: typeId,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when type with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductType 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("productType - id must be defined")
|
||||
})
|
||||
|
||||
it("should return type based on config select param", async () => {
|
||||
const type = await service.retrieve(typeId, {
|
||||
select: ["id", "value"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(type))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id: typeId,
|
||||
value: typeValue,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
const typeId = "type-1"
|
||||
|
||||
it("should delete the product type given an ID successfully", async () => {
|
||||
await service.delete([typeId])
|
||||
|
||||
const types = await service.list({
|
||||
id: typeId,
|
||||
})
|
||||
|
||||
expect(types).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
const typeId = "type-1"
|
||||
|
||||
it("should update the value of the type successfully", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id: typeId,
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const productType = await service.retrieve(typeId)
|
||||
|
||||
expect(productType.value).toEqual("UK")
|
||||
})
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductType with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a type successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
|
||||
const [productType] = await service.list({
|
||||
value: "UK",
|
||||
})
|
||||
|
||||
expect(productType.value).toEqual("UK")
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
import { ProductOption } from "@medusajs/client-types"
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { Collection } from "@mikro-orm/core"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { Product, ProductTag, ProductVariant } from "@models"
|
||||
import { ProductVariantService } from "@services"
|
||||
import { asValue } from "awilix"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
import {
|
||||
createOptions,
|
||||
createProductAndTags,
|
||||
@@ -14,313 +10,319 @@ import {
|
||||
} from "../../../__fixtures__/product"
|
||||
import { productsData, variantsData } from "../../../__fixtures__/product/data"
|
||||
import { buildProductVariantOnlyData } from "../../../__fixtures__/variant/data/create-variant"
|
||||
import { TestDatabase } from "../../../utils"
|
||||
|
||||
// TODO: fix tests
|
||||
describe.skip("ProductVariant Service", () => {
|
||||
let service: ProductVariantService
|
||||
let testManager: SqlEntityManager
|
||||
let repositoryManager: SqlEntityManager
|
||||
let variantOne: ProductVariant
|
||||
let variantTwo: ProductVariant
|
||||
let productOne: Product
|
||||
const productVariantTestOne = "test-1"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestDatabase.setupDatabase()
|
||||
repositoryManager = await TestDatabase.forkManager()
|
||||
jest.setTimeout(30000)
|
||||
|
||||
const container = createMedusaContainer()
|
||||
container.register("manager", asValue(repositoryManager))
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PRODUCT,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
medusaApp,
|
||||
}: SuiteOptions<IProductModuleService>) => {
|
||||
describe.skip("ProductVariant Service", () => {
|
||||
let variantOne: ProductVariant
|
||||
let variantTwo: ProductVariant
|
||||
let productOne: Product
|
||||
const productVariantTestOne = "test-1"
|
||||
let service: ProductVariantService
|
||||
|
||||
await ContainerLoader({ container })
|
||||
|
||||
service = container.resolve("productVariantService")
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await TestDatabase.clearDatabase()
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
beforeEach(async () => {
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
beforeEach(() => {
|
||||
service = medusaApp.modules["productService"].productVariantService_
|
||||
})
|
||||
|
||||
variantOne = testManager.create(ProductVariant, {
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
inventory_quantity: 10,
|
||||
product: productOne,
|
||||
})
|
||||
describe("list", () => {
|
||||
beforeEach(async () => {
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
variantTwo = testManager.create(ProductVariant, {
|
||||
id: "test-2",
|
||||
title: "variant",
|
||||
inventory_quantity: 10,
|
||||
product: productOne,
|
||||
})
|
||||
|
||||
await testManager.persistAndFlush([variantOne, variantTwo])
|
||||
})
|
||||
|
||||
it("selecting by properties, scopes out the results", async () => {
|
||||
const results = await service.list({
|
||||
id: variantOne.id,
|
||||
})
|
||||
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: variantOne.id,
|
||||
title: "variant 1",
|
||||
inventory_quantity: "10",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("passing a limit, scopes the result to the limit", async () => {
|
||||
const results = await service.list(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: variantOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("passing populate, scopes the results of the response", async () => {
|
||||
const results = await service.list(
|
||||
{
|
||||
id: productVariantTestOne,
|
||||
},
|
||||
{
|
||||
select: ["id", "title", "product.title"] as any,
|
||||
relations: ["product"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
product: expect.objectContaining({
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
tags: expect.any(Collection<ProductTag>),
|
||||
variants: expect.any(Collection<ProductVariant>),
|
||||
}),
|
||||
}),
|
||||
])
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
expect(JSON.parse(JSON.stringify(results))).toEqual([
|
||||
{
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
product: {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
variantOne = testManager.create(ProductVariant, {
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
inventory_quantity: 10,
|
||||
product: productOne,
|
||||
})
|
||||
|
||||
describe("relation: options", () => {
|
||||
let products: Product[]
|
||||
let variants: ProductVariant[]
|
||||
let options: ProductOption[]
|
||||
variantTwo = testManager.create(ProductVariant, {
|
||||
id: "test-2",
|
||||
title: "variant",
|
||||
inventory_quantity: 10,
|
||||
product: productOne,
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
testManager = await TestDatabase.forkManager()
|
||||
await testManager.persistAndFlush([variantOne, variantTwo])
|
||||
})
|
||||
|
||||
products = (await createProductAndTags(
|
||||
testManager,
|
||||
productsData
|
||||
)) as Product[]
|
||||
variants = (await createProductVariants(
|
||||
testManager,
|
||||
variantsData
|
||||
)) as ProductVariant[]
|
||||
it("selecting by properties, scopes out the results", async () => {
|
||||
const results = await service.list({
|
||||
id: variantOne.id,
|
||||
})
|
||||
|
||||
options = await createOptions(testManager, [
|
||||
{
|
||||
id: "test-option-1",
|
||||
title: "size",
|
||||
product: products[0],
|
||||
values: [
|
||||
{ id: "value-1", value: "XS", variant: products[0].variants[0] },
|
||||
{ id: "value-1", value: "XL", variant: products[0].variants[0] },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "test-option-2",
|
||||
title: "color",
|
||||
product: products[0],
|
||||
value: "blue",
|
||||
variant: products[0].variants[0],
|
||||
},
|
||||
])
|
||||
})
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: variantOne.id,
|
||||
title: "variant 1",
|
||||
inventory_quantity: "10",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("filter by options relation", async () => {
|
||||
const variants = await service.list(
|
||||
{ options: { id: ["value-1"] } },
|
||||
{ relations: ["options"] }
|
||||
)
|
||||
it("passing a limit, scopes the result to the limit", async () => {
|
||||
const results = await service.list(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
}
|
||||
)
|
||||
|
||||
expect(JSON.parse(JSON.stringify(variants))).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productVariantTestOne,
|
||||
title: "variant title",
|
||||
sku: "sku 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: variantOne.id,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
describe("create", function () {
|
||||
let products: Product[]
|
||||
let productOptions!: ProductOption[]
|
||||
it("passing populate, scopes the results of the response", async () => {
|
||||
const results = await service.list(
|
||||
{
|
||||
id: productVariantTestOne,
|
||||
},
|
||||
{
|
||||
select: ["id", "title", "product.title"] as any,
|
||||
relations: ["product"],
|
||||
}
|
||||
)
|
||||
|
||||
beforeEach(async () => {
|
||||
testManager = await TestDatabase.forkManager()
|
||||
expect(results).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
product: expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
tags: expect.any(Collection<ProductTag>),
|
||||
variants: expect.any(Collection<ProductVariant>),
|
||||
}),
|
||||
}),
|
||||
])
|
||||
|
||||
products = (await createProductAndTags(
|
||||
testManager,
|
||||
productsData
|
||||
)) as Product[]
|
||||
|
||||
productOptions = await createOptions(testManager, [
|
||||
{
|
||||
id: "test-option-1",
|
||||
title: "size",
|
||||
product: products[0],
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should create a variant", async () => {
|
||||
const data = buildProductVariantOnlyData({
|
||||
options: [
|
||||
{
|
||||
option: productOptions[0],
|
||||
value: "XS",
|
||||
},
|
||||
],
|
||||
expect(JSON.parse(JSON.stringify(results))).toEqual([
|
||||
{
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
product: {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
const variants = await service.create(products[0].id, [data])
|
||||
describe("relation: options", () => {
|
||||
let products: Product[]
|
||||
let variants: ProductVariant[]
|
||||
let options: ProductOption[]
|
||||
|
||||
expect(variants).toHaveLength(1)
|
||||
expect(variants[0].options).toHaveLength(1)
|
||||
beforeEach(async () => {
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
expect(JSON.parse(JSON.stringify(variants[0]))).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
title: data.title,
|
||||
sku: data.sku,
|
||||
inventory_quantity: 100,
|
||||
allow_backorder: false,
|
||||
manage_inventory: true,
|
||||
variant_rank: 0,
|
||||
product: expect.objectContaining({
|
||||
id: products[0].id,
|
||||
}),
|
||||
options: expect.arrayContaining([
|
||||
products = (await createProductAndTags(
|
||||
testManager,
|
||||
productsData
|
||||
)) as Product[]
|
||||
variants = (await createProductVariants(
|
||||
testManager,
|
||||
variantsData
|
||||
)) as ProductVariant[]
|
||||
|
||||
options = await createOptions(testManager, [
|
||||
{
|
||||
id: "test-option-1",
|
||||
title: "size",
|
||||
product: products[0],
|
||||
values: [
|
||||
{
|
||||
id: "value-1",
|
||||
value: "XS",
|
||||
variant: products[0].variants[0],
|
||||
},
|
||||
{
|
||||
id: "value-1",
|
||||
value: "XL",
|
||||
variant: products[0].variants[0],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "test-option-2",
|
||||
title: "color",
|
||||
product: products[0],
|
||||
value: "blue",
|
||||
variant: products[0].variants[0],
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("filter by options relation", async () => {
|
||||
const variants = await service.list(
|
||||
{ options: { id: ["value-1"] } },
|
||||
{ relations: ["options"] }
|
||||
)
|
||||
|
||||
expect(JSON.parse(JSON.stringify(variants))).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productVariantTestOne,
|
||||
title: "variant title",
|
||||
sku: "sku 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", function () {
|
||||
let products: Product[]
|
||||
let productOptions!: ProductOption[]
|
||||
|
||||
beforeEach(async () => {
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
products = (await createProductAndTags(
|
||||
testManager,
|
||||
productsData
|
||||
)) as Product[]
|
||||
|
||||
productOptions = await createOptions(testManager, [
|
||||
{
|
||||
id: "test-option-1",
|
||||
title: "size",
|
||||
product: products[0],
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should create a variant", async () => {
|
||||
const data = buildProductVariantOnlyData({
|
||||
options: [
|
||||
{
|
||||
option: productOptions[0],
|
||||
value: "XS",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const variants = await service.create(products[0].id, [data])
|
||||
|
||||
expect(variants).toHaveLength(1)
|
||||
expect(variants[0].options).toHaveLength(1)
|
||||
|
||||
expect(JSON.parse(JSON.stringify(variants[0]))).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: data.options![0].value,
|
||||
}),
|
||||
]),
|
||||
title: data.title,
|
||||
sku: data.sku,
|
||||
inventory_quantity: 100,
|
||||
allow_backorder: false,
|
||||
manage_inventory: true,
|
||||
variant_rank: 0,
|
||||
product: expect.objectContaining({
|
||||
id: products[0].id,
|
||||
}),
|
||||
options: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: data.options![0].value,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
beforeEach(async () => {
|
||||
testManager = await TestDatabase.forkManager()
|
||||
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
variantOne = testManager.create(ProductVariant, {
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
inventory_quantity: 10,
|
||||
product: productOne,
|
||||
})
|
||||
describe("retrieve", () => {
|
||||
beforeEach(async () => {
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
await testManager.persistAndFlush([variantOne])
|
||||
})
|
||||
|
||||
it("should return the requested variant", async () => {
|
||||
const result = await service.retrieve(variantOne.id)
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const result = await service.retrieve(variantOne.id, {
|
||||
select: ["id", "title", "product.title"] as any,
|
||||
relations: ["product"],
|
||||
})
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
product_id: "product-1",
|
||||
product: expect.objectContaining({
|
||||
productOne = testManager.create(Product, {
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
variantOne = testManager.create(ProductVariant, {
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
inventory_quantity: 10,
|
||||
product: productOne,
|
||||
})
|
||||
|
||||
await testManager.persistAndFlush([variantOne])
|
||||
})
|
||||
)
|
||||
|
||||
it("should return the requested variant", async () => {
|
||||
const result = await service.retrieve(variantOne.id)
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return requested attributes when requested through config", async () => {
|
||||
const result = await service.retrieve(variantOne.id, {
|
||||
select: ["id", "title", "product.title"] as any,
|
||||
relations: ["product"],
|
||||
})
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.objectContaining({
|
||||
id: productVariantTestOne,
|
||||
title: "variant 1",
|
||||
product_id: "product-1",
|
||||
product: expect.objectContaining({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a variant with ID does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductVariant 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("productVariant - id must be defined")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw an error when a variant with ID does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"ProductVariant 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("productVariant - id must be defined")
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_PRODUCT_DB_SCHEMA = "medusa-product"
|
||||
@@ -1,22 +0,0 @@
|
||||
const { dropDatabase } = require("pg-god")
|
||||
|
||||
const DB_HOST = process.env.DB_HOST ?? "localhost"
|
||||
const DB_USERNAME = process.env.DB_USERNAME ?? "postgres"
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD ?? ""
|
||||
const DB_NAME = process.env.DB_TEMP_NAME
|
||||
|
||||
const pgGodCredentials = {
|
||||
user: DB_USERNAME,
|
||||
password: DB_PASSWORD,
|
||||
host: DB_HOST,
|
||||
}
|
||||
|
||||
afterAll(async () => {
|
||||
try {
|
||||
await dropDatabase({ databaseName: DB_NAME }, pgGodCredentials)
|
||||
} catch (e) {
|
||||
console.error(
|
||||
`This might fail if it is run during the unit tests since there is no database to drop. Otherwise, please check what is the issue. ${e.message}`
|
||||
)
|
||||
}
|
||||
})
|
||||
@@ -1,6 +0,0 @@
|
||||
import { ModuleServiceInitializeOptions } from "@medusajs/types"
|
||||
|
||||
export const databaseOptions: ModuleServiceInitializeOptions["database"] = {
|
||||
schema: "public",
|
||||
clientUrl: "medusa-products-test",
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
import { TSMigrationGenerator } from "@mikro-orm/migrations"
|
||||
import { MikroORM, Options, SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import * as ProductModels from "@models"
|
||||
import * as process from "process"
|
||||
|
||||
const DB_HOST = process.env.DB_HOST ?? "localhost"
|
||||
const DB_USERNAME = process.env.DB_USERNAME ?? ""
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD
|
||||
const DB_NAME = process.env.DB_TEMP_NAME
|
||||
export const DB_URL = `postgres://${DB_USERNAME}${
|
||||
DB_PASSWORD ? `:${DB_PASSWORD}` : ""
|
||||
}@${DB_HOST}/${DB_NAME}`
|
||||
|
||||
const ORMConfig: Options = {
|
||||
type: "postgresql",
|
||||
clientUrl: DB_URL,
|
||||
entities: Object.values(ProductModels),
|
||||
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
|
||||
debug: false,
|
||||
migrations: {
|
||||
path: "../../src/migrations",
|
||||
pathTs: "../../src/migrations",
|
||||
glob: "!(*.d).{js,ts}",
|
||||
silent: true,
|
||||
dropTables: true,
|
||||
transactional: true,
|
||||
allOrNothing: true,
|
||||
safe: false,
|
||||
generator: TSMigrationGenerator,
|
||||
},
|
||||
}
|
||||
|
||||
interface TestDatabase {
|
||||
orm: MikroORM | null
|
||||
manager: SqlEntityManager | null
|
||||
|
||||
setupDatabase(): Promise<void>
|
||||
clearDatabase(): Promise<void>
|
||||
getManager(): SqlEntityManager
|
||||
forkManager(): Promise<SqlEntityManager>
|
||||
getORM(): MikroORM
|
||||
}
|
||||
|
||||
export const TestDatabase: TestDatabase = {
|
||||
orm: null,
|
||||
manager: null,
|
||||
|
||||
getManager() {
|
||||
if (this.manager === null) {
|
||||
throw new Error("manager entity not available")
|
||||
}
|
||||
|
||||
return this.manager
|
||||
},
|
||||
|
||||
async forkManager() {
|
||||
if (this.manager === null) {
|
||||
throw new Error("manager entity not available")
|
||||
}
|
||||
|
||||
return await this.manager.fork()
|
||||
},
|
||||
|
||||
getORM() {
|
||||
if (this.orm === null) {
|
||||
throw new Error("orm entity not available")
|
||||
}
|
||||
|
||||
return this.orm
|
||||
},
|
||||
|
||||
async setupDatabase() {
|
||||
// Initializing the ORM
|
||||
this.orm = await MikroORM.init(ORMConfig)
|
||||
|
||||
if (this.orm === null) {
|
||||
throw new Error("ORM not configured")
|
||||
}
|
||||
|
||||
this.manager = await this.orm.em
|
||||
|
||||
await this.orm.schema.refreshDatabase() // ensure db exists and is fresh
|
||||
},
|
||||
|
||||
async clearDatabase() {
|
||||
if (this.orm === null) {
|
||||
throw new Error("ORM not configured")
|
||||
}
|
||||
|
||||
await this.orm.close()
|
||||
|
||||
this.orm = null
|
||||
this.manager = null
|
||||
},
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
|
||||
|
||||
import { DB_URL } from "./database"
|
||||
import { EventBusService } from "../__fixtures__/event-bus"
|
||||
|
||||
export function getInitModuleConfig(
|
||||
additionalOptions: any = {},
|
||||
injectedDependencies: null | Record<string, any> = null
|
||||
) {
|
||||
const moduleOptions = {
|
||||
defaultAdapterOptions: {
|
||||
database: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
|
||||
},
|
||||
},
|
||||
...additionalOptions,
|
||||
}
|
||||
|
||||
const modulesConfig_ = {
|
||||
[Modules.PRODUCT]: {
|
||||
definition: ModulesDefinition[Modules.PRODUCT],
|
||||
options: moduleOptions,
|
||||
},
|
||||
}
|
||||
|
||||
const defaultInjectedDependencies = {
|
||||
eventBusModuleService: new EventBusService(),
|
||||
}
|
||||
|
||||
return {
|
||||
injectedDependencies: injectedDependencies ?? defaultInjectedDependencies,
|
||||
modulesConfig: modulesConfig_,
|
||||
databaseConfig: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA,
|
||||
},
|
||||
joinerConfig: [],
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from "./database"
|
||||
export * from "./get-init-module-config"
|
||||
@@ -17,6 +17,4 @@ module.exports = {
|
||||
testEnvironment: `node`,
|
||||
moduleFileExtensions: [`js`, `ts`],
|
||||
modulePathIgnorePatterns: ["dist/"],
|
||||
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
|
||||
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
|
||||
}
|
||||
|
||||
@@ -693,7 +693,6 @@ export default class ProductModuleService<
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<ProductTypes.ProductDTO[] | ProductTypes.ProductDTO> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const products = await this.create_(input, sharedContext)
|
||||
|
||||
const createdProducts = await this.baseRepository_.serialize<
|
||||
|
||||
@@ -1,464 +1,444 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { createCampaigns } from "../../../__fixtures__/campaigns"
|
||||
import { createPromotions } from "../../../__fixtures__/promotion"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Promotion Module Service: Campaigns", () => {
|
||||
let service: IPromotionModuleService
|
||||
let repositoryManager: SqlEntityManager
|
||||
let shutdownFunc: () => void
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.PROMOTION]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
repositoryManager = MikroOrmWrapper.forkManager()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("listAndCountCampaigns", () => {
|
||||
beforeEach(async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
})
|
||||
|
||||
it("should return all campaigns and its count", async () => {
|
||||
const [campaigns, count] = await service.listAndCountCampaigns()
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(campaigns).toEqual([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: expect.any(Date),
|
||||
ends_at: expect.any(Date),
|
||||
budget: expect.any(Object),
|
||||
created_at: expect.any(Date),
|
||||
updated_at: expect.any(Date),
|
||||
deleted_at: null,
|
||||
},
|
||||
{
|
||||
id: "campaign-id-2",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-2",
|
||||
starts_at: expect.any(Date),
|
||||
ends_at: expect.any(Date),
|
||||
budget: expect.any(Object),
|
||||
created_at: expect.any(Date),
|
||||
updated_at: expect.any(Date),
|
||||
deleted_at: null,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should return all campaigns based on config select and relations param", async () => {
|
||||
const [campaigns, count] = await service.listAndCountCampaigns(
|
||||
{
|
||||
id: ["campaign-id-1"],
|
||||
},
|
||||
{
|
||||
relations: ["budget"],
|
||||
select: ["name", "budget.limit"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(campaigns).toEqual([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
budget: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
campaign: expect.any(Object),
|
||||
limit: 1000,
|
||||
}),
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("createCampaigns", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toContain(
|
||||
"Value for Campaign.campaign_identifier is required, 'undefined' found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a basic campaign successfully", async () => {
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign(createdCampaign.id)
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PROMOTION,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPromotionModuleService>) => {
|
||||
describe("Promotion Module Service: Campaigns", () => {
|
||||
describe("listAndCountCampaigns", () => {
|
||||
beforeEach(async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a campaign with campaign budget successfully", async () => {
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
it("should return all campaigns and its count", async () => {
|
||||
const [campaigns, count] = await service.listAndCountCampaigns()
|
||||
|
||||
const [createdPromotion] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
budget: {
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
used: 10,
|
||||
},
|
||||
},
|
||||
])
|
||||
expect(count).toEqual(2)
|
||||
expect(campaigns).toEqual([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: expect.any(Date),
|
||||
ends_at: expect.any(Date),
|
||||
budget: expect.any(Object),
|
||||
created_at: expect.any(Date),
|
||||
updated_at: expect.any(Date),
|
||||
deleted_at: null,
|
||||
},
|
||||
{
|
||||
id: "campaign-id-2",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-2",
|
||||
starts_at: expect.any(Date),
|
||||
ends_at: expect.any(Date),
|
||||
budget: expect.any(Object),
|
||||
created_at: expect.any(Date),
|
||||
updated_at: expect.any(Date),
|
||||
deleted_at: null,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
const campaign = await service.retrieveCampaign(createdPromotion.id, {
|
||||
relations: ["budget"],
|
||||
it("should return all campaigns based on config select and relations param", async () => {
|
||||
const [campaigns, count] = await service.listAndCountCampaigns(
|
||||
{
|
||||
id: ["campaign-id-1"],
|
||||
},
|
||||
{
|
||||
relations: ["budget"],
|
||||
select: ["name", "budget.limit"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(campaigns).toEqual([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
budget: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
campaign: expect.any(Object),
|
||||
limit: 1000,
|
||||
}),
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
used: 10,
|
||||
}),
|
||||
describe("createCampaigns", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toContain(
|
||||
"Value for Campaign.campaign_identifier is required, 'undefined' found"
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a basic campaign with promotions successfully", async () => {
|
||||
await createPromotions(repositoryManager)
|
||||
it("should create a basic campaign successfully", async () => {
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
},
|
||||
])
|
||||
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
},
|
||||
])
|
||||
const campaign = await service.retrieveCampaign(createdCampaign.id)
|
||||
|
||||
const campaign = await service.retrieveCampaign(createdCampaign.id, {
|
||||
relations: ["promotions"],
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
promotions: [
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a campaign with campaign budget successfully", async () => {
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
|
||||
const [createdPromotion] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
budget: {
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
used: 10,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign(createdPromotion.id, {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-2",
|
||||
}),
|
||||
],
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
used: 10,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateCampaigns", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
.updateCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
it("should create a basic campaign with promotions successfully", async () => {
|
||||
await createPromotions(MikroOrmWrapper.forkManager())
|
||||
|
||||
expect(error.message).toContain('Campaign with id "" not found')
|
||||
})
|
||||
const startsAt = new Date("01/01/2024")
|
||||
const endsAt = new Date("01/01/2025")
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
},
|
||||
])
|
||||
|
||||
it("should update the attributes of a campaign successfully", async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
const campaign = await service.retrieveCampaign(createdCampaign.id, {
|
||||
relations: ["promotions"],
|
||||
})
|
||||
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should update the attributes of a campaign budget successfully", async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
budget: {
|
||||
limit: 100,
|
||||
used: 100,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
budget: expect.objectContaining({
|
||||
limit: 100,
|
||||
used: 100,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should update promotions of a campaign successfully", async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
await createPromotions(repositoryManager)
|
||||
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
},
|
||||
])
|
||||
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
promotions: [
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: startsAt,
|
||||
ends_at: endsAt,
|
||||
promotions: [
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-2",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateCampaigns", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
.updateCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toContain('Campaign with id "" not found')
|
||||
})
|
||||
|
||||
it("should update the attributes of a campaign successfully", async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-2",
|
||||
}),
|
||||
],
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should remove promotions of the campaign successfully", async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
await createPromotions(repositoryManager)
|
||||
it("should update the attributes of a campaign budget successfully", async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
})
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
budget: {
|
||||
limit: 100,
|
||||
used: 100,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const updatedCampaign = await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
promotions: [{ id: "promotion-id-1" }],
|
||||
})
|
||||
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
promotions: [
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
],
|
||||
budget: expect.objectContaining({
|
||||
limit: 100,
|
||||
used: 100,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveCampaign", () => {
|
||||
beforeEach(async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
})
|
||||
it("should update promotions of a campaign successfully", async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
await createPromotions(MikroOrmWrapper.forkManager())
|
||||
|
||||
const id = "campaign-id-1"
|
||||
const [updatedCampaign] = await service.updateCampaigns([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
},
|
||||
])
|
||||
|
||||
it("should return campaign for the given id", async () => {
|
||||
const campaign = await service.retrieveCampaign(id)
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
description: "test description 1",
|
||||
currency: "EUR",
|
||||
campaign_identifier: "new",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
promotions: [
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-2",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when campaign with id does not exist", async () => {
|
||||
let error
|
||||
it("should remove promotions of the campaign successfully", async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
await createPromotions(MikroOrmWrapper.forkManager())
|
||||
|
||||
try {
|
||||
await service.retrieveCampaign("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
|
||||
})
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"Campaign with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
const updatedCampaign = await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
promotions: [{ id: "promotion-id-1" }],
|
||||
})
|
||||
|
||||
it("should throw an error when a id is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveCampaign(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("campaign - id must be defined")
|
||||
})
|
||||
|
||||
it("should return campaign based on config select param", async () => {
|
||||
const campaign = await service.retrieveCampaign(id, {
|
||||
select: ["id"],
|
||||
expect(updatedCampaign).toEqual(
|
||||
expect.objectContaining({
|
||||
promotions: [
|
||||
expect.objectContaining({
|
||||
id: "promotion-id-1",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(campaign))
|
||||
describe("retrieveCampaign", () => {
|
||||
beforeEach(async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
const id = "campaign-id-1"
|
||||
|
||||
describe("deleteCampaigns", () => {
|
||||
it("should delete the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
it("should return campaign for the given id", async () => {
|
||||
const campaign = await service.retrieveCampaign(id)
|
||||
|
||||
await service.deleteCampaigns([createdCampaign.id])
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
const campaigns = await service.listCampaigns(
|
||||
{
|
||||
id: [createdCampaign.id],
|
||||
},
|
||||
{ withDeleted: true }
|
||||
)
|
||||
it("should throw an error when campaign with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
try {
|
||||
await service.retrieveCampaign("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
describe("softDeleteCampaigns", () => {
|
||||
it("should soft delete the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
expect(error.message).toEqual(
|
||||
"Campaign with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
await service.softDeleteCampaigns([createdCampaign.id])
|
||||
it("should throw an error when a id is not provided", async () => {
|
||||
let error
|
||||
|
||||
const campaigns = await service.listCampaigns({
|
||||
id: [createdCampaign.id],
|
||||
try {
|
||||
await service.retrieveCampaign(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("campaign - id must be defined")
|
||||
})
|
||||
|
||||
it("should return campaign based on config select param", async () => {
|
||||
const campaign = await service.retrieveCampaign(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(campaign))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
describe("deleteCampaigns", () => {
|
||||
it("should delete the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
await service.deleteCampaigns([createdCampaign.id])
|
||||
|
||||
const campaigns = await service.listCampaigns(
|
||||
{
|
||||
id: [createdCampaign.id],
|
||||
},
|
||||
{ withDeleted: true }
|
||||
)
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("softDeleteCampaigns", () => {
|
||||
it("should soft delete the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
await service.softDeleteCampaigns([createdCampaign.id])
|
||||
|
||||
const campaigns = await service.listCampaigns({
|
||||
id: [createdCampaign.id],
|
||||
})
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("restoreCampaigns", () => {
|
||||
it("should restore the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
await service.softDeleteCampaigns([createdCampaign.id])
|
||||
|
||||
let campaigns = await service.listCampaigns({
|
||||
id: [createdCampaign.id],
|
||||
})
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
await service.restoreCampaigns([createdCampaign.id])
|
||||
|
||||
campaigns = await service.listCampaigns({ id: [createdCampaign.id] })
|
||||
expect(campaigns).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("restoreCampaigns", () => {
|
||||
it("should restore the campaigns given an id successfully", async () => {
|
||||
const [createdCampaign] = await service.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
await service.softDeleteCampaigns([createdCampaign.id])
|
||||
|
||||
let campaigns = await service.listCampaigns({ id: [createdCampaign.id] })
|
||||
|
||||
expect(campaigns).toHaveLength(0)
|
||||
await service.restoreCampaigns([createdCampaign.id])
|
||||
|
||||
campaigns = await service.listCampaigns({ id: [createdCampaign.id] })
|
||||
expect(campaigns).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,209 +1,189 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { createCampaigns } from "../../../__fixtures__/campaigns"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Promotion Service: campaign usage", () => {
|
||||
let service: IPromotionModuleService
|
||||
let repositoryManager: SqlEntityManager
|
||||
let shutdownFunc: () => void
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.PROMOTION]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
repositoryManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
await createCampaigns(repositoryManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("registerUsage", () => {
|
||||
it("should register usage for type spend", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_SPEND",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-1",
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PROMOTION,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPromotionModuleService>) => {
|
||||
describe("Promotion Service: campaign usage", () => {
|
||||
beforeEach(async () => {
|
||||
await createCampaigns(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
describe("registerUsage", () => {
|
||||
it("should register usage for type spend", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_SPEND",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-1",
|
||||
})
|
||||
|
||||
const campaign = await service.retrieveCampaign("campaign-id-1", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
|
||||
expect(campaign.budget).toEqual(
|
||||
expect.objectContaining({
|
||||
type: "spend",
|
||||
limit: 1000,
|
||||
used: 700,
|
||||
const campaign = await service.retrieveCampaign("campaign-id-1", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign.budget).toEqual(
|
||||
expect.objectContaining({
|
||||
type: "spend",
|
||||
limit: 1000,
|
||||
used: 700,
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should register usage for type usage", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_USAGE",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-2",
|
||||
})
|
||||
it("should register usage for type usage", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_USAGE",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-2",
|
||||
})
|
||||
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign("campaign-id-2", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
const campaign = await service.retrieveCampaign("campaign-id-2", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign.budget).toEqual(
|
||||
expect.objectContaining({
|
||||
type: "usage",
|
||||
limit: 1000,
|
||||
used: 1,
|
||||
expect(campaign.budget).toEqual(
|
||||
expect.objectContaining({
|
||||
type: "usage",
|
||||
limit: 1000,
|
||||
used: 1,
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should not throw an error when compute action with code does not exist", async () => {
|
||||
const response = await service
|
||||
.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: "DOESNOTEXIST",
|
||||
},
|
||||
])
|
||||
.catch((e) => e)
|
||||
it("should not throw an error when compute action with code does not exist", async () => {
|
||||
const response = await service
|
||||
.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: "DOESNOTEXIST",
|
||||
},
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response).toEqual(undefined)
|
||||
})
|
||||
|
||||
it("should not register usage when limit is exceed for type usage", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_USAGE",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-2",
|
||||
})
|
||||
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-2",
|
||||
budget: { used: 1000, limit: 1000 },
|
||||
})
|
||||
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign("campaign-id-2", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
used: 1000,
|
||||
}),
|
||||
expect(response).toEqual(undefined)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should not register usage above limit when exceeded for type spend", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_SPEND",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-1",
|
||||
})
|
||||
it("should not register usage when limit is exceed for type usage", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_USAGE",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-2",
|
||||
})
|
||||
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
budget: { used: 900, limit: 1000 },
|
||||
})
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-2",
|
||||
budget: { used: 1000, limit: 1000 },
|
||||
})
|
||||
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 100,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 100,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 200,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 500,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign("campaign-id-1", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
const campaign = await service.retrieveCampaign("campaign-id-2", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
used: 1000,
|
||||
}),
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
used: 1000,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
|
||||
it("should not register usage above limit when exceeded for type spend", async () => {
|
||||
const createdPromotion = await service.create({
|
||||
code: "TEST_PROMO_SPEND",
|
||||
type: "standard",
|
||||
campaign_id: "campaign-id-1",
|
||||
})
|
||||
|
||||
await service.updateCampaigns({
|
||||
id: "campaign-id-1",
|
||||
budget: { used: 900, limit: 1000 },
|
||||
})
|
||||
|
||||
await service.registerUsage([
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_express",
|
||||
amount: 100,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
{
|
||||
action: "addShippingMethodAdjustment",
|
||||
shipping_method_id: "shipping_method_standard",
|
||||
amount: 100,
|
||||
code: createdPromotion.code!,
|
||||
},
|
||||
])
|
||||
|
||||
const campaign = await service.retrieveCampaign("campaign-id-1", {
|
||||
relations: ["budget"],
|
||||
})
|
||||
|
||||
expect(campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
used: 1000,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,71 +1,58 @@
|
||||
import { createMedusaContainer, PromotionType } from "@medusajs/utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { PromotionService } from "@services"
|
||||
import { PromotionType } from "@medusajs/utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { createPromotions } from "../../../__fixtures__/promotion"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { asValue } from "awilix"
|
||||
import ContainerLoader from "../../../../src/loaders/container"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Promotion Service", () => {
|
||||
let service: PromotionService
|
||||
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("promotionService")
|
||||
|
||||
await createPromotions(testManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
.create([
|
||||
{
|
||||
type: PromotionType.STANDARD,
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toContain(
|
||||
"Value for Promotion.code is required, 'undefined' found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a promotion successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
code: "PROMOTION_TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
},
|
||||
])
|
||||
|
||||
const [promotion] = await service.list({
|
||||
code: ["PROMOTION_TEST"],
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PROMOTION,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPromotionModuleService>) => {
|
||||
describe("Promotion Service", () => {
|
||||
beforeEach(async () => {
|
||||
await createPromotions(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
|
||||
expect(promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
code: "PROMOTION_TEST",
|
||||
is_automatic: false,
|
||||
type: "standard",
|
||||
describe("create", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
.create([
|
||||
{
|
||||
type: PromotionType.STANDARD,
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toContain(
|
||||
"Value for Promotion.code is required, 'undefined' found"
|
||||
)
|
||||
})
|
||||
)
|
||||
|
||||
it("should create a promotion successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
code: "PROMOTION_TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
},
|
||||
])
|
||||
|
||||
const [promotion] = await service.list({
|
||||
code: ["PROMOTION_TEST"],
|
||||
})
|
||||
|
||||
expect(promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
code: "PROMOTION_TEST",
|
||||
is_automatic: false,
|
||||
type: "standard",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -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-promotion-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_PROMOTION_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-promotion-test",
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import {TestDatabaseUtils} from "medusa-test-utils"
|
||||
|
||||
import * as PromotionModels from "@models"
|
||||
|
||||
const pathToMigrations = "../../src/migrations"
|
||||
const mikroOrmEntities = PromotionModels 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,33 +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_PROMOTION_DB_SCHEMA,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const injectedDependencies = {}
|
||||
|
||||
const modulesConfig_ = {
|
||||
[Modules.PROMOTION]: {
|
||||
definition: ModulesDefinition[Modules.PROMOTION],
|
||||
options: moduleOptions,
|
||||
},
|
||||
}
|
||||
|
||||
return {
|
||||
injectedDependencies,
|
||||
modulesConfig: modulesConfig_,
|
||||
databaseConfig: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_PROMOTION_DB_SCHEMA,
|
||||
},
|
||||
joinerConfig: [],
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from "./config"
|
||||
export * from "./database"
|
||||
@@ -18,6 +18,4 @@ module.exports = {
|
||||
testEnvironment: `node`,
|
||||
moduleFileExtensions: [`js`, `ts`],
|
||||
modulePathIgnorePatterns: ["dist/"],
|
||||
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
|
||||
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
|
||||
}
|
||||
|
||||
@@ -1,362 +1,353 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { MikroOrmWrapper } from "../utils"
|
||||
import { getInitModuleConfig } from "../utils/get-init-module-config"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Region Module Service", () => {
|
||||
let service: IRegionModuleService
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
service = medusaApp.modules[Modules.REGION]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
it("should create countries on application start", async () => {
|
||||
const countries = await service.listCountries({}, { take: null })
|
||||
expect(countries.length).toEqual(250)
|
||||
})
|
||||
|
||||
it("should create and list a region", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "Europe",
|
||||
currency_code: "EUR",
|
||||
automatic_taxes: false,
|
||||
})
|
||||
|
||||
expect(createdRegion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: createdRegion.id,
|
||||
name: "Europe",
|
||||
currency_code: "eur",
|
||||
countries: [],
|
||||
automatic_taxes: false,
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.REGION,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IRegionModuleService>) => {
|
||||
describe("Region Module Service", () => {
|
||||
it("should create countries on application start", async () => {
|
||||
const countries = await service.listCountries({}, { take: null })
|
||||
expect(countries.length).toEqual(250)
|
||||
})
|
||||
)
|
||||
|
||||
const region = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
it("should create and list a region", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "Europe",
|
||||
currency_code: "EUR",
|
||||
automatic_taxes: false,
|
||||
})
|
||||
|
||||
expect(region).toEqual(
|
||||
expect.objectContaining({
|
||||
id: region.id,
|
||||
name: "Europe",
|
||||
currency_code: "eur",
|
||||
countries: [],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a region with countries", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
const region = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
|
||||
expect(region).toEqual(
|
||||
expect.objectContaining({
|
||||
id: region.id,
|
||||
name: "North America",
|
||||
currency_code: "usd",
|
||||
automatic_taxes: true,
|
||||
countries: [
|
||||
expect(createdRegion).toEqual(
|
||||
expect.objectContaining({
|
||||
display_name: "Canada",
|
||||
iso_2: "ca",
|
||||
}),
|
||||
id: createdRegion.id,
|
||||
name: "Europe",
|
||||
currency_code: "eur",
|
||||
countries: [],
|
||||
automatic_taxes: false,
|
||||
})
|
||||
)
|
||||
|
||||
const region = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
|
||||
expect(region).toEqual(
|
||||
expect.objectContaining({
|
||||
display_name: "United States",
|
||||
iso_2: "us",
|
||||
}),
|
||||
],
|
||||
id: region.id,
|
||||
name: "Europe",
|
||||
currency_code: "eur",
|
||||
countries: [],
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw when country doesn't exist", async () => {
|
||||
await expect(
|
||||
service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["neverland"],
|
||||
})
|
||||
).rejects.toThrowError('Countries with codes: "neverland" do not exist')
|
||||
})
|
||||
|
||||
it("should throw when country is already assigned to a region", async () => {
|
||||
await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us"],
|
||||
})
|
||||
|
||||
await expect(
|
||||
service.create({
|
||||
name: "United States",
|
||||
currency_code: "USD",
|
||||
countries: ["us"],
|
||||
})
|
||||
).rejects.toThrowError(
|
||||
'Countries with codes: "us" are already assigned to a region'
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw when country is being assigned to multiple regions", async () => {
|
||||
await expect(
|
||||
service.create([
|
||||
{
|
||||
name: "United States",
|
||||
it("should create a region with countries", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us"],
|
||||
},
|
||||
{
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
const region = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
|
||||
expect(region).toEqual(
|
||||
expect.objectContaining({
|
||||
id: region.id,
|
||||
name: "North America",
|
||||
currency_code: "usd",
|
||||
automatic_taxes: true,
|
||||
countries: [
|
||||
expect.objectContaining({
|
||||
display_name: "Canada",
|
||||
iso_2: "ca",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
display_name: "United States",
|
||||
iso_2: "us",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw when country doesn't exist", async () => {
|
||||
await expect(
|
||||
service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["neverland"],
|
||||
})
|
||||
).rejects.toThrowError('Countries with codes: "neverland" do not exist')
|
||||
})
|
||||
|
||||
it("should throw when country is already assigned to a region", async () => {
|
||||
await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us"],
|
||||
},
|
||||
])
|
||||
).rejects.toThrowError(
|
||||
'Countries with codes: "us" are already assigned to a region'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it("should upsert the region successfully", async () => {
|
||||
const createdRegion = await service.upsert({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
await expect(
|
||||
service.create({
|
||||
name: "United States",
|
||||
currency_code: "USD",
|
||||
countries: ["us"],
|
||||
})
|
||||
).rejects.toThrowError(
|
||||
'Countries with codes: "us" are already assigned to a region'
|
||||
)
|
||||
})
|
||||
|
||||
await service.upsert({
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "MXN",
|
||||
countries: ["us", "mx"],
|
||||
automatic_taxes: false,
|
||||
})
|
||||
it("should throw when country is being assigned to multiple regions", async () => {
|
||||
await expect(
|
||||
service.create([
|
||||
{
|
||||
name: "United States",
|
||||
currency_code: "USD",
|
||||
countries: ["us"],
|
||||
},
|
||||
{
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us"],
|
||||
},
|
||||
])
|
||||
).rejects.toThrowError(
|
||||
'Countries with codes: "us" are already assigned to a region'
|
||||
)
|
||||
})
|
||||
|
||||
const latestRegion = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
it("should upsert the region successfully", async () => {
|
||||
const createdRegion = await service.upsert({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
expect(latestRegion).toMatchObject({
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "mxn",
|
||||
automatic_taxes: false,
|
||||
})
|
||||
expect(latestRegion.countries.map((c) => c.iso_2)).toEqual(["mx", "us"])
|
||||
})
|
||||
|
||||
it("should allow mixing create and update operations in upsert", async () => {
|
||||
const createdRegion = await service.upsert({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
const upserted = await service.upsert([
|
||||
{
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
},
|
||||
{
|
||||
name: "Central America",
|
||||
currency_code: "MXN",
|
||||
countries: ["mx"],
|
||||
},
|
||||
])
|
||||
|
||||
expect(upserted).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
await service.upsert({
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "usd",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "Central America",
|
||||
currency_code: "mxn",
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("should update the region successfully", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
await service.update(createdRegion.id, {
|
||||
name: "Americas",
|
||||
currency_code: "MXN",
|
||||
countries: ["us", "mx"],
|
||||
})
|
||||
|
||||
const latestRegion = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
|
||||
expect(latestRegion).toMatchObject({
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "mxn",
|
||||
})
|
||||
|
||||
expect(latestRegion.countries.map((c) => c.iso_2)).toEqual(["mx", "us"])
|
||||
})
|
||||
|
||||
it("should update the region without affecting countries if countries are undefined", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
await service.update(createdRegion.id, {
|
||||
name: "Americas",
|
||||
currency_code: "MXN",
|
||||
})
|
||||
|
||||
const updatedRegion = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
|
||||
expect(updatedRegion).toMatchObject({
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "mxn",
|
||||
})
|
||||
|
||||
expect(updatedRegion.countries.map((c) => c.iso_2)).toEqual(["ca", "us"])
|
||||
})
|
||||
|
||||
it("should remove the countries in a region successfully", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
await service.update(createdRegion.id, {
|
||||
name: "Americas",
|
||||
currency_code: "MXN",
|
||||
countries: [],
|
||||
})
|
||||
|
||||
const updatedRegion = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
|
||||
expect(updatedRegion).toMatchObject({
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "mxn",
|
||||
})
|
||||
|
||||
expect(updatedRegion.countries).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should fail updating the region countries to non-existent ones", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
await expect(
|
||||
service.update(
|
||||
{ id: createdRegion.id },
|
||||
{
|
||||
countries: ["us", "neverland"],
|
||||
}
|
||||
)
|
||||
).rejects.toThrowError('Countries with codes: "neverland" do not exist')
|
||||
})
|
||||
|
||||
it("should fail updating the region if there are duplicate countries", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
await expect(
|
||||
service.update(
|
||||
{ id: createdRegion.id },
|
||||
{
|
||||
countries: ["us", "us"],
|
||||
}
|
||||
)
|
||||
).rejects.toThrowError(
|
||||
'Countries with codes: "us" are already assigned to a region'
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail updating the region if country is already used", async () => {
|
||||
const [createdRegion] = await service.create([
|
||||
{
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
},
|
||||
{
|
||||
name: "Americas",
|
||||
currency_code: "USD",
|
||||
countries: ["mx"],
|
||||
},
|
||||
])
|
||||
|
||||
await expect(
|
||||
service.update(
|
||||
{ id: createdRegion.id },
|
||||
{
|
||||
currency_code: "MXN",
|
||||
countries: ["us", "mx"],
|
||||
}
|
||||
)
|
||||
).rejects.toThrowError(
|
||||
'Countries with codes: "mx" are already assigned to a region'
|
||||
)
|
||||
})
|
||||
automatic_taxes: false,
|
||||
})
|
||||
|
||||
it("should unset the region ID on the country when deleting a region", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
const latestRegion = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
|
||||
expect(latestRegion).toMatchObject({
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "mxn",
|
||||
automatic_taxes: false,
|
||||
})
|
||||
expect(latestRegion.countries.map((c) => c.iso_2)).toEqual(["mx", "us"])
|
||||
})
|
||||
|
||||
it("should allow mixing create and update operations in upsert", async () => {
|
||||
const createdRegion = await service.upsert({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
const upserted = await service.upsert([
|
||||
{
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
},
|
||||
{
|
||||
name: "Central America",
|
||||
currency_code: "MXN",
|
||||
countries: ["mx"],
|
||||
},
|
||||
])
|
||||
|
||||
expect(upserted).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "usd",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "Central America",
|
||||
currency_code: "mxn",
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("should update the region successfully", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
await service.update(createdRegion.id, {
|
||||
name: "Americas",
|
||||
currency_code: "MXN",
|
||||
countries: ["us", "mx"],
|
||||
})
|
||||
|
||||
const latestRegion = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
|
||||
expect(latestRegion).toMatchObject({
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "mxn",
|
||||
})
|
||||
|
||||
expect(latestRegion.countries.map((c) => c.iso_2)).toEqual(["mx", "us"])
|
||||
})
|
||||
|
||||
it("should update the region without affecting countries if countries are undefined", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
await service.update(createdRegion.id, {
|
||||
name: "Americas",
|
||||
currency_code: "MXN",
|
||||
})
|
||||
|
||||
const updatedRegion = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
|
||||
expect(updatedRegion).toMatchObject({
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "mxn",
|
||||
})
|
||||
|
||||
expect(updatedRegion.countries.map((c) => c.iso_2)).toEqual([
|
||||
"ca",
|
||||
"us",
|
||||
])
|
||||
})
|
||||
|
||||
it("should remove the countries in a region successfully", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
await service.update(createdRegion.id, {
|
||||
name: "Americas",
|
||||
currency_code: "MXN",
|
||||
countries: [],
|
||||
})
|
||||
|
||||
const updatedRegion = await service.retrieve(createdRegion.id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
|
||||
expect(updatedRegion).toMatchObject({
|
||||
id: createdRegion.id,
|
||||
name: "Americas",
|
||||
currency_code: "mxn",
|
||||
})
|
||||
|
||||
expect(updatedRegion.countries).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should fail updating the region countries to non-existent ones", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
await expect(
|
||||
service.update(
|
||||
{ id: createdRegion.id },
|
||||
{
|
||||
countries: ["us", "neverland"],
|
||||
}
|
||||
)
|
||||
).rejects.toThrowError('Countries with codes: "neverland" do not exist')
|
||||
})
|
||||
|
||||
it("should fail updating the region if there are duplicate countries", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
await expect(
|
||||
service.update(
|
||||
{ id: createdRegion.id },
|
||||
{
|
||||
countries: ["us", "us"],
|
||||
}
|
||||
)
|
||||
).rejects.toThrowError(
|
||||
'Countries with codes: "us" are already assigned to a region'
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail updating the region if country is already used", async () => {
|
||||
const [createdRegion] = await service.create([
|
||||
{
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
},
|
||||
{
|
||||
name: "Americas",
|
||||
currency_code: "USD",
|
||||
countries: ["mx"],
|
||||
},
|
||||
])
|
||||
|
||||
await expect(
|
||||
service.update(
|
||||
{ id: createdRegion.id },
|
||||
{
|
||||
countries: ["us", "mx"],
|
||||
}
|
||||
)
|
||||
).rejects.toThrowError(
|
||||
'Countries with codes: "mx" are already assigned to a region'
|
||||
)
|
||||
})
|
||||
|
||||
it("should unset the region ID on the country when deleting a region", async () => {
|
||||
const createdRegion = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
await service.delete(createdRegion.id)
|
||||
|
||||
const resp = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
expect(resp.countries).toHaveLength(2)
|
||||
})
|
||||
})
|
||||
|
||||
await service.delete(createdRegion.id)
|
||||
|
||||
const resp = await service.create({
|
||||
name: "North America",
|
||||
currency_code: "USD",
|
||||
countries: ["us", "ca"],
|
||||
})
|
||||
|
||||
expect(resp.countries).toHaveLength(2)
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,8 +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-region-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_REGION_DB_SCHEMA = "public"
|
||||
// TODO: Remove this when all modules are migrated to v2
|
||||
process.env.MEDUSA_FF_MEDUSA_V2 = true
|
||||
@@ -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-region-test",
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import {TestDatabaseUtils} from "medusa-test-utils"
|
||||
|
||||
import * as RegionModels from "@models"
|
||||
|
||||
const pathToMigrations = "../../src/migrations"
|
||||
const mikroOrmEntities = RegionModels 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,33 +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_REGION_DB_SCHEMA,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const injectedDependencies = {}
|
||||
|
||||
const modulesConfig_ = {
|
||||
[Modules.REGION]: {
|
||||
definition: ModulesDefinition[Modules.REGION],
|
||||
options: moduleOptions,
|
||||
},
|
||||
}
|
||||
|
||||
return {
|
||||
injectedDependencies,
|
||||
modulesConfig: modulesConfig_,
|
||||
databaseConfig: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_REGION_DB_SCHEMA,
|
||||
},
|
||||
joinerConfig: [],
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export * from "./config"
|
||||
export * from "./database"
|
||||
|
||||
@@ -17,6 +17,4 @@ module.exports = {
|
||||
testEnvironment: `node`,
|
||||
moduleFileExtensions: [`js`, `ts`],
|
||||
modulePathIgnorePatterns: ["dist/"],
|
||||
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
|
||||
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
import { IUserModuleService } from "@medusajs/types/dist/user"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { MockEventBusService } from "medusa-test-utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { UserEvents } from "@medusajs/utils"
|
||||
import { createInvites } from "../../../__fixtures__/invite"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -28,243 +25,235 @@ const defaultInviteData = [
|
||||
},
|
||||
]
|
||||
|
||||
describe("UserModuleService - Invite", () => {
|
||||
let service: IUserModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.USER]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
testManager = MikroOrmWrapper.forkManager()
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
describe("listInvites", () => {
|
||||
it("should list invites", async () => {
|
||||
await createInvites(testManager, defaultInviteData)
|
||||
|
||||
const invites = await service.listInvites()
|
||||
|
||||
expect(invites).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list invites by id", async () => {
|
||||
await createInvites(testManager, defaultInviteData)
|
||||
const invites = await service.listInvites({
|
||||
id: ["1"],
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.USER,
|
||||
moduleOptions: {
|
||||
jwt_secret: "test",
|
||||
},
|
||||
injectedDependencies: {
|
||||
eventBusModuleService: new MockEventBusService(),
|
||||
},
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IUserModuleService>) => {
|
||||
describe("UserModuleService - Invite", () => {
|
||||
beforeEach(async () => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
expect(invites).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
describe("listInvites", () => {
|
||||
it("should list invites", async () => {
|
||||
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
|
||||
|
||||
describe("listAndCountInvites", () => {
|
||||
it("should list and count invites", async () => {
|
||||
await createInvites(testManager, defaultInviteData)
|
||||
const [invites, count] = await service.listAndCountInvites()
|
||||
const invites = await service.listInvites()
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(invites).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should listAndCount invites by id", async () => {
|
||||
await createInvites(testManager, defaultInviteData)
|
||||
const [invites, count] = await service.listAndCountInvites({
|
||||
id: "1",
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(invites).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveInvite", () => {
|
||||
const id = "1"
|
||||
|
||||
it("should return an invite for the given id", async () => {
|
||||
await createInvites(testManager, defaultInviteData)
|
||||
const invite = await service.retrieveInvite(id)
|
||||
|
||||
expect(invite).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
expect(invites).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an invite with the given id does not exist", async () => {
|
||||
const error = await service
|
||||
.retrieveInvite("does-not-exist")
|
||||
.catch((e) => e)
|
||||
it("should list invites by id", async () => {
|
||||
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
|
||||
const invites = await service.listInvites({
|
||||
id: ["1"],
|
||||
})
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"Invite with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when inviteId is not provided", async () => {
|
||||
const error = await service
|
||||
.retrieveInvite(undefined as unknown as string)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual("invite - id must be defined")
|
||||
})
|
||||
|
||||
it("should return invite based on config select param", async () => {
|
||||
await createInvites(testManager, defaultInviteData)
|
||||
const invite = await service.retrieveInvite(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
expect(invite).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateInvite", () => {
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
const error = await service
|
||||
.updateInvites([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
},
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual('Invite with id "does-not-exist" not found')
|
||||
})
|
||||
|
||||
it("should emit invite updated events", async () => {
|
||||
await createInvites(testManager, defaultInviteData)
|
||||
|
||||
jest.clearAllMocks()
|
||||
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
await service.updateInvites([
|
||||
{
|
||||
id: "1",
|
||||
accepted: true,
|
||||
},
|
||||
])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.invite_updated,
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("resendInvite", () => {
|
||||
it("should emit token generated event for invites", async () => {
|
||||
await createInvites(testManager, defaultInviteData)
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
|
||||
await service.refreshInviteTokens(["1"])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.invite_token_generated,
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
describe("createInvitie", () => {
|
||||
it("should create an invite successfully", async () => {
|
||||
await service.createInvites(defaultInviteData)
|
||||
|
||||
const [invites, count] = await service.listAndCountInvites({
|
||||
id: ["1"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(invites[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
expect(invites).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it("should emit invite created events", async () => {
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
await service.createInvites(defaultInviteData)
|
||||
describe("listAndCountInvites", () => {
|
||||
it("should list and count invites", async () => {
|
||||
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
|
||||
const [invites, count] = await service.listAndCountInvites()
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.invite_created,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "2" },
|
||||
}),
|
||||
eventName: UserEvents.invite_created,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.invite_token_generated,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "2" },
|
||||
}),
|
||||
eventName: UserEvents.invite_token_generated,
|
||||
}),
|
||||
])
|
||||
expect(count).toEqual(2)
|
||||
expect(invites).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should listAndCount invites by id", async () => {
|
||||
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
|
||||
const [invites, count] = await service.listAndCountInvites({
|
||||
id: "1",
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(invites).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveInvite", () => {
|
||||
const id = "1"
|
||||
|
||||
it("should return an invite for the given id", async () => {
|
||||
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
|
||||
const invite = await service.retrieveInvite(id)
|
||||
|
||||
expect(invite).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an invite with the given id does not exist", async () => {
|
||||
const error = await service
|
||||
.retrieveInvite("does-not-exist")
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"Invite with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when inviteId is not provided", async () => {
|
||||
const error = await service
|
||||
.retrieveInvite(undefined as unknown as string)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual("invite - id must be defined")
|
||||
})
|
||||
|
||||
it("should return invite based on config select param", async () => {
|
||||
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
|
||||
const invite = await service.retrieveInvite(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
expect(invite).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateInvite", () => {
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
const error = await service
|
||||
.updateInvites([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
},
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'Invite with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should emit invite updated events", async () => {
|
||||
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
|
||||
|
||||
jest.clearAllMocks()
|
||||
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
await service.updateInvites([
|
||||
{
|
||||
id: "1",
|
||||
accepted: true,
|
||||
},
|
||||
])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.invite_updated,
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("resendInvite", () => {
|
||||
it("should emit token generated event for invites", async () => {
|
||||
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
|
||||
await service.refreshInviteTokens(["1"])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.invite_token_generated,
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
describe("createInvitie", () => {
|
||||
it("should create an invite successfully", async () => {
|
||||
await service.createInvites(defaultInviteData)
|
||||
|
||||
const [invites, count] = await service.listAndCountInvites({
|
||||
id: ["1"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(invites[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should emit invite created events", async () => {
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
await service.createInvites(defaultInviteData)
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.invite_created,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "2" },
|
||||
}),
|
||||
eventName: UserEvents.invite_created,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.invite_token_generated,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "2" },
|
||||
}),
|
||||
eventName: UserEvents.invite_token_generated,
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
import { IUserModuleService } from "@medusajs/types/dist/user"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { MockEventBusService } from "medusa-test-utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { UserEvents } from "@medusajs/utils"
|
||||
import { createUsers } from "../../../__fixtures__/user"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -21,231 +18,224 @@ const defaultUserData = [
|
||||
},
|
||||
]
|
||||
|
||||
describe("UserModuleService - User", () => {
|
||||
let service: IUserModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.USER]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
testManager = MikroOrmWrapper.forkManager()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
it("should list users", async () => {
|
||||
await createUsers(testManager, defaultUserData)
|
||||
|
||||
const users = await service.list()
|
||||
|
||||
expect(users).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list users by id", async () => {
|
||||
await createUsers(testManager, defaultUserData)
|
||||
const users = await service.list({
|
||||
id: ["1"],
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.USER,
|
||||
moduleOptions: {
|
||||
jwt_secret: "test",
|
||||
},
|
||||
injectedDependencies: {
|
||||
eventBusModuleService: new MockEventBusService(),
|
||||
},
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
medusaApp,
|
||||
}: SuiteOptions<IUserModuleService>) => {
|
||||
describe("UserModuleService - User", () => {
|
||||
afterEach(async () => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
expect(users).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
describe("list", () => {
|
||||
it("should list users", async () => {
|
||||
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
|
||||
|
||||
describe("listAndCount", () => {
|
||||
it("should list and count users", async () => {
|
||||
await createUsers(testManager, defaultUserData)
|
||||
const [users, count] = await service.listAndCount()
|
||||
const users = await service.list()
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(users).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list and count users by id", async () => {
|
||||
await createUsers(testManager, defaultUserData)
|
||||
const [Users, count] = await service.listAndCount({
|
||||
id: "1",
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(Users).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const id = "1"
|
||||
|
||||
it("should return an user for the given id", async () => {
|
||||
await createUsers(testManager, defaultUserData)
|
||||
|
||||
const user = await service.retrieve(id)
|
||||
|
||||
expect(user).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
expect(users).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an user with the given id does not exist", async () => {
|
||||
const error = await service.retrieve("does-not-exist").catch((e) => e)
|
||||
it("should list users by id", async () => {
|
||||
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
|
||||
const users = await service.list({
|
||||
id: ["1"],
|
||||
})
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"User with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a userId is not provided", async () => {
|
||||
const error = await service
|
||||
.retrieve(undefined as unknown as string)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual("user - id must be defined")
|
||||
})
|
||||
|
||||
it("should return user based on config select param", async () => {
|
||||
await createUsers(testManager, defaultUserData)
|
||||
|
||||
const User = await service.retrieve(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(User))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
const id = "1"
|
||||
|
||||
it("should delete the users given an id successfully", async () => {
|
||||
await createUsers(testManager, defaultUserData)
|
||||
|
||||
await service.delete([id])
|
||||
|
||||
const users = await service.list({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(users).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
const error = await service
|
||||
.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
},
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual('User with id "does-not-exist" not found')
|
||||
})
|
||||
|
||||
it("should emit user created events", async () => {
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
await service.create(defaultUserData)
|
||||
|
||||
jest.clearAllMocks()
|
||||
|
||||
await service.update([
|
||||
{
|
||||
id: "1",
|
||||
first_name: "John",
|
||||
},
|
||||
])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.updated,
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a user successfully", async () => {
|
||||
await service.create(defaultUserData)
|
||||
|
||||
const [User, count] = await service.listAndCount({
|
||||
id: ["1"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(User[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
expect(users).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it("should emit user created events", async () => {
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
await service.create(defaultUserData)
|
||||
describe("listAndCount", () => {
|
||||
it("should list and count users", async () => {
|
||||
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
|
||||
const [users, count] = await service.listAndCount()
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.created,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "2" },
|
||||
}),
|
||||
eventName: UserEvents.created,
|
||||
}),
|
||||
])
|
||||
expect(count).toEqual(2)
|
||||
expect(users).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list and count users by id", async () => {
|
||||
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
|
||||
const [Users, count] = await service.listAndCount({
|
||||
id: "1",
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(Users).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const id = "1"
|
||||
|
||||
it("should return an user for the given id", async () => {
|
||||
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
|
||||
|
||||
const user = await service.retrieve(id)
|
||||
|
||||
expect(user).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an user with the given id does not exist", async () => {
|
||||
const error = await service.retrieve("does-not-exist").catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"User with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a userId is not provided", async () => {
|
||||
const error = await service
|
||||
.retrieve(undefined as unknown as string)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual("user - id must be defined")
|
||||
})
|
||||
|
||||
it("should return user based on config select param", async () => {
|
||||
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
|
||||
|
||||
const User = await service.retrieve(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(User))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
const id = "1"
|
||||
|
||||
it("should delete the users given an id successfully", async () => {
|
||||
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
|
||||
|
||||
await service.delete([id])
|
||||
|
||||
const users = await service.list({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(users).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
const error = await service
|
||||
.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
},
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'User with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should emit user created events", async () => {
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
await service.create(defaultUserData)
|
||||
|
||||
jest.clearAllMocks()
|
||||
|
||||
await service.update([
|
||||
{
|
||||
id: "1",
|
||||
first_name: "John",
|
||||
},
|
||||
])
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.updated,
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a user successfully", async () => {
|
||||
await service.create(defaultUserData)
|
||||
|
||||
const [User, count] = await service.listAndCount({
|
||||
id: ["1"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(User[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should emit user created events", async () => {
|
||||
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
|
||||
await service.create(defaultUserData)
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
}),
|
||||
eventName: UserEvents.created,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
data: { id: "2" },
|
||||
}),
|
||||
eventName: UserEvents.created,
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -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-user-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_USER_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-user-test",
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import * as UserModels from "@models"
|
||||
|
||||
import {TestDatabaseUtils} from "medusa-test-utils"
|
||||
|
||||
const pathToMigrations = "../../src/migrations"
|
||||
const mikroOrmEntities = UserModels 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,37 +0,0 @@
|
||||
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
|
||||
|
||||
import { DB_URL } from "./database"
|
||||
import { MockEventBusService } from "medusa-test-utils"
|
||||
|
||||
export function getInitModuleConfig() {
|
||||
const moduleOptions = {
|
||||
defaultAdapterOptions: {
|
||||
database: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_USER_DB_SCHEMA,
|
||||
},
|
||||
},
|
||||
jwt_secret: "test",
|
||||
}
|
||||
|
||||
const injectedDependencies = {
|
||||
eventBusModuleService: new MockEventBusService(),
|
||||
}
|
||||
|
||||
const modulesConfig_ = {
|
||||
[Modules.USER]: {
|
||||
definition: ModulesDefinition[Modules.USER],
|
||||
options: moduleOptions,
|
||||
},
|
||||
}
|
||||
|
||||
return {
|
||||
injectedDependencies,
|
||||
modulesConfig: modulesConfig_,
|
||||
databaseConfig: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_USER_DB_SCHEMA,
|
||||
},
|
||||
joinerConfig: [],
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user