Chore/rm main entity concept (#7709)

**What**
Update the `MedusaService` class, factory and types to remove the concept of main modules. The idea being that all method will be explicitly named and suffixes to represent the object you are trying to manipulate.
This pr also includes various fixes in different modules

Co-authored-by: Stevche Radevski <4820812+sradevski@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2024-06-19 15:02:16 +02:00
committed by GitHub
parent 2895ccfba8
commit 48963f55ef
533 changed files with 6469 additions and 9769 deletions

View File

@@ -4,9 +4,7 @@ import { UserEvents } from "@medusajs/utils"
import {
MockEventBusService,
moduleIntegrationTestRunner,
SuiteOptions,
} from "medusa-test-utils"
import { createInvites } from "../../../__fixtures__/invite"
jest.setTimeout(30000)
@@ -28,7 +26,7 @@ const defaultInviteData = [
},
]
moduleIntegrationTestRunner({
moduleIntegrationTestRunner<IUserModuleService>({
moduleName: Modules.USER,
moduleOptions: {
jwt_secret: "test",
@@ -36,10 +34,7 @@ moduleIntegrationTestRunner({
injectedDependencies: {
eventBusModuleService: new MockEventBusService(),
},
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IUserModuleService>) => {
testSuite: ({ service }) => {
describe("UserModuleService - Invite", () => {
beforeEach(async () => {
jest.clearAllMocks()
@@ -47,8 +42,7 @@ moduleIntegrationTestRunner({
describe("listInvites", () => {
it("should list invites", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
await service.createInvites(defaultInviteData)
const invites = await service.listInvites()
expect(invites).toEqual([
@@ -62,7 +56,7 @@ moduleIntegrationTestRunner({
})
it("should list invites by id", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
await service.createInvites(defaultInviteData)
const invites = await service.listInvites({
id: ["1"],
})
@@ -77,7 +71,7 @@ moduleIntegrationTestRunner({
describe("listAndCountInvites", () => {
it("should list and count invites", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
await service.createInvites(defaultInviteData)
const [invites, count] = await service.listAndCountInvites()
expect(count).toEqual(2)
@@ -92,7 +86,7 @@ moduleIntegrationTestRunner({
})
it("should listAndCount invites by id", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
await service.createInvites(defaultInviteData)
const [invites, count] = await service.listAndCountInvites({
id: "1",
})
@@ -110,7 +104,7 @@ moduleIntegrationTestRunner({
const id = "1"
it("should return an invite for the given id", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
await service.createInvites(defaultInviteData)
const invite = await service.retrieveInvite(id)
expect(invite).toEqual(
@@ -139,7 +133,7 @@ moduleIntegrationTestRunner({
})
it("should return invite based on config select param", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
await service.createInvites(defaultInviteData)
const invite = await service.retrieveInvite(id, {
select: ["id"],
})
@@ -166,7 +160,7 @@ moduleIntegrationTestRunner({
})
it("should emit invite updated events", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
await service.createInvites(defaultInviteData)
jest.clearAllMocks()
@@ -190,12 +184,12 @@ moduleIntegrationTestRunner({
describe("resendInvite", () => {
it("should emit token generated event for invites", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
await service.createInvites(defaultInviteData)
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.refreshInviteTokens(["1"])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledTimes(2)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
data: { id: "1" },

View File

@@ -4,9 +4,7 @@ import { UserEvents } from "@medusajs/utils"
import {
MockEventBusService,
moduleIntegrationTestRunner,
SuiteOptions,
} from "medusa-test-utils"
import { createUsers } from "../../../__fixtures__/user"
jest.setTimeout(30000)
@@ -21,7 +19,7 @@ const defaultUserData = [
},
]
moduleIntegrationTestRunner({
moduleIntegrationTestRunner<IUserModuleService>({
moduleName: Modules.USER,
moduleOptions: {
jwt_secret: "test",
@@ -29,11 +27,7 @@ moduleIntegrationTestRunner({
injectedDependencies: {
eventBusModuleService: new MockEventBusService(),
},
testSuite: ({
MikroOrmWrapper,
service,
medusaApp,
}: SuiteOptions<IUserModuleService>) => {
testSuite: ({ service }) => {
describe("UserModuleService - User", () => {
afterEach(async () => {
jest.clearAllMocks()
@@ -41,9 +35,8 @@ moduleIntegrationTestRunner({
describe("list", () => {
it("should list users", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
const users = await service.list()
await service.createUsers(defaultUserData)
const users = await service.listUsers()
expect(users).toEqual([
expect.objectContaining({
@@ -56,8 +49,8 @@ moduleIntegrationTestRunner({
})
it("should list users by id", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
const users = await service.list({
await service.createUsers(defaultUserData)
const users = await service.listUsers({
id: ["1"],
})
@@ -71,8 +64,8 @@ moduleIntegrationTestRunner({
describe("listAndCount", () => {
it("should list and count users", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
const [users, count] = await service.listAndCount()
await service.createUsers(defaultUserData)
const [users, count] = await service.listAndCountUsers()
expect(count).toEqual(2)
expect(users).toEqual([
@@ -86,8 +79,8 @@ moduleIntegrationTestRunner({
})
it("should list and count users by id", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
const [Users, count] = await service.listAndCount({
await service.createUsers(defaultUserData)
const [Users, count] = await service.listAndCountUsers({
id: "1",
})
@@ -104,9 +97,9 @@ moduleIntegrationTestRunner({
const id = "1"
it("should return an user for the given id", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
await service.createUsers(defaultUserData)
const user = await service.retrieve(id)
const user = await service.retrieveUser(id)
expect(user).toEqual(
expect.objectContaining({
@@ -116,7 +109,9 @@ moduleIntegrationTestRunner({
})
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)
const error = await service
.retrieveUser("does-not-exist")
.catch((e) => e)
expect(error.message).toEqual(
"User with id: does-not-exist was not found"
@@ -125,16 +120,16 @@ moduleIntegrationTestRunner({
it("should throw an error when a userId is not provided", async () => {
const error = await service
.retrieve(undefined as unknown as string)
.retrieveUser(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)
await service.createUsers(defaultUserData)
const User = await service.retrieve(id, {
const User = await service.retrieveUser(id, {
select: ["id"],
})
@@ -150,11 +145,11 @@ moduleIntegrationTestRunner({
const id = "1"
it("should delete the users given an id successfully", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
await service.createUsers(defaultUserData)
await service.delete([id])
await service.deleteUsers([id])
const users = await service.list({
const users = await service.listUsers({
id: [id],
})
@@ -165,7 +160,7 @@ moduleIntegrationTestRunner({
describe("update", () => {
it("should throw an error when a id does not exist", async () => {
const error = await service
.update([
.updateUsers([
{
id: "does-not-exist",
},
@@ -179,11 +174,11 @@ moduleIntegrationTestRunner({
it("should emit user created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.create(defaultUserData)
await service.createUsers(defaultUserData)
jest.clearAllMocks()
await service.update([
await service.updateUsers([
{
id: "1",
first_name: "John",
@@ -202,9 +197,9 @@ moduleIntegrationTestRunner({
describe("create", () => {
it("should create a user successfully", async () => {
await service.create(defaultUserData)
await service.createUsers(defaultUserData)
const [User, count] = await service.listAndCount({
const [User, count] = await service.listAndCountUsers({
id: ["1"],
})
@@ -218,7 +213,7 @@ moduleIntegrationTestRunner({
it("should emit user created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.create(defaultUserData)
await service.createUsers(defaultUserData)
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([