Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
Signed-off-by: NishantSinghhhhh <[email protected]>
  • Loading branch information
NishantSinghhhhh committed Dec 28, 2024
1 parent f7390de commit 7d5c8f6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 76 deletions.
53 changes: 52 additions & 1 deletion tests/resolvers/Query/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { User } from "../../../src/models";
import { user as userResolver } from "../../../src/resolvers/Query/user";
import { connect, disconnect } from "../../helpers/db";

import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { deleteUserFromCache } from "../../../src/services/UserCache/deleteUserFromCache";
import type { QueryUserArgs } from "../../../src/types/generatedGraphQLTypes";
import type { TestUserType } from "../../helpers/userAndOrg";
Expand Down Expand Up @@ -64,4 +64,55 @@ describe("resolvers -> Query -> user", () => {
image: null,
});
});

it("throws NotFoundError when the specified user ID does not exist", async () => {
expect.assertions(2);
const nonExistentUserId = new Types.ObjectId().toString();
const args = {
id: nonExistentUserId,
};

const context = {
userId: new Types.ObjectId().toString(),
};

if (typeof userResolver === "function") {
try {
await userResolver({}, args, context);
} catch (error: unknown) {
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toEqual(USER_NOT_FOUND_ERROR.DESC);
}
} else {
throw new Error("userResolver is not defined");
}
});

it("throws NotFoundError when fetching user profile and user is null", async () => {
expect.assertions(2);
const args = {
id: new Types.ObjectId().toString(),
};

const context = {
userId: new Types.ObjectId().toString(),
};

// Mock User.findById to return null
vi.spyOn(User, "findById").mockResolvedValueOnce(null);

if (typeof userResolver === "function") {
try {
await userResolver({}, args, context);
} catch (error: unknown) {
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toEqual(USER_NOT_FOUND_ERROR.DESC);
}
} else {
throw new Error("userResolver is not defined");
}

// Restore original implementation
vi.restoreAllMocks();
});
});
76 changes: 1 addition & 75 deletions tests/resolvers/User/userAccess.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { User, Organization, AppUserProfile } from "../../../src/models";
import { connect, disconnect } from "../../helpers/db";
import type { TestUserType } from "../../helpers/userAndOrg";
import { createTestUser } from "../../helpers/userAndOrg";
import { beforeAll, afterAll, describe, it, expect, vi } from "vitest";
import { beforeAll, afterAll, describe, it, expect } from "vitest";
import type mongoose from "mongoose";
import { Types } from "mongoose";
import { FundraisingCampaignPledge } from "../../../src/models/FundraisingCampaignPledge";
Expand Down Expand Up @@ -200,78 +200,4 @@ describe("user Query", () => {
image: user?.image ? `${BASE_URL}${user.image}` : null,
});
});

it("throws NotFoundError when the specified user ID does not exist", async () => {
expect.assertions(2);
const nonExistentUserId = new Types.ObjectId().toString();
const args = {
id: nonExistentUserId,
};

const context = {
userId: new Types.ObjectId().toString(),
};

if (typeof userResolver === "function") {
try {
await userResolver({}, args, context);
} catch (error: unknown) {
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toEqual(USER_NOT_FOUND_ERROR.DESC);
}
} else {
throw new Error("userResolver is not defined");
}
});

it("throws NotFoundError when fetching user profile and user is null", async () => {
expect.assertions(2);
const args = {
id: new Types.ObjectId().toString(),
};

const context = {
userId: new Types.ObjectId().toString(),
};

// Mock User.findById to return null
vi.spyOn(User, "findById").mockResolvedValueOnce(null);

if (typeof userResolver === "function") {
try {
await userResolver({}, args, context);
} catch (error: unknown) {
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toEqual(USER_NOT_FOUND_ERROR.DESC);
}
} else {
throw new Error("userResolver is not defined");
}

// Restore original implementation
vi.restoreAllMocks();
});

it("throws NotFoundError when fetching user profile and user is null", async () => {
expect.assertions(2);

const args = { id: new Types.ObjectId().toString() };
const context = { userId: new Types.ObjectId().toString() };

// Mock User.findById to return null
vi.spyOn(User, "findById").mockResolvedValueOnce(null);

if (typeof userResolver === "function") {
try {
await userResolver({}, args, context);
} catch (error: unknown) {
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toEqual(USER_NOT_FOUND_ERROR.DESC);
}
} else {
throw new Error("userResolver is undefined");
}

vi.restoreAllMocks();
});
});

0 comments on commit 7d5c8f6

Please sign in to comment.