diff --git a/tests/resolvers/middleware/currentUserExists.spec.ts b/tests/resolvers/middleware/currentUserExists.spec.ts index c20465ec00..1e79c668de 100644 --- a/tests/resolvers/middleware/currentUserExists.spec.ts +++ b/tests/resolvers/middleware/currentUserExists.spec.ts @@ -3,7 +3,6 @@ import type mongoose from "mongoose"; import { Types } from "mongoose"; import { connect, disconnect } from "../../helpers/db"; import { currentUserExists } from "../../../src/resolvers/middleware/currentUserExists"; -import { USER_NOT_FOUND_ERROR } from "../../../src/constants"; import { beforeAll, afterAll, @@ -15,15 +14,14 @@ import { } from "vitest"; import type { TestUserType } from "../../helpers/userAndOrg"; import { createTestUser } from "../../helpers/userAndOrg"; +import { requestContext } from "../../../src/libraries"; let testUser: TestUserType; let MONGOOSE_INSTANCE: typeof mongoose; -let composedResolver: (root: any, args: any, context: any, info: any) => any; beforeAll(async () => { MONGOOSE_INSTANCE = await connect(); testUser = await createTestUser(); - composedResolver = currentUserExists(); }); afterAll(async () => { @@ -37,33 +35,28 @@ describe("resolvers -> Middleware -> currentUserExists", () => { vi.resetModules(); }); - it(`throws NotFoundError if no user exists with _id === context.userId`, async () => { - const { requestContext } = await import("../../../src/libraries"); - - const spy = vi - .spyOn(requestContext, "translate") - .mockImplementationOnce((message) => `Translated ${message}`); - - try { - const context = { - userId: Types.ObjectId().toString(), - }; - - await composedResolver({}, {}, context, {}); - } catch (error: any) { - expect(spy).toHaveBeenLastCalledWith(USER_NOT_FOUND_ERROR.MESSAGE); - expect(error.message).toEqual( - `Translated ${USER_NOT_FOUND_ERROR.MESSAGE}`, - ); - } + it("Test: User Exists", async () => { + vi.spyOn(requestContext, "translate").mockImplementation( + (): string => "test error message", + ); + const context = { + userId: testUser?.id.toString(), + }; + const next = vi.fn().mockReturnValue("next executed"); + const functionCall = await currentUserExists()(next)({}, {}, context, {}); + expect(functionCall).toBe("next executed"); }); - it(`throws no error if a user exists with _id === context.userId`, async () => { + it("Test: User does not exist", async () => { + vi.spyOn(requestContext, "translate").mockImplementation( + (): string => "test error message", + ); const context = { - userId: testUser!.id.toString(), + userId: Types.ObjectId().toString(), }; - - const nextResolver = await composedResolver({}, {}, context, {}); - expect(nextResolver).not.toBeNull(); + const next = vi.fn(); + await expect( + async () => await currentUserExists()(next)({}, {}, context, {}), + ).rejects.toThrowError("test error message"); }); });