From 53dcb520720801b3886fc966a94c2b50b2e36b97 Mon Sep 17 00:00:00 2001 From: Johan Book <13253042+johanbook@users.noreply.github.com> Date: Mon, 8 Jul 2024 14:49:18 +0200 Subject: [PATCH] test(api): add test for switch organization handler (#797) Co-authored-by: Johan Book <{ID}+{username}@users.noreply.github.com> --- .../switch-organization.handler.spec.ts | 31 ++++++++ .../switch-organization.handler.ts | 2 +- .../domain/services/membership.service.ts | 2 +- services/api/src/test/index.ts | 1 + services/api/src/test/mocks/event-bus.mock.ts | 2 +- .../api/src/test/mocks/repository.mock.ts | 2 +- services/api/src/test/test-suite.ts | 76 +++++++++++++++++++ 7 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 services/api/src/core/organizations/application/handlers/command-handlers/switch-organization.handler.spec.ts create mode 100644 services/api/src/test/index.ts create mode 100644 services/api/src/test/test-suite.ts diff --git a/services/api/src/core/organizations/application/handlers/command-handlers/switch-organization.handler.spec.ts b/services/api/src/core/organizations/application/handlers/command-handlers/switch-organization.handler.spec.ts new file mode 100644 index 00000000..43e0b33b --- /dev/null +++ b/services/api/src/core/organizations/application/handlers/command-handlers/switch-organization.handler.spec.ts @@ -0,0 +1,31 @@ +import { map } from "src/core/mapper"; +import { TestSuite } from "src/test"; + +import { SwitchOrganizationCommand } from "../../contracts/commands/switch-organization.command"; +import { SwitchOrganizationHandler } from "./switch-organization.handler"; + +describe(SwitchOrganizationHandler.name, () => { + let commandHandler: SwitchOrganizationHandler; + let testSuite: TestSuite; + + beforeEach(() => { + testSuite = new TestSuite(); + + commandHandler = new SwitchOrganizationHandler( + testSuite.currentOrganizationService, + testSuite.currentProfileService, + testSuite.organizationService, + ); + }); + + describe("can switch organization", () => { + it("should save changes to organization", async () => { + testSuite.memberships.exist = () => Promise.resolve(true); + + const command = map(SwitchOrganizationCommand, { organizationId: 1 }); + await commandHandler.execute(command); + + expect(testSuite.activeOrganizations.save).toHaveBeenCalled(); + }); + }); +}); diff --git a/services/api/src/core/organizations/application/handlers/command-handlers/switch-organization.handler.ts b/services/api/src/core/organizations/application/handlers/command-handlers/switch-organization.handler.ts index 2729301a..060ec47f 100644 --- a/services/api/src/core/organizations/application/handlers/command-handlers/switch-organization.handler.ts +++ b/services/api/src/core/organizations/application/handlers/command-handlers/switch-organization.handler.ts @@ -30,7 +30,7 @@ export class SwitchOrganizationHandler throw new UnauthorizedException(); } - this.currentOrganizationService.switchCurrentOrganization( + await this.currentOrganizationService.switchCurrentOrganization( command.organizationId, ); } diff --git a/services/api/src/core/organizations/domain/services/membership.service.ts b/services/api/src/core/organizations/domain/services/membership.service.ts index 26b60f6b..5773ab5e 100644 --- a/services/api/src/core/organizations/domain/services/membership.service.ts +++ b/services/api/src/core/organizations/domain/services/membership.service.ts @@ -40,7 +40,7 @@ export class MembershipService { if (!membership) { throw new NotFoundException( - "Unable to find membership to current organization. Please contact support", + "Unable to find membership to current organization", ); } diff --git a/services/api/src/test/index.ts b/services/api/src/test/index.ts new file mode 100644 index 00000000..58b7c721 --- /dev/null +++ b/services/api/src/test/index.ts @@ -0,0 +1 @@ +export { TestSuite } from "./test-suite"; diff --git a/services/api/src/test/mocks/event-bus.mock.ts b/services/api/src/test/mocks/event-bus.mock.ts index 55cb8899..7a431fcd 100644 --- a/services/api/src/test/mocks/event-bus.mock.ts +++ b/services/api/src/test/mocks/event-bus.mock.ts @@ -6,5 +6,5 @@ class EventBusMock { } export function createEventBusMock(): EventBus { - return new EventBusMock() as any; + return new EventBusMock() as unknown as EventBus; } diff --git a/services/api/src/test/mocks/repository.mock.ts b/services/api/src/test/mocks/repository.mock.ts index 45d981a6..bc042cab 100644 --- a/services/api/src/test/mocks/repository.mock.ts +++ b/services/api/src/test/mocks/repository.mock.ts @@ -55,5 +55,5 @@ class MockRepository { export function createMockRepository( data?: T[], ): Repository { - return new MockRepository(data) as any; + return new MockRepository(data) as unknown as Repository; } diff --git a/services/api/src/test/test-suite.ts b/services/api/src/test/test-suite.ts new file mode 100644 index 00000000..98e9c220 --- /dev/null +++ b/services/api/src/test/test-suite.ts @@ -0,0 +1,76 @@ +import { EventBus } from "@nestjs/cqrs"; +import { Repository } from "typeorm"; + +import { + CurrentOrganizationService, + Organization, +} from "src/core/organizations"; +import { ActiveOrganizationService } from "src/core/organizations/domain/services/active-organization.service"; +import { MembershipService } from "src/core/organizations/domain/services/membership.service"; +import { OrganizationService } from "src/core/organizations/domain/services/organization.service"; +import { ActiveOrganization } from "src/core/organizations/infrastructure/entities/active-organization.entity"; +import { OrganizationMembership } from "src/core/organizations/infrastructure/entities/organization-membership.entity"; +import { CurrentProfileService, Profile } from "src/core/profiles"; + +import { + createEventBusMock, + createMockRepository, + createUserIdServiceMock, +} from "./mocks"; + +export class TestSuite { + eventBus: EventBus; + + activeOrganizations: Repository; + memberships: Repository; + organizations: Repository; + profiles: Repository; + + activeOrganizationSerivce: ActiveOrganizationService; + currentOrganizationService: CurrentOrganizationService; + currentProfileService: CurrentProfileService; + membershipService: MembershipService; + organizationService: OrganizationService; + + constructor() { + this.eventBus = createEventBusMock(); + + this.activeOrganizations = createMockRepository(); + this.memberships = createMockRepository(); + this.organizations = createMockRepository([ + { id: "my-organization-id" } as any, + ]); + this.profiles = createMockRepository([ + { id: "my-profile-id" } as any, + ]); + + const userIdService = createUserIdServiceMock(); + + this.currentProfileService = new CurrentProfileService( + this.profiles, + userIdService, + ); + + this.membershipService = new MembershipService( + this.currentProfileService, + this.memberships, + ); + + this.activeOrganizationSerivce = new ActiveOrganizationService( + this.activeOrganizations, + this.currentProfileService, + ); + + this.currentOrganizationService = new CurrentOrganizationService( + this.activeOrganizationSerivce, + this.membershipService, + this.organizations, + ); + + this.organizationService = new OrganizationService( + this.eventBus, + this.membershipService, + this.organizations, + ); + } +}