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

Added tests for file src/resolvers/Mutation/createPost.ts #1833

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 104 additions & 9 deletions tests/resolvers/Mutation/createPost.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import {
createTestUserAndOrganization,
createTestUser,
} from "../../helpers/userAndOrg";
import { Organization } from "../../../src/models";
import { Organization, Post } from "../../../src/models";
import * as uploadEncodedImage from "../../../src/utilities/encodedImageStorage/uploadEncodedImage";
import * as uploadEncodedVideo from "../../../src/utilities/encodedVideoStorage/uploadEncodedVideo";
import { createPost as createPostResolverImage } from "../../../src/resolvers/Mutation/createPost";

let testUser: TestUserType;
Expand Down Expand Up @@ -146,11 +147,13 @@ describe("resolvers -> Mutation -> createPost", () => {
userId: randomUser?.id,
};

expect(args.data.pinned).toBe(true);
const { createPost: createPostResolver } = await import(
"../../../src/resolvers/Mutation/createPost"
);

await createPostResolver?.({}, args, context);
const createPost = await createPostResolver?.({}, args, context);
expect(createPost?.pinned).toBe(true);
} catch (error: any) {
expect(spy).toBeCalledWith(USER_NOT_AUTHORIZED_TO_PIN.MESSAGE);
expect(error.message).toEqual(
Expand Down Expand Up @@ -179,16 +182,18 @@ describe("resolvers -> Mutation -> createPost", () => {
userId: testUser?.id,
};

expect(args.data.pinned).toBe(true);

const { createPost: createPostResolver } = await import(
"../../../src/resolvers/Mutation/createPost"
);
const createdPost = await createPostResolver?.({}, args, context);

expect(createdPost).toEqual(
expect.objectContaining({
text: "New Post Text",
videoUrl: null, // Update the expected value to match the received value
title: "New Post Title",
pinned: true,
})
);

Expand All @@ -203,7 +208,7 @@ describe("resolvers -> Mutation -> createPost", () => {
).toBeTruthy();
});

it(`creates the post and returns it when image is not provided`, async () => {
it(`creates the post and returns it when image or video is not provided`, async () => {
const args: MutationCreatePostArgs = {
data: {
organizationId: testOrganization?.id,
Expand All @@ -218,11 +223,14 @@ describe("resolvers -> Mutation -> createPost", () => {
userId: testUser?.id,
};

expect(args.data.pinned).toBe(true);

const { createPost: createPostResolver } = await import(
"../../../src/resolvers/Mutation/createPost"
);

const createPostPayload = await createPostResolver?.({}, args, context);
expect(createPostPayload?.pinned).toBe(true);

expect(createPostPayload).toEqual(
expect.objectContaining({
Expand All @@ -235,13 +243,90 @@ describe("resolvers -> Mutation -> createPost", () => {
);
});

it(`creates the post and and returns it when an image is provided`, async () => {
const args: MutationCreatePostArgs = {
data: {
organizationId: testOrganization?.id,
text: "text",
title: "title",
pinned: true,
},
file: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAZSURBVBhXYzxz5sx/BiBgefLkCQMbGxsDAEdkBicg9wbaAAAAAElFTkSuQmCC", // Provide a supported file type
};

vi.spyOn(uploadEncodedImage, "uploadEncodedImage").mockImplementation(
async (encodedImageURL: string) => encodedImageURL
);

const context = {
userId: testUser?.id,
apiRootUrl: BASE_URL,
};

expect(args.data.pinned).toBe(true);

const createPostPayload = await createPostResolverImage?.(
{},
args,
context
);
expect(createPostPayload?.pinned).toBe(true);

const testCreatePostPayload = await Post.findOne({
_id: createPostPayload?._id,
imageUrl: { $ne: null },
}).lean();

//Ensures that the post is created and imageUrl is not null
expect(testCreatePostPayload).not.toBeNull();
});

it(`creates the post and and returns it when a video is provided`, async () => {
const args: MutationCreatePostArgs = {
data: {
organizationId: testOrganization?.id,
text: "text",
title: "title",
pinned: true,
},
file: "data:video/mp4;base64,VIDEO_BASE64_DATA_HERE", // Provide a supported file type
};

vi.spyOn(uploadEncodedVideo, "uploadEncodedVideo").mockImplementation(
async (uploadEncodedVideo: string) => uploadEncodedVideo
);

const context = {
userId: testUser?.id,
apiRootUrl: BASE_URL,
};

expect(args.data.pinned).toBe(true);

const createPostPayload = await createPostResolverImage?.(
{},
args,
context
);
expect(createPostPayload?.pinned).toBe(true);

const testCreatePostPayload = await Post.findOne({
_id: createPostPayload?._id,
videoUrl: { $ne: null },
}).lean();

//Ensures that the post is created and videoUrl is not null
expect(testCreatePostPayload).not.toBeNull();
});

it(`creates the post and throws an error for unsupported file type`, async () => {
const args: MutationCreatePostArgs = {
data: {
organizationId: testOrganization?.id,
text: "text",
videoUrl: "videoUrl",
title: "title",
pinned: true,
},
file: "unsupportedFile.txt", // Provide an unsupported file type
};
Expand All @@ -251,6 +336,8 @@ describe("resolvers -> Mutation -> createPost", () => {
apiRootUrl: BASE_URL,
};

expect(args.data.pinned).toBe(true);

// Mock the uploadEncodedImage function to throw an error for unsupported file types
vi.spyOn(uploadEncodedImage, "uploadEncodedImage").mockImplementation(
() => {
Expand Down Expand Up @@ -285,12 +372,13 @@ describe("resolvers -> Mutation -> createPost", () => {
const context = {
userId: testUser?.id,
};

expect(args.data.pinned).toBe(true);
const { createPost: createPostResolver } = await import(
"../../../src/resolvers/Mutation/createPost"
);

await createPostResolver?.({}, args, context);
const createdPost = await createPostResolver?.({}, args, context);
expect(createdPost?.pinned).toBe(true);
} catch (error: any) {
expect(error.message).toEqual(
`${LENGTH_VALIDATION_ERROR.MESSAGE} 256 characters in title`
Expand Down Expand Up @@ -318,11 +406,14 @@ describe("resolvers -> Mutation -> createPost", () => {
userId: testUser?.id,
};

expect(args.data.pinned).toBe(true);

const { createPost: createPostResolver } = await import(
"../../../src/resolvers/Mutation/createPost"
);

await createPostResolver?.({}, args, context);
const createdPost = await createPostResolver?.({}, args, context);
expect(createdPost?.pinned).toBe(true);
} catch (error: any) {
expect(error.message).toEqual(
`${LENGTH_VALIDATION_ERROR.MESSAGE} 500 characters in information`
Expand All @@ -349,10 +440,12 @@ describe("resolvers -> Mutation -> createPost", () => {
userId: testUser?.id,
};

expect(args.data.pinned).toBe(false);
const { createPost: createPostResolver } = await import(
"../../../src/resolvers/Mutation/createPost"
);
await createPostResolver?.({}, args, context);
const createdPost = await createPostResolver?.({}, args, context);
expect(createdPost?.pinned).toBe(false);
} catch (error: any) {
expect(error.message).toEqual(
`Post needs to be pinned inorder to add a title`
Expand All @@ -378,11 +471,13 @@ describe("resolvers -> Mutation -> createPost", () => {
const context = {
userId: testUser?.id,
};
expect(args.data.pinned).toBe(true);

const { createPost: createPostResolver } = await import(
"../../../src/resolvers/Mutation/createPost"
);
await createPostResolver?.({}, args, context);
const createPost = await createPostResolver?.({}, args, context);
expect(createPost?.pinned).toBe(true);
} catch (error: any) {
expect(error.message).toEqual(`Please provide a title to pin post`);
}
Expand Down
Loading