-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: team invite signup not working (#13717)
* Fix team invite signup not working * Add unit test --------- Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
- Loading branch information
1 parent
4360c36
commit 2f1316a
Showing
3 changed files
with
107 additions
and
6 deletions.
There are no files selected for viewing
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,92 @@ | ||
import prismaMock from "../../../tests/libs/__mocks__/prismaMock"; | ||
|
||
import { describe, expect, it, beforeEach } from "vitest"; | ||
|
||
import { usernameCheckForSignup } from "./username"; | ||
|
||
describe("usernameCheckForSignup ", async () => { | ||
beforeEach(() => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
prismaMock.user.findUnique.mockImplementation(() => { | ||
return null; | ||
}); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
prismaMock.user.findMany.mockImplementation(() => { | ||
return []; | ||
}); | ||
}); | ||
|
||
it("should return available true for an email that doesn't exist", async () => { | ||
const res = await usernameCheckForSignup({ username: "johnny", email: "johnny@example.com" }); | ||
expect(res).toEqual({ | ||
available: true, | ||
premium: false, | ||
suggestedUsername: "", | ||
}); | ||
}); | ||
|
||
it("should return available false for an email that exists and a different username is provided", async () => { | ||
mockUserInDB({ | ||
id: 1, | ||
email: "john@example.com", | ||
username: "john", | ||
}); | ||
const res = await usernameCheckForSignup({ username: "johnny", email: "john@example.com" }); | ||
expect(res).toEqual({ | ||
available: false, | ||
premium: false, | ||
suggestedUsername: "johnny001", | ||
}); | ||
}); | ||
|
||
it("should return available true for an email that exists but the user is signing up for an organization", async () => { | ||
const userId = 1; | ||
mockUserInDB({ | ||
id: userId, | ||
email: "john@example.com", | ||
username: "john", | ||
}); | ||
mockMembership({ userId }); | ||
const res = await usernameCheckForSignup({ username: "john", email: "john@example.com" }); | ||
expect(res).toEqual({ | ||
available: true, | ||
// An organization can't have premium username | ||
premium: false, | ||
suggestedUsername: "", | ||
}); | ||
}); | ||
}); | ||
|
||
function mockUserInDB({ id, email, username }: { id: number; email: string; username: string }) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
prismaMock.user.findUnique.mockImplementation((arg) => { | ||
if (arg.where.email === email) { | ||
return { | ||
id, | ||
email, | ||
username, | ||
}; | ||
} | ||
return null; | ||
}); | ||
} | ||
|
||
function mockMembership({ userId }: { userId: number }) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
prismaMock.membership.findFirst.mockImplementation((arg) => { | ||
const isOrganizationWhereClause = | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
arg?.where?.team?.metadata?.path[0] === "isOrganization" && arg?.where?.team?.metadata?.equals === true; | ||
if (arg?.where?.userId === userId && isOrganizationWhereClause) { | ||
return { | ||
userId, | ||
teamId: 1, | ||
}; | ||
} | ||
}); | ||
} |
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