Skip to content

Commit

Permalink
Modified Tests for currentUserExists.ts (#1911)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarishshahmohsin authored Feb 28, 2024
1 parent 41713ab commit 2d1ad97
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions tests/resolvers/middleware/currentUserExists.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 () => {
Expand All @@ -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");
});
});

0 comments on commit 2d1ad97

Please sign in to comment.