|
| 1 | +import { EventBus } from "@nestjs/cqrs"; |
| 2 | +import { Repository } from "typeorm"; |
| 3 | + |
| 4 | +import { UserIdService } from "src/core/authentication"; |
| 5 | +import { createEventBusMock } from "src/test/mocks"; |
| 6 | +import { createMockRepository } from "src/test/mocks/repository.mock"; |
| 7 | +import { createUserIdServiceMock } from "src/test/mocks/user-id.service.mock"; |
| 8 | + |
| 9 | +import { CurrentProfileService } from "../../../domain/services/current-profile.service"; |
| 10 | +import { ProfileService } from "../../../domain/services/profile.service"; |
| 11 | +import { Profile } from "../../../infrastructure/entities/profile.entity"; |
| 12 | +import { DeleteCurrentProfileHandler } from "./delete-current-profile.handler"; |
| 13 | + |
| 14 | +describe(DeleteCurrentProfileHandler.name, () => { |
| 15 | + let commandHandler: DeleteCurrentProfileHandler; |
| 16 | + let currentProfileService: CurrentProfileService; |
| 17 | + let eventBus: EventBus; |
| 18 | + let profiles: Repository<Profile>; |
| 19 | + let profileService: ProfileService; |
| 20 | + let userIdService: UserIdService; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + eventBus = createEventBusMock(); |
| 24 | + profiles = createMockRepository<Profile>(); |
| 25 | + userIdService = createUserIdServiceMock(); |
| 26 | + |
| 27 | + profileService = new ProfileService(eventBus, profiles); |
| 28 | + |
| 29 | + currentProfileService = new CurrentProfileService(profiles, userIdService); |
| 30 | + |
| 31 | + commandHandler = new DeleteCurrentProfileHandler( |
| 32 | + currentProfileService, |
| 33 | + profileService, |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + describe("execute", () => { |
| 38 | + it("should delete current profile", async () => { |
| 39 | + const profile = new Profile(); |
| 40 | + profiles.save(profile); |
| 41 | + |
| 42 | + expect(profiles.find()).not.toEqual([]); |
| 43 | + |
| 44 | + await commandHandler.execute(); |
| 45 | + |
| 46 | + expect(profiles.find()).toEqual([]); |
| 47 | + expect(eventBus.publish).toHaveBeenCalledTimes(1); |
| 48 | + }); |
| 49 | + }); |
| 50 | +}); |
0 commit comments