-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Added Tests for: src/graphql/types/User/homePhoneNumber.ts #5352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ayush4958
wants to merge
4
commits into
PalisadoesFoundation:develop
Choose a base branch
from
Ayush4958:add-test-coverage-for-homephonenumber
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
da0d17c
test: add coverage for homePhoneNumber resolver
Ayush4958 3932893
test: add 100% code coverage for homePhoneNumber resolver
Ayush4958 7d4df9e
test: use strict toHaveBeenCalledWith assertions for findFirst queries
Ayush4958 bd5f627
test: add db failure simulation test for homePhoneNumber resolver
Ayush4958 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,167 @@ | ||
| import { faker } from "@faker-js/faker"; | ||
| import type { GraphQLFieldResolver, GraphQLObjectType } from "graphql"; | ||
| import { createMockGraphQLContext } from "test/_Mocks_/mockContextCreator/mockContextCreator"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import type { GraphQLContext } from "~/src/graphql/context"; | ||
| import { schema } from "~/src/graphql/schema"; | ||
| import type { User as UserType } from "~/src/graphql/types/User/User"; | ||
|
|
||
| const userType = schema.getType("User") as GraphQLObjectType; | ||
| const homePhoneNumberResolver = userType.getFields().homePhoneNumber | ||
| ?.resolve as GraphQLFieldResolver<UserType, GraphQLContext>; | ||
|
|
||
| describe("homePhoneNumberResolver", () => { | ||
| const DEFAULT_PHONE = "1234567890"; | ||
| const ROLE_REGULAR = "regular"; | ||
| const ROLE_ADMIN = "administrator"; | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| const runResolver = (parent: UserType, context: GraphQLContext) => | ||
| homePhoneNumberResolver(parent, {}, context, {} as never); | ||
|
|
||
| const createSetup = ( | ||
| isAuthenticated: boolean, | ||
| targetPhone: string | null | undefined, | ||
| isSelfAccess: boolean, | ||
| ) => { | ||
| const currentUserId = faker.string.uuid(); | ||
| const targetUserId = isSelfAccess ? currentUserId : faker.string.uuid(); | ||
| const { context, mocks } = createMockGraphQLContext( | ||
| isAuthenticated, | ||
| currentUserId, | ||
| ); | ||
|
|
||
| const parent = { | ||
| id: targetUserId, | ||
| homePhoneNumber: targetPhone, | ||
| } as unknown as UserType; | ||
|
|
||
| return { context, mocks, parent, currentUserId, targetUserId }; | ||
| }; | ||
|
|
||
| it("throws unauthenticated error when client is not authenticated", async () => { | ||
| const { context, mocks, parent } = createSetup(false, DEFAULT_PHONE, false); | ||
|
|
||
| await expect(runResolver(parent, context)).rejects.toThrowError( | ||
| expect.objectContaining({ | ||
| extensions: expect.objectContaining({ | ||
| code: "unauthenticated", | ||
| }), | ||
| }), | ||
| ); | ||
| expect( | ||
| mocks.drizzleClient.query.usersTable.findFirst, | ||
| ).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("throws unauthenticated error when current user cannot be found in database", async () => { | ||
| const { context, mocks, parent } = createSetup(true, DEFAULT_PHONE, false); | ||
|
|
||
| mocks.drizzleClient.query.usersTable.findFirst.mockResolvedValue(undefined); | ||
|
|
||
| await expect(runResolver(parent, context)).rejects.toThrowError( | ||
| expect.objectContaining({ | ||
| extensions: expect.objectContaining({ | ||
| code: "unauthenticated", | ||
| }), | ||
| }), | ||
| ); | ||
| expect( | ||
| mocks.drizzleClient.query.usersTable.findFirst, | ||
| ).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("throws unauthorized_action error when regular user accesses another user's data", async () => { | ||
| const { context, mocks, parent } = createSetup(true, DEFAULT_PHONE, false); | ||
|
|
||
| mocks.drizzleClient.query.usersTable.findFirst.mockResolvedValue({ | ||
| role: ROLE_REGULAR, | ||
| }); | ||
|
|
||
| await expect(runResolver(parent, context)).rejects.toThrowError( | ||
| expect.objectContaining({ | ||
| extensions: expect.objectContaining({ | ||
| code: "unauthorized_action", | ||
| }), | ||
| }), | ||
| ); | ||
| expect( | ||
| mocks.drizzleClient.query.usersTable.findFirst, | ||
| ).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("returns homePhoneNumber when user accesses their own data", async () => { | ||
| const expectedPhone = faker.phone.number(); | ||
| const { context, mocks, parent } = createSetup(true, expectedPhone, true); | ||
|
|
||
| mocks.drizzleClient.query.usersTable.findFirst.mockResolvedValue({ | ||
| role: ROLE_REGULAR, | ||
| }); | ||
|
|
||
| const result = await runResolver(parent, context); | ||
| expect(result).toBe(expectedPhone); | ||
| expect( | ||
| mocks.drizzleClient.query.usersTable.findFirst, | ||
| ).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("returns homePhoneNumber when administrator accesses another user's data", async () => { | ||
| const expectedPhone = faker.phone.number(); | ||
| const { context, mocks, parent } = createSetup(true, expectedPhone, false); | ||
|
|
||
| mocks.drizzleClient.query.usersTable.findFirst.mockResolvedValue({ | ||
| role: ROLE_ADMIN, | ||
| }); | ||
|
|
||
| const result = await runResolver(parent, context); | ||
| expect(result).toBe(expectedPhone); | ||
| expect( | ||
| mocks.drizzleClient.query.usersTable.findFirst, | ||
| ).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("returns null when homePhoneNumber is null", async () => { | ||
| const { context, mocks, parent } = createSetup(true, null, true); | ||
|
|
||
| mocks.drizzleClient.query.usersTable.findFirst.mockResolvedValue({ | ||
| role: ROLE_REGULAR, | ||
| }); | ||
|
|
||
| const result = await runResolver(parent, context); | ||
| expect(result).toBeNull(); | ||
| expect( | ||
| mocks.drizzleClient.query.usersTable.findFirst, | ||
| ).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("returns empty string when homePhoneNumber is empty string", async () => { | ||
| const { context, mocks, parent } = createSetup(true, "", true); | ||
|
|
||
| mocks.drizzleClient.query.usersTable.findFirst.mockResolvedValue({ | ||
| role: ROLE_REGULAR, | ||
| }); | ||
|
|
||
| const result = await runResolver(parent, context); | ||
| expect(result).toBe(""); | ||
| expect( | ||
| mocks.drizzleClient.query.usersTable.findFirst, | ||
| ).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("returns undefined when homePhoneNumber is undefined", async () => { | ||
| const { context, mocks, parent } = createSetup(true, undefined, true); | ||
|
|
||
| mocks.drizzleClient.query.usersTable.findFirst.mockResolvedValue({ | ||
| role: ROLE_REGULAR, | ||
| }); | ||
|
|
||
| const result = await runResolver(parent, context); | ||
| expect(result).toBeUndefined(); | ||
| expect( | ||
| mocks.drizzleClient.query.usersTable.findFirst, | ||
| ).toHaveBeenCalledOnce(); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.