-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(api): add test for switch organization handler (#797)
Co-authored-by: Johan Book <{ID}+{username}@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...e/organizations/application/handlers/command-handlers/switch-organization.handler.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { TestSuite } from "./test-suite"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |