|
| 1 | +import { beforeEach, vi, expect } from "vitest"; |
| 2 | +import { mockReset, mockDeep } from "vitest-mock-extended"; |
| 3 | + |
| 4 | +import type * as inviteMemberUtils from "../utils"; |
| 5 | + |
| 6 | +vi.mock("../utils", async () => { |
| 7 | + return inviteMemberUtilsMock; |
| 8 | +}); |
| 9 | + |
| 10 | +beforeEach(() => { |
| 11 | + mockReset(inviteMemberUtilsMock); |
| 12 | +}); |
| 13 | +const inviteMemberUtilsMock = mockDeep<typeof inviteMemberUtils>(); |
| 14 | + |
| 15 | +export const inviteMemberutilsScenarios = { |
| 16 | + checkPermissions: { |
| 17 | + fakePassed: () => |
| 18 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 19 | + //@ts-ignore |
| 20 | + inviteMemberUtilsMock.checkPermissions.mockResolvedValue(undefined), |
| 21 | + }, |
| 22 | + getTeamOrThrow: { |
| 23 | + fakeReturnTeam: (team: { id: number } & Record<string, any>, forInput: { teamId: number }) => { |
| 24 | + const fakedVal = { |
| 25 | + organizationSettings: null, |
| 26 | + parent: null, |
| 27 | + parentId: null, |
| 28 | + ...team, |
| 29 | + }; |
| 30 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 31 | + //@ts-ignore |
| 32 | + inviteMemberUtilsMock.getTeamOrThrow.mockImplementation((teamId) => { |
| 33 | + if (forInput.teamId === teamId) { |
| 34 | + return fakedVal; |
| 35 | + } |
| 36 | + throw new Error("Mock Error: Unhandled input"); |
| 37 | + }); |
| 38 | + return fakedVal; |
| 39 | + }, |
| 40 | + }, |
| 41 | + getOrgState: { |
| 42 | + /** |
| 43 | + * `getOrgState` completely generates the return value from input without using any outside variable like DB, etc. |
| 44 | + * So, it makes sense to let it use the actual implementation instead of mocking the output based on input |
| 45 | + */ |
| 46 | + useActual: async function () { |
| 47 | + const actualImport = await vi.importActual<typeof inviteMemberUtils>("../utils"); |
| 48 | + |
| 49 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 50 | + //@ts-ignore |
| 51 | + return inviteMemberUtilsMock.getOrgState.mockImplementation(actualImport.getOrgState); |
| 52 | + }, |
| 53 | + }, |
| 54 | + getUniqueInvitationsOrThrowIfEmpty: { |
| 55 | + useActual: async function () { |
| 56 | + const actualImport = await vi.importActual<typeof inviteMemberUtils>("../utils"); |
| 57 | + |
| 58 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 59 | + //@ts-ignore |
| 60 | + return inviteMemberUtilsMock.getUniqueInvitationsOrThrowIfEmpty.mockImplementation( |
| 61 | + actualImport.getUniqueInvitationsOrThrowIfEmpty |
| 62 | + ); |
| 63 | + }, |
| 64 | + }, |
| 65 | + findUsersWithInviteStatus: { |
| 66 | + useAdvancedMock: function ( |
| 67 | + returnVal: Awaited<ReturnType<typeof inviteMemberUtilsMock.findUsersWithInviteStatus>>, |
| 68 | + forInput: { |
| 69 | + team: any; |
| 70 | + invitations: { |
| 71 | + usernameOrEmail: string; |
| 72 | + }[]; |
| 73 | + } |
| 74 | + ) { |
| 75 | + inviteMemberUtilsMock.findUsersWithInviteStatus.mockImplementation(({ invitations, team }) => { |
| 76 | + const allInvitationsExist = invitations.every((invitation) => |
| 77 | + forInput.invitations.find((i) => i.usernameOrEmail === invitation.usernameOrEmail) |
| 78 | + ); |
| 79 | + if (forInput.team.id == team.id && allInvitationsExist) return returnVal; |
| 80 | + }); |
| 81 | + return returnVal; |
| 82 | + }, |
| 83 | + }, |
| 84 | + getOrgConnectionInfo: { |
| 85 | + useActual: async function () { |
| 86 | + const actualImport = await vi.importActual<typeof inviteMemberUtils>("../utils"); |
| 87 | + |
| 88 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 89 | + //@ts-ignore |
| 90 | + return inviteMemberUtilsMock.getOrgConnectionInfo.mockImplementation(actualImport.getOrgConnectionInfo); |
| 91 | + }, |
| 92 | + }, |
| 93 | +}; |
| 94 | + |
| 95 | +export const expects = { |
| 96 | + expectSignupEmailsToBeSent: ({ |
| 97 | + emails, |
| 98 | + team, |
| 99 | + inviterName, |
| 100 | + isOrg, |
| 101 | + teamId, |
| 102 | + }: { |
| 103 | + emails: string[]; |
| 104 | + team; |
| 105 | + inviterName: string; |
| 106 | + teamId: number; |
| 107 | + isOrg: boolean; |
| 108 | + }) => { |
| 109 | + emails.forEach((email, index) => { |
| 110 | + expect(inviteMemberUtilsMock.sendSignupToOrganizationEmail.mock.calls[index][0]).toEqual( |
| 111 | + expect.objectContaining({ |
| 112 | + usernameOrEmail: email, |
| 113 | + team: team, |
| 114 | + inviterName: inviterName, |
| 115 | + teamId: teamId, |
| 116 | + isOrg: isOrg, |
| 117 | + }) |
| 118 | + ); |
| 119 | + }); |
| 120 | + }, |
| 121 | +}; |
| 122 | +export default inviteMemberUtilsMock; |
0 commit comments