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

tests for src/lib/resolvers/Subscription/messageSentToGroupChat.ts #1095

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 19 additions & 18 deletions src/resolvers/Subscription/messageSentToGroupChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@ import { GroupChat } from "../../models";

const MESSAGE_SENT_TO_GROUP_CHAT = "MESSAGE_SENT_TO_GROUP_CHAT";

export const filterFunction = async function (
payload: any,
context: any
): Promise<boolean> {
const { currentUserId } = context.context;
const groupChatId = payload.messageSentToGroupChat.groupChatMessageBelongsTo;

const groupChat = await GroupChat.findOne({
_id: groupChatId,
}).lean();

const currentUserIsGroupChatMember = groupChat!.users.some(
(user) => user.toString() === currentUserId.toString()
);
return currentUserIsGroupChatMember;
};

export const messageSentToGroupChat: SubscriptionResolvers["messageSentToGroupChat"] =
{
// @ts-ignore
subscribe: withFilter(
(_parent, _args, context) =>
context.pubsub.asyncIterator([MESSAGE_SENT_TO_GROUP_CHAT]),

async (payload, _variables, context) => {
const { currentUserId } = context.context;

const groupChatId =
payload.messageSentToGroupChat.groupChatMessageBelongsTo;

const groupChat = await GroupChat.findOne({
_id: groupChatId,
}).lean();

const currentUserIsGroupChatMember = groupChat!.users.some(
(user) => user.toString() === currentUserId.toString()
);

return currentUserIsGroupChatMember;
}
context?.pubsub?.asyncIterator([MESSAGE_SENT_TO_GROUP_CHAT]),
async (payload, _variables, context) => filterFunction(payload, context)
),
};
59 changes: 59 additions & 0 deletions tests/resolvers/Subscription/messageSentToGroupChat.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import "dotenv/config";
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { connect, disconnect } from "../../helpers/db";
import mongoose from "mongoose";
import {
testGroupChatType,
createTestGroupChatMessage,
} from "../../helpers/groupChat";
import { filterFunction } from "../../../src/resolvers/Subscription/messageSentToGroupChat";

let MONGOOSE_INSTANCE: typeof mongoose | null;
let testGroupChat: testGroupChatType;

beforeAll(async () => {
MONGOOSE_INSTANCE = await connect();
testGroupChat = (await createTestGroupChatMessage())[2];
});
afterAll(async () => {
await disconnect(MONGOOSE_INSTANCE!);
});

describe("src -> resolvers -> Subscription -> messageSentToGroupChat", () => {
it("subscription filter function returns true", async () => {
const { messageSentToGroupChat: messageSentToGroupChatPayload } =
await import(
"../../../src/resolvers/Subscription/messageSentToGroupChat"
);

console.log("Before :", messageSentToGroupChatPayload);
console.log("gROUPChat", testGroupChat);
const _args = {};
const _parent = {};
const context = {
pubsub: {
asyncInterator: (_action: "MESSAGE_SENT_TO_GROUP_CHAT") => {
return;
},
},
context: { currentUserId: testGroupChat!.users[0] },
};
const payload = {
messageSentToGroupChat: {
groupChatMessageBelongsTo: testGroupChat!._id,
},
};
// @ts-ignore
messageSentToGroupChatPayload._parent = _parent;
// @ts-ignore
messageSentToGroupChatPayload._args = _args;
// @ts-ignore
messageSentToGroupChatPayload.context = context;
// @ts-ignore
messageSentToGroupChatPayload.payload = payload;
// @ts-ignore
const x = messageSentToGroupChatPayload?.subscribe;
expect(x()).not.toBe(null);
expect(await filterFunction(payload, context)).toBe(true);
});
});