Skip to content

Commit fb5076f

Browse files
authored
Query returns null for the image field of the likedBy object of the post. (#2507)
* fixed likedby image issue * fixed likedby image issue * fixed likedby firstName and lastName issue * Added tests * fixing linting errors * fixing errors with test * removing unwanted files * Update docker-compose.prod.yaml * Update .coderabbit.yaml * Update .coderabbit.yaml * Update README.md * files * file * hlast * last * hlast
1 parent 54d44cf commit fb5076f

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/resolvers/Organization/posts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ export const posts: OrganizationResolvers["posts"] = async (
7979
})
8080
.sort(sort)
8181
.limit(parsedArgs.limit)
82+
.populate({
83+
path: "likedBy",
84+
select: "image firstName lastName",
85+
})
8286
.lean()
8387
.exec(),
8488

tests/resolvers/Post/likedBy.spec.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import "dotenv/config";
2+
import { connect, disconnect } from "../../helpers/db";
3+
import type mongoose from "mongoose";
4+
import { beforeAll, afterAll, describe, it, expect } from "vitest";
5+
import type { TestPostType } from "../../helpers/posts";
6+
import { createTestPost } from "../../helpers/posts";
7+
import type {
8+
TestOrganizationType,
9+
TestUserType,
10+
} from "../../helpers/userAndOrg";
11+
import type { InterfacePost } from "../../../src/models";
12+
import { Organization, Post, User } from "../../../src/models";
13+
import { posts as postResolver } from "../../../src/resolvers/Organization/posts";
14+
15+
let testPost: TestPostType;
16+
let testUser: TestUserType;
17+
let testOrganization: TestOrganizationType;
18+
let MONGOOSE_INSTANCE: typeof mongoose;
19+
20+
beforeAll(async () => {
21+
MONGOOSE_INSTANCE = await connect();
22+
[testUser, testOrganization, testPost] = await createTestPost();
23+
24+
await User.findByIdAndUpdate(testUser?._id, {
25+
$set: {
26+
image: "exampleimageurl.com",
27+
},
28+
});
29+
30+
await Post.updateOne(
31+
{
32+
_id: testPost?._id,
33+
},
34+
{
35+
$push: {
36+
likedBy: testUser?._id,
37+
},
38+
$inc: {
39+
likeCount: 1,
40+
commentCount: 1,
41+
},
42+
},
43+
);
44+
});
45+
46+
afterAll(async () => {
47+
await disconnect(MONGOOSE_INSTANCE);
48+
});
49+
50+
describe("resolvers -> organization -> posts", () => {
51+
it(`returns the post object for parent post`, async () => {
52+
const parent = await Organization.findById(testOrganization?._id).lean();
53+
54+
if (!parent) {
55+
throw new Error("Parent organization not found.");
56+
}
57+
58+
const postPayload = (await postResolver?.(parent, { first: 1 }, {})) as {
59+
edges: { node: InterfacePost }[];
60+
totalCount: number;
61+
};
62+
63+
expect(postPayload).toBeDefined();
64+
if (!postPayload) {
65+
throw new Error("postPayload is null or undefined");
66+
}
67+
expect(postPayload.edges).toBeDefined();
68+
expect(Array.isArray(postPayload.edges)).toBe(true);
69+
70+
const posts = await Post.find({
71+
organization: testOrganization?._id,
72+
}).lean();
73+
74+
expect(postPayload.edges.length).toEqual(posts.length);
75+
expect(postPayload.totalCount).toEqual(posts.length);
76+
const returnedPost = postPayload.edges[0].node;
77+
expect(returnedPost._id).toEqual(testPost?._id.toString());
78+
expect(returnedPost.likedBy).toHaveLength(1);
79+
expect(returnedPost.likedBy[0]._id).toEqual(testUser?._id);
80+
expect(returnedPost.likedBy[0].image).not.toBeNull();
81+
});
82+
});

0 commit comments

Comments
 (0)