Skip to content

Commit

Permalink
test(api): add test for switch organization handler (#797)
Browse files Browse the repository at this point in the history
Co-authored-by: Johan Book <{ID}+{username}@users.noreply.github.com>
  • Loading branch information
johanbook and Johan Book authored Jul 8, 2024
1 parent 86c5580 commit 53dcb52
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class SwitchOrganizationHandler
throw new UnauthorizedException();
}

this.currentOrganizationService.switchCurrentOrganization(
await this.currentOrganizationService.switchCurrentOrganization(
command.organizationId,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
}

Expand Down
1 change: 1 addition & 0 deletions services/api/src/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { TestSuite } from "./test-suite";
2 changes: 1 addition & 1 deletion services/api/src/test/mocks/event-bus.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ class EventBusMock {
}

export function createEventBusMock(): EventBus {
return new EventBusMock() as any;
return new EventBusMock() as unknown as EventBus;
}
2 changes: 1 addition & 1 deletion services/api/src/test/mocks/repository.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ class MockRepository<T extends ObjectLiteral> {
export function createMockRepository<T extends ObjectLiteral>(
data?: T[],
): Repository<T> {
return new MockRepository(data) as any;
return new MockRepository(data) as unknown as Repository<T>;
}
76 changes: 76 additions & 0 deletions services/api/src/test/test-suite.ts
Original file line number Diff line number Diff line change
@@ -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<ActiveOrganization>;
memberships: Repository<OrganizationMembership>;
organizations: Repository<Organization>;
profiles: Repository<Profile>;

activeOrganizationSerivce: ActiveOrganizationService;
currentOrganizationService: CurrentOrganizationService;
currentProfileService: CurrentProfileService;
membershipService: MembershipService;
organizationService: OrganizationService;

constructor() {
this.eventBus = createEventBusMock();

this.activeOrganizations = createMockRepository<ActiveOrganization>();
this.memberships = createMockRepository<OrganizationMembership>();
this.organizations = createMockRepository<Organization>([
{ id: "my-organization-id" } as any,
]);
this.profiles = createMockRepository<Profile>([
{ 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,
);
}
}

0 comments on commit 53dcb52

Please sign in to comment.