|
| 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