Skip to content

Commit

Permalink
test: Add tests for 100% coverage of src/resolvers/User/posts.ts
Browse files Browse the repository at this point in the history
- Implemented comprehensive test cases for `posts.ts` to ensure 100% code coverage.
- Added tests for various pagination arguments (`first`, `last`, `before`, `after`).
- Included edge cases for no posts and invalid filters.
- Verified error handling for invalid arguments and cursor values.

Fixes #1928
  • Loading branch information
Aaradhy-Sharma committed Jul 26, 2024
1 parent d164ddd commit 998f844
Showing 1 changed file with 58 additions and 0 deletions.
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);
}
});
});

0 comments on commit 998f844

Please sign in to comment.