Skip to content
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

Add tests for 100% coverage of src/resolvers/User/posts.ts #2417

Merged
58 changes: 58 additions & 0 deletions tests/resolvers/User/post.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Replace with the correct path

import { GraphQLError } from "graphql";
import type mongoose from "mongoose";
import { Types } from "mongoose";
Expand Down Expand Up @@ -62,6 +63,7 @@ describe("resolvers -> User -> post", () => {
}
}
});

it(`returns the expected connection object`, async () => {
const parent = testUser as InterfaceUser;
const connection = await postResolver?.(
Expand Down Expand Up @@ -92,7 +94,49 @@ describe("resolvers -> User -> post", () => {
expect(connection?.pageInfo.startCursor).toEqual(testPost2?._id.toString());
expect(connection?.totalCount).toEqual(totalCount);
});

it("returns an empty connection object if no posts are found", async () => {
await Post.deleteMany({ creatorId: testUser?._id });
const parent = testUser as InterfaceUser;
const connection = await postResolver?.(parent, { first: 2 }, {});

expect(connection?.edges).toHaveLength(0);
expect(connection?.totalCount).toEqual(0);
expect(connection?.pageInfo.endCursor).toBeNull();
expect(connection?.pageInfo.startCursor).toBeNull();
expect(connection?.pageInfo.hasNextPage).toBe(false);
expect(connection?.pageInfo.hasPreviousPage).toBe(false);
});

it("handles different pagination arguments correctly", async () => {
// Recreate posts for pagination testing
testPost = await Post.create({
text: `text${nanoid().toLowerCase()}`,
creatorId: testUser?._id,
organization: testOrganization?._id,
pinned: false,
});
testPost2 = await Post.create({
text: `text${nanoid().toLowerCase()}`,
creatorId: testUser?._id,
organization: testOrganization?._id,
pinned: false,
});

const parent = testUser as InterfaceUser;

const connectionFirst = await postResolver?.(parent, { first: 1 }, {});
expect(connectionFirst?.edges).toHaveLength(1);
expect(connectionFirst?.pageInfo.hasNextPage).toBe(true);
expect(connectionFirst?.pageInfo.hasPreviousPage).toBe(false);

const connectionLast = await postResolver?.(parent, { last: 1 }, {});
expect(connectionLast?.edges).toHaveLength(1);
expect(connectionLast?.pageInfo.hasNextPage).toBe(false);
expect(connectionLast?.pageInfo.hasPreviousPage).toBe(true);
});
});

describe("parseCursor function", () => {
it("returns failure state if argument cursorValue is an invalid cursor", async () => {
const result = await parseCursor({
Expand Down Expand Up @@ -121,4 +165,18 @@ describe("parseCursor function", () => {
expect(result.parsedCursor).toEqual(testPost?._id.toString());
}
});

it("returns failure state if creatorId is invalid", async () => {
const result = await parseCursor({
cursorName: "after",
cursorPath: ["after"],
cursorValue: testPost?._id.toString() as string,
creatorId: new Types.ObjectId().toString(),
});

expect(result.isSuccessful).toEqual(false);
if (result.isSuccessful === false) {
expect(result.errors.length).toBeGreaterThan(0);
}
});
});
Loading