From 991ea3169c41ce1b03d1ab88fba541f738116f55 Mon Sep 17 00:00:00 2001 From: meetul Date: Sun, 7 Jan 2024 16:32:37 +0530 Subject: [PATCH] Add tests for Action Items --- locales/en.json | 2 + locales/fr.json | 2 + locales/hi.json | 2 + locales/sp.json | 2 + locales/zh.json | 2 + src/resolvers/Mutation/updateActionItem.ts | 5 + tests/helpers/actionItem.ts | 124 ++++++++ tests/resolvers/ActionItem/assignedBy.spec.ts | 36 +++ tests/resolvers/ActionItem/assignedTo.spec.ts | 36 +++ tests/resolvers/ActionItem/category.spec.ts | 36 +++ tests/resolvers/ActionItem/createdBy.spec.ts | 36 +++ tests/resolvers/ActionItem/event.spec.ts | 72 +++++ tests/resolvers/ActionItem/updatedBy.spec.ts | 36 +++ tests/resolvers/Event/actionItems.spec.ts | 39 +++ .../Mutation/createActionItem.spec.ts | 242 +++++++++++++++ .../resolvers/Mutation/createCategory.spec.ts | 52 +++- .../Mutation/removeActionItem.spec.ts | 291 ++++++++++++++++++ tests/resolvers/Mutation/removeEvent.spec.ts | 27 +- .../Mutation/removeOrganization.spec.ts | 21 +- .../Mutation/updateActionItem.spec.ts | 261 ++++++++++++++++ .../resolvers/Mutation/updateCategory.spec.ts | 39 ++- tests/resolvers/Query/actionItem.spec.ts | 51 +++ .../Query/actionItemsByEvents.spec.ts | 41 +++ 23 files changed, 1450 insertions(+), 5 deletions(-) create mode 100644 tests/helpers/actionItem.ts create mode 100644 tests/resolvers/ActionItem/assignedBy.spec.ts create mode 100644 tests/resolvers/ActionItem/assignedTo.spec.ts create mode 100644 tests/resolvers/ActionItem/category.spec.ts create mode 100644 tests/resolvers/ActionItem/createdBy.spec.ts create mode 100644 tests/resolvers/ActionItem/event.spec.ts create mode 100644 tests/resolvers/ActionItem/updatedBy.spec.ts create mode 100644 tests/resolvers/Event/actionItems.spec.ts create mode 100644 tests/resolvers/Mutation/createActionItem.spec.ts create mode 100644 tests/resolvers/Mutation/removeActionItem.spec.ts create mode 100644 tests/resolvers/Mutation/updateActionItem.spec.ts create mode 100644 tests/resolvers/Query/actionItem.spec.ts create mode 100644 tests/resolvers/Query/actionItemsByEvents.spec.ts diff --git a/locales/en.json b/locales/en.json index 93b372d625..73075b61a1 100644 --- a/locales/en.json +++ b/locales/en.json @@ -5,6 +5,8 @@ "user.alreadyMember": "User is already a member", "user.profileImage.notFound": "User profile image not found", "task.notFound": "Task not found", + "category.notFound": "Category not found", + "actionItem.notFound": "Action Item not found", "advertisement.notFound": "Advertisement not found", "event.notFound": "Event not found", "eventProject.notFound": "Event project not found", diff --git a/locales/fr.json b/locales/fr.json index 37b669c31a..247582a3b8 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -5,6 +5,8 @@ "user.alreadyMember": "L'utilisateur est déjà membre", "user.profileImage.notFound": "Image du profil utilisateur introuvable", "task.notFound": "Tâche introuvable", + "category.notFound": "Catégorie non trouvée", + "actionItem.notFound": "Élément d\\’action non trouvé", "event.notFound": "Événement non trouvé", "eventProject.notFound": "Projet d'événement introuvable", "organization.notFound": "Organisation introuvable", diff --git a/locales/hi.json b/locales/hi.json index 5cb76a4748..f2e2863f34 100644 --- a/locales/hi.json +++ b/locales/hi.json @@ -5,6 +5,8 @@ "user.alreadyMember": "उपयोगकर्ता पहले से ही एक सदस्य है", "user.profileImage.notFound": "उपयोगकर्ता प्रोफ़ाइल छवि नहीं मिली", "task.notFound": "कार्य नहीं मिला", + "category.notFound": "श्रेणी नहीं मिली", + "actionItem.notFound": "कार्रवाई का मद नहीं मिला", "advertisement.notFound": "विज्ञापन नहीं मिला", "event.notFound": "घटना नहीं मिली", "eventProject.notFound": "इवेंट प्रोजेक्ट नहीं मिला", diff --git a/locales/sp.json b/locales/sp.json index 1f2822eca5..17131e712a 100644 --- a/locales/sp.json +++ b/locales/sp.json @@ -5,6 +5,8 @@ "user.alreadyMember": "El usuario ya es miembro", "user.profileImage.notFound": "No se encontró la imagen de perfil de usuario", "task.notFound": "Tarea no encontrada", + "category.notFound": "Categoría no encontrada", + "actionItem.notFound": "Elemento de acción no encontrado", "event.notFound": "Evento no encontrado", "eventProject.notFound": "Proyecto de evento no encontrado", "organization.notFound": "Organización no encontrada", diff --git a/locales/zh.json b/locales/zh.json index 60ce501ce3..899de1a251 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -5,6 +5,8 @@ "user.alreadyMember": "用戶已經是會員", "user.profileImage.notFound": "未找到用戶個人資料圖像", "task.notFound": "找不到任務", + "category.notFound": "找不到类别", + "actionItem.notFound": "找不到操作项", "event.notFound": "未找到事件", "eventProject.notFound": "未找到事件項目", "organization.notFound": "未找到組織", diff --git a/src/resolvers/Mutation/updateActionItem.ts b/src/resolvers/Mutation/updateActionItem.ts index 249a4435f4..249eb13273 100644 --- a/src/resolvers/Mutation/updateActionItem.ts +++ b/src/resolvers/Mutation/updateActionItem.ts @@ -130,6 +130,10 @@ export const updateActionItem: MutationResolvers["updateActionItem"] = async ( ? actionItem.assignmentDate : new Date(); + const updatedAssignedBy = sameAssignedUser + ? actionItem.assignedBy + : context.userId; + const updatedActionItem = await ActionItem.findOneAndUpdate( { _id: args.id, @@ -138,6 +142,7 @@ export const updateActionItem: MutationResolvers["updateActionItem"] = async ( ...(args.data as UpdateActionItemInputType), assignmentDate: updatedAssignmentDate, updatedBy: context.userId, + assignedBy: updatedAssignedBy, }, { new: true, diff --git a/tests/helpers/actionItem.ts b/tests/helpers/actionItem.ts new file mode 100644 index 0000000000..0a0c20a813 --- /dev/null +++ b/tests/helpers/actionItem.ts @@ -0,0 +1,124 @@ +import type { InterfaceActionItem } from "../../src/models"; +import { ActionItem, Category, Event } from "../../src/models"; +import type { Document } from "mongoose"; +import { + createTestUser, + createTestUserAndOrganization, + type TestOrganizationType, + type TestUserType, +} from "./userAndOrg"; +import type { TestCategoryType } from "./category"; +import { createTestCategory } from "./category"; +import { nanoid } from "nanoid"; +import type { TestEventType } from "./events"; + +export type TestActionItemType = InterfaceActionItem & Document; + +export const createTestActionItem = async (): Promise< + [ + TestUserType, + TestOrganizationType, + TestCategoryType, + TestActionItemType, + TestUserType + ] +> => { + const [testUser, testOrganization] = await createTestUserAndOrganization(); + const randomUser = await createTestUser(); + + const testCategory = await Category.create({ + createdBy: testUser?._id, + updatedBy: testUser?._id, + org: testOrganization?._id, + category: "Default", + }); + + const testActionItem = await ActionItem.create({ + createdBy: testUser?._id, + updatedBy: testUser?._id, + assignedTo: randomUser?._id, + assignedBy: testUser?._id, + category: testCategory?._id, + }); + + return [testUser, testOrganization, testCategory, testActionItem, randomUser]; +}; + +interface InterfaceCreateNewTestAction { + currUserId: string; + assignedUserId: string; + categoryId: string; +} + +export const createNewTestActionItem = async ({ + currUserId, + assignedUserId, + categoryId, +}: InterfaceCreateNewTestAction): Promise => { + const newTestActionItem = await ActionItem.create({ + createdBy: currUserId, + updatedBy: currUserId, + assignedTo: assignedUserId, + assignedBy: currUserId, + category: categoryId, + }); + + return newTestActionItem; +}; + +export const createTestActionItems = async (): Promise< + [TestUserType, TestEventType] +> => { + const randomUser = await createTestUser(); + const [testUser, testOrganization, testCategory] = await createTestCategory(); + + const testActionItem1 = await ActionItem.create({ + createdBy: testUser?._id, + updatedBy: testUser?._id, + assignedTo: randomUser?._id, + assignedBy: testUser?._id, + category: testCategory?._id, + }); + + const testActionItem2 = await ActionItem.create({ + createdBy: testUser?._id, + updatedBy: testUser?._id, + assignedTo: randomUser?._id, + assignedBy: testUser?._id, + category: testCategory?._id, + }); + + const testEvent = await Event.create({ + title: `title${nanoid().toLowerCase()}`, + description: `description${nanoid().toLowerCase()}`, + allDay: true, + startDate: new Date(), + recurring: true, + isPublic: true, + isRegisterable: true, + creator: testUser?._id, + admins: [testUser?._id], + organization: testOrganization?._id, + actionItems: [testActionItem1?._id, testActionItem2?._id], + }); + + await ActionItem.updateOne( + { + _id: testActionItem1?._id, + }, + { + event: testEvent?._id, + } + ); + + await ActionItem.updateOne( + { + _id: testActionItem2?._id, + }, + { + event: testEvent?._id, + } + ); + + return [testUser, testEvent]; +}; diff --git a/tests/resolvers/ActionItem/assignedBy.spec.ts b/tests/resolvers/ActionItem/assignedBy.spec.ts new file mode 100644 index 0000000000..2d6e79bbed --- /dev/null +++ b/tests/resolvers/ActionItem/assignedBy.spec.ts @@ -0,0 +1,36 @@ +import "dotenv/config"; +import { assignedBy as assignedByResolver } from "../../../src/resolvers/ActionItem/assignedBy"; +import { connect, disconnect } from "../../helpers/db"; +import type mongoose from "mongoose"; +import { beforeAll, afterAll, describe, it, expect } from "vitest"; +import { User } from "../../../src/models"; +import type { TestUserType } from "../../helpers/userAndOrg"; +import type { TestActionItemType } from "../../helpers/actionItem"; +import { createTestActionItem } from "../../helpers/actionItem"; + +let MONGOOSE_INSTANCE: typeof mongoose; +let testUser: TestUserType; +let testActionItem: TestActionItemType; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + [testUser, , , testActionItem] = await createTestActionItem(); +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> ActionItem -> assignedBy", () => { + it(`returns the assigner for parent action item`, async () => { + const parent = testActionItem?.toObject(); + + const assignedByPayload = await assignedByResolver?.(parent, {}, {}); + + const assignedByObject = await User.findOne({ + _id: testUser?._id, + }).lean(); + + expect(assignedByPayload).toEqual(assignedByObject); + }); +}); diff --git a/tests/resolvers/ActionItem/assignedTo.spec.ts b/tests/resolvers/ActionItem/assignedTo.spec.ts new file mode 100644 index 0000000000..f5051e0a47 --- /dev/null +++ b/tests/resolvers/ActionItem/assignedTo.spec.ts @@ -0,0 +1,36 @@ +import "dotenv/config"; +import { assignedTo as assignedToResolver } from "../../../src/resolvers/ActionItem/assignedTo"; +import { connect, disconnect } from "../../helpers/db"; +import type mongoose from "mongoose"; +import { beforeAll, afterAll, describe, it, expect } from "vitest"; +import { User } from "../../../src/models"; +import type { TestUserType } from "../../helpers/userAndOrg"; +import type { TestActionItemType } from "../../helpers/actionItem"; +import { createTestActionItem } from "../../helpers/actionItem"; + +let MONGOOSE_INSTANCE: typeof mongoose; +let randomTestUser: TestUserType; +let testActionItem: TestActionItemType; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + [, , , testActionItem, randomTestUser] = await createTestActionItem(); +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> ActionItem -> assignedBy", () => { + it(`returns the assignee for parent action item`, async () => { + const parent = testActionItem?.toObject(); + + const assignedToPayload = await assignedToResolver?.(parent, {}, {}); + + const assignedToObject = await User.findOne({ + _id: randomTestUser?._id, + }).lean(); + + expect(assignedToPayload).toEqual(assignedToObject); + }); +}); diff --git a/tests/resolvers/ActionItem/category.spec.ts b/tests/resolvers/ActionItem/category.spec.ts new file mode 100644 index 0000000000..3e87ab0c43 --- /dev/null +++ b/tests/resolvers/ActionItem/category.spec.ts @@ -0,0 +1,36 @@ +import "dotenv/config"; +import { category as categoryResolver } from "../../../src/resolvers/ActionItem/category"; +import { connect, disconnect } from "../../helpers/db"; +import type mongoose from "mongoose"; +import { beforeAll, afterAll, describe, it, expect } from "vitest"; +import { Category } from "../../../src/models"; +import type { TestActionItemType } from "../../helpers/actionItem"; +import { createTestActionItem } from "../../helpers/actionItem"; +import type { TestCategoryType } from "../../helpers/category"; + +let MONGOOSE_INSTANCE: typeof mongoose; +let testActionItem: TestActionItemType; +let testCategory: TestCategoryType; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + [, , testCategory, testActionItem] = await createTestActionItem(); +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> ActionItem -> category", () => { + it(`returns the category for parent action item`, async () => { + const parent = testActionItem?.toObject(); + + const categoryPayload = await categoryResolver?.(parent, {}, {}); + + const categoryObject = await Category.findOne({ + _id: testCategory?._id, + }).lean(); + + expect(categoryPayload).toEqual(categoryObject); + }); +}); diff --git a/tests/resolvers/ActionItem/createdBy.spec.ts b/tests/resolvers/ActionItem/createdBy.spec.ts new file mode 100644 index 0000000000..574fd5e6e7 --- /dev/null +++ b/tests/resolvers/ActionItem/createdBy.spec.ts @@ -0,0 +1,36 @@ +import "dotenv/config"; +import { createdBy as createdByResolver } from "../../../src/resolvers/ActionItem/createdBy"; +import { connect, disconnect } from "../../helpers/db"; +import type mongoose from "mongoose"; +import { beforeAll, afterAll, describe, it, expect } from "vitest"; +import { User } from "../../../src/models"; +import type { TestUserType } from "../../helpers/userAndOrg"; +import type { TestActionItemType } from "../../helpers/actionItem"; +import { createTestActionItem } from "../../helpers/actionItem"; + +let MONGOOSE_INSTANCE: typeof mongoose; +let testUser: TestUserType; +let testActionItem: TestActionItemType; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + [testUser, , , testActionItem] = await createTestActionItem(); +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> ActionItem -> createdBy", () => { + it(`returns the creator for parent action item`, async () => { + const parent = testActionItem?.toObject(); + + const createdByPayload = await createdByResolver?.(parent, {}, {}); + + const createdByObject = await User.findOne({ + _id: testUser?._id, + }).lean(); + + expect(createdByPayload).toEqual(createdByObject); + }); +}); diff --git a/tests/resolvers/ActionItem/event.spec.ts b/tests/resolvers/ActionItem/event.spec.ts new file mode 100644 index 0000000000..c5911097ad --- /dev/null +++ b/tests/resolvers/ActionItem/event.spec.ts @@ -0,0 +1,72 @@ +import "dotenv/config"; +import { event as eventResolver } from "../../../src/resolvers/ActionItem/event"; +import { connect, disconnect } from "../../helpers/db"; +import type mongoose from "mongoose"; +import { beforeAll, afterAll, describe, it, expect } from "vitest"; +import type { InterfaceActionItem } from "../../../src/models"; +import { ActionItem, Event } from "../../../src/models"; +import type { + TestOrganizationType, + TestUserType, +} from "../../helpers/userAndOrg"; +import type { TestActionItemType } from "../../helpers/actionItem"; +import { createTestActionItem } from "../../helpers/actionItem"; +import { nanoid } from "nanoid"; + +let MONGOOSE_INSTANCE: typeof mongoose; +let testUser: TestUserType; +let testOrganization: TestOrganizationType; +let testActionItem: TestActionItemType; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + [testUser, testOrganization, , testActionItem] = await createTestActionItem(); +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> ActionItem -> event", () => { + it(`returns the event for parent action item`, async () => { + const testEvent = await Event.create({ + title: `title${nanoid().toLowerCase()}`, + description: `description${nanoid().toLowerCase()}`, + allDay: true, + startDate: new Date(), + recurring: true, + isPublic: true, + isRegisterable: true, + creator: testUser?._id, + admins: [testUser?._id], + organization: testOrganization?._id, + actionItems: [testActionItem?._id], + }); + + const updatedTestActionItem = await ActionItem.findOneAndUpdate( + { + _id: testActionItem?._id, + }, + { + event: testEvent?._id, + }, + { + new: true, + } + ); + + const parent = updatedTestActionItem?.toObject(); + + const eventByPayload = await eventResolver?.( + parent as InterfaceActionItem, + {}, + {} + ); + + expect(eventByPayload).toEqual( + expect.objectContaining({ + actionItems: [updatedTestActionItem?._id], + }) + ); + }); +}); diff --git a/tests/resolvers/ActionItem/updatedBy.spec.ts b/tests/resolvers/ActionItem/updatedBy.spec.ts new file mode 100644 index 0000000000..432ab54d3a --- /dev/null +++ b/tests/resolvers/ActionItem/updatedBy.spec.ts @@ -0,0 +1,36 @@ +import "dotenv/config"; +import { updatedBy as updatedByResolver } from "../../../src/resolvers/ActionItem/updatedBy"; +import { connect, disconnect } from "../../helpers/db"; +import type mongoose from "mongoose"; +import { beforeAll, afterAll, describe, it, expect } from "vitest"; +import { User } from "../../../src/models"; +import type { TestUserType } from "../../helpers/userAndOrg"; +import type { TestActionItemType } from "../../helpers/actionItem"; +import { createTestActionItem } from "../../helpers/actionItem"; + +let MONGOOSE_INSTANCE: typeof mongoose; +let testUser: TestUserType; +let testActionItem: TestActionItemType; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + [testUser, , , testActionItem] = await createTestActionItem(); +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> ActionItem -> updatedBy", () => { + it(`returns the updater for parent action item`, async () => { + const parent = testActionItem?.toObject(); + + const updatedByPayload = await updatedByResolver?.(parent, {}, {}); + + const updatedByObject = await User.findOne({ + _id: testUser?._id, + }).lean(); + + expect(updatedByPayload).toEqual(updatedByObject); + }); +}); diff --git a/tests/resolvers/Event/actionItems.spec.ts b/tests/resolvers/Event/actionItems.spec.ts new file mode 100644 index 0000000000..916a437fdd --- /dev/null +++ b/tests/resolvers/Event/actionItems.spec.ts @@ -0,0 +1,39 @@ +import "dotenv/config"; +import { actionItems as actionItemsResolver } from "../../../src/resolvers/Event/actionItems"; +import { connect, disconnect } from "../../helpers/db"; +import type mongoose from "mongoose"; +import { ActionItem } from "../../../src/models"; +import { beforeAll, afterAll, describe, it, expect } from "vitest"; +import type { TestEventType } from "../../helpers/events"; +import { createTestActionItems } from "../../helpers/actionItem"; + +let MONGOOSE_INSTANCE: typeof mongoose; +let testEvent: TestEventType; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + [, testEvent] = await createTestActionItems(); +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> Organization -> actionItems", () => { + it(`returns all actionItems for parent Event`, async () => { + const parent = testEvent?.toObject(); + if (parent) { + const actionCategoriesPayload = await actionItemsResolver?.( + parent, + {}, + {} + ); + + const actionItems = await ActionItem.find({ + event: testEvent?._id, + }).lean(); + + expect(actionCategoriesPayload).toEqual(actionItems); + } + }); +}); diff --git a/tests/resolvers/Mutation/createActionItem.spec.ts b/tests/resolvers/Mutation/createActionItem.spec.ts new file mode 100644 index 0000000000..a163b85367 --- /dev/null +++ b/tests/resolvers/Mutation/createActionItem.spec.ts @@ -0,0 +1,242 @@ +import "dotenv/config"; +import type mongoose from "mongoose"; +import { Types } from "mongoose"; +import type { MutationCreateActionItemArgs } from "../../../src/types/generatedGraphQLTypes"; +import { createActionItem as createActionItemResolver } from "../../../src/resolvers/Mutation/createActionItem"; +import { connect, disconnect } from "../../helpers/db"; +import { + USER_NOT_FOUND_ERROR, + CATEGORY_NOT_FOUND_ERROR, + USER_NOT_AUTHORIZED_ERROR, + EVENT_NOT_FOUND_ERROR, +} from "../../../src/constants"; +import { beforeAll, afterAll, describe, it, expect, vi } from "vitest"; +import { createTestUser } from "../../helpers/userAndOrg"; +import type { + TestOrganizationType, + TestUserType, +} from "../../helpers/userAndOrg"; + +import type { TestCategoryType } from "../../helpers/category"; +import { createTestCategory } from "../../helpers/category"; +import type { TestEventType } from "../../helpers/events"; +import { Event, User } from "../../../src/models"; +import { nanoid } from "nanoid"; + +let randomUser: TestUserType; +let randomUser2: TestUserType; +let superAdminTestUser: TestUserType; +let testUser: TestUserType; +let testOrganization: TestOrganizationType; +let testCategory: TestCategoryType; +let testEvent: TestEventType; +let MONGOOSE_INSTANCE: typeof mongoose; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + const { requestContext } = await import("../../../src/libraries"); + vi.spyOn(requestContext, "translate").mockImplementation( + (message) => message + ); + + randomUser = await createTestUser(); + randomUser2 = await createTestUser(); + + superAdminTestUser = await User.findOneAndUpdate( + { + _id: randomUser2?._id, + }, + { + userType: "SUPERADMIN", + }, + { + new: true, + } + ); + + [testUser, testOrganization, testCategory] = await createTestCategory(); + + testEvent = await Event.create({ + title: `title${nanoid().toLowerCase()}`, + description: `description${nanoid().toLowerCase()}`, + allDay: true, + startDate: new Date(), + recurring: true, + isPublic: true, + isRegisterable: true, + creator: randomUser?._id, + admins: [randomUser?._id], + organization: testOrganization?._id, + }); +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> Mutation -> createActionItem", () => { + it(`throws NotFoundError if no user exists with _id === context.userId`, async () => { + try { + const args: MutationCreateActionItemArgs = { + data: { + assignedTo: randomUser?._id, + }, + categoryId: testCategory?._id, + }; + + const context = { + userId: Types.ObjectId().toString(), + }; + + await createActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(USER_NOT_FOUND_ERROR.MESSAGE); + } + }); + + it(`throws NotFoundError if no category exists with _id === args.orgId`, async () => { + try { + const args: MutationCreateActionItemArgs = { + data: { + assignedTo: randomUser?._id, + }, + categoryId: Types.ObjectId().toString(), + }; + + const context = { + userId: testUser?._id, + }; + + await createActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(CATEGORY_NOT_FOUND_ERROR.MESSAGE); + } + }); + + it(`throws NotFoundError if no event exists with _id === args.data.event`, async () => { + try { + const args: MutationCreateActionItemArgs = { + data: { + assignedTo: randomUser?._id, + event: Types.ObjectId().toString(), + }, + categoryId: testCategory?._id, + }; + + const context = { + userId: randomUser?._id, + }; + + await createActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(EVENT_NOT_FOUND_ERROR.MESSAGE); + } + }); + + it(`throws NotAuthorizedError if the user is not authorized for performing the operation`, async () => { + try { + const args: MutationCreateActionItemArgs = { + data: { + assignedTo: randomUser?._id, + }, + categoryId: testCategory?._id, + }; + + const context = { + userId: randomUser?._id, + }; + + await createActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(USER_NOT_AUTHORIZED_ERROR.MESSAGE); + } + }); + + it(`creates the actionItem when user is authorized as an eventAdmin`, async () => { + const args: MutationCreateActionItemArgs = { + data: { + assignedTo: randomUser?._id, + event: testEvent?._id, + }, + categoryId: testCategory?._id, + }; + + const context = { + userId: randomUser?._id, + }; + + const createActionItemPayload = await createActionItemResolver?.( + {}, + args, + context + ); + + expect(createActionItemPayload).toEqual( + expect.objectContaining({ + category: testCategory?._id, + }) + ); + + const updatedTestEvent = await Event.findOne({ + _id: testEvent?._id, + }) + .select(["actionItems"]) + .lean(); + + expect(updatedTestEvent).toEqual( + expect.objectContaining({ + actionItems: expect.arrayContaining([createActionItemPayload?._id]), + }) + ); + }); + + it(`creates the actionItem when user is authorized as an orgAdmin`, async () => { + const args: MutationCreateActionItemArgs = { + data: { + assignedTo: randomUser?._id, + }, + categoryId: testCategory?._id, + }; + + const context = { + userId: testUser?._id, + }; + + const createActionItemPayload = await createActionItemResolver?.( + {}, + args, + context + ); + + expect(createActionItemPayload).toEqual( + expect.objectContaining({ + category: testCategory?._id, + }) + ); + }); + + it(`creates the actionItem when user is authorized as superadmin`, async () => { + const args: MutationCreateActionItemArgs = { + data: { + assignedTo: randomUser?._id, + }, + categoryId: testCategory?._id, + }; + + const context = { + userId: superAdminTestUser?._id, + }; + + const createActionItemPayload = await createActionItemResolver?.( + {}, + args, + context + ); + + expect(createActionItemPayload).toEqual( + expect.objectContaining({ + category: testCategory?._id, + }) + ); + }); +}); diff --git a/tests/resolvers/Mutation/createCategory.spec.ts b/tests/resolvers/Mutation/createCategory.spec.ts index d06d68df39..af681f5ed1 100644 --- a/tests/resolvers/Mutation/createCategory.spec.ts +++ b/tests/resolvers/Mutation/createCategory.spec.ts @@ -19,7 +19,7 @@ import type { TestUserType, } from "../../helpers/userAndOrg"; -import { Organization } from "../../../src/models"; +import { Organization, User } from "../../../src/models"; let randomUser: TestUserType; let testUser: TestUserType; @@ -94,7 +94,7 @@ describe("resolvers -> Mutation -> createCategory", () => { } }); - it(`creates the category and returns it`, async () => { + it(`creates the category and returns it as an admin`, async () => { const args: MutationCreateCategoryArgs = { orgId: testOrganization?._id, category: "Default", @@ -129,4 +129,52 @@ describe("resolvers -> Mutation -> createCategory", () => { }) ); }); + + it(`creates the category and returns it as superAdmin`, async () => { + const superAdminTestUser = await User.findOneAndUpdate( + { + _id: randomUser?._id, + }, + { + userType: "SUPERADMIN", + }, + { + new: true, + } + ); + + const args: MutationCreateCategoryArgs = { + orgId: testOrganization?._id, + category: "Default", + }; + + const context = { + userId: superAdminTestUser?._id, + }; + + const createCategoryPayload = await createCategoryResolver?.( + {}, + args, + context + ); + + expect(createCategoryPayload).toEqual( + expect.objectContaining({ + org: testOrganization?._id, + category: "Default", + }) + ); + + const updatedTestOrganization = await Organization.findOne({ + _id: testOrganization?._id, + }) + .select(["actionCategories"]) + .lean(); + + expect(updatedTestOrganization).toEqual( + expect.objectContaining({ + actionCategories: expect.arrayContaining([createCategoryPayload?._id]), + }) + ); + }); }); diff --git a/tests/resolvers/Mutation/removeActionItem.spec.ts b/tests/resolvers/Mutation/removeActionItem.spec.ts new file mode 100644 index 0000000000..8bd74946df --- /dev/null +++ b/tests/resolvers/Mutation/removeActionItem.spec.ts @@ -0,0 +1,291 @@ +import "dotenv/config"; +import type mongoose from "mongoose"; +import { Types } from "mongoose"; +import type { MutationRemoveActionItemArgs } from "../../../src/types/generatedGraphQLTypes"; +import { connect, disconnect } from "../../helpers/db"; +import { + USER_NOT_FOUND_ERROR, + ACTION_ITEM_NOT_FOUND_ERROR, + USER_NOT_AUTHORIZED_ERROR, + EVENT_NOT_FOUND_ERROR, +} from "../../../src/constants"; +import { beforeAll, afterAll, describe, it, expect, vi } from "vitest"; +import { + createTestUser, + createTestUserAndOrganization, +} from "../../helpers/userAndOrg"; +import { removeActionItem as removeActionItemResolver } from "../../../src/resolvers/Mutation/removeActionItem"; +import type { + TestOrganizationType, + TestUserType, +} from "../../helpers/userAndOrg"; + +import type { TestCategoryType } from "../../helpers/category"; +import { ActionItem, Event, User } from "../../../src/models"; +import type { TestActionItemType } from "../../helpers/actionItem"; +import { + createNewTestActionItem, + createTestActionItem, +} from "../../helpers/actionItem"; +import type { TestEventType } from "../../helpers/events"; +import { nanoid } from "nanoid"; + +let randomUser: TestUserType; +let assignedTestUser: TestUserType; +let testUser: TestUserType; +let testUser2: TestUserType; +let testOrganization: TestOrganizationType; +let testCategory: TestCategoryType; +let testActionItem: TestActionItemType; +let testEvent: TestEventType; +let MONGOOSE_INSTANCE: typeof mongoose; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + const { requestContext } = await import("../../../src/libraries"); + vi.spyOn(requestContext, "translate").mockImplementation( + (message) => message + ); + + randomUser = await createTestUser(); + + [testUser2] = await createTestUserAndOrganization(); + [testUser, testOrganization, testCategory, testActionItem, assignedTestUser] = + await createTestActionItem(); + + testEvent = await Event.create({ + title: `title${nanoid().toLowerCase()}`, + description: `description${nanoid().toLowerCase()}`, + allDay: true, + startDate: new Date(), + recurring: true, + isPublic: true, + isRegisterable: true, + creator: testUser2?._id, + admins: [testUser2?._id], + organization: testOrganization?._id, + }); +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> Mutation -> removeActionItem", () => { + it(`throws NotFoundError if no user exists with _id === context.userId`, async () => { + try { + const args: MutationRemoveActionItemArgs = { + id: Types.ObjectId().toString(), + }; + + const context = { + userId: Types.ObjectId().toString(), + }; + + await removeActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(USER_NOT_FOUND_ERROR.MESSAGE); + } + }); + + it(`throws NotFoundError if no action item exists with _id === args.id`, async () => { + try { + const args: MutationRemoveActionItemArgs = { + id: Types.ObjectId().toString(), + }; + + const context = { + userId: testUser?._id, + }; + + await removeActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(ACTION_ITEM_NOT_FOUND_ERROR.MESSAGE); + } + }); + + it(`throws NotAuthorizedError if the user is not a superadmin/orgAdmin/eventAdmin`, async () => { + try { + const args: MutationRemoveActionItemArgs = { + id: testActionItem?._id, + }; + + const context = { + userId: testUser2?._id, + }; + + await removeActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(USER_NOT_AUTHORIZED_ERROR.MESSAGE); + } + }); + + it(`removes the action item and returns it as an admin`, async () => { + const args: MutationRemoveActionItemArgs = { + id: testActionItem?._id, + }; + + const context = { + userId: testUser?._id, + }; + + const removedActionItemPayload = await removeActionItemResolver?.( + {}, + args, + context + ); + + // console.log(removedActionItemPayload); + expect(removedActionItemPayload).toEqual( + expect.objectContaining({ + assignedTo: assignedTestUser?._id, + }) + ); + }); + + it(`removes the action item and returns it as superadmin`, async () => { + const newTestActionItem = await createNewTestActionItem({ + currUserId: testUser?._id, + assignedUserId: randomUser?._id, + categoryId: testCategory?._id, + }); + + const superAdminTestUser = await User.findOneAndUpdate( + { + _id: randomUser?._id, + }, + { + userType: "SUPERADMIN", + }, + { + new: true, + } + ); + + const args: MutationRemoveActionItemArgs = { + id: newTestActionItem?._id, + }; + + const context = { + userId: superAdminTestUser?._id, + }; + + const removedActionItemPayload = await removeActionItemResolver?.( + {}, + args, + context + ); + + expect(removedActionItemPayload).toEqual( + expect.objectContaining({ + assignedTo: randomUser?._id, + }) + ); + }); + + it(`throws NotFoundError if no event exists to which the action item is associated`, async () => { + const newTestActionItem = await createNewTestActionItem({ + currUserId: testUser?._id, + assignedUserId: randomUser?._id, + categoryId: testCategory?._id, + }); + + const updatedTestActionItem = await ActionItem.findOneAndUpdate( + { + _id: newTestActionItem?._id, + }, + { + event: Types.ObjectId().toString(), + }, + { + new: true, + } + ); + + try { + const args: MutationRemoveActionItemArgs = { + id: updatedTestActionItem?._id, + }; + + const context = { + userId: testUser?._id, + }; + + await removeActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(EVENT_NOT_FOUND_ERROR.MESSAGE); + } + }); + + it(`removes the actionItem when the user is authorized as an eventAdmin`, async () => { + const newTestActionItem = await createNewTestActionItem({ + currUserId: testUser?._id, + assignedUserId: randomUser?._id, + categoryId: testCategory?._id, + }); + + const updatedTestActionItem = await ActionItem.findOneAndUpdate( + { + _id: newTestActionItem?._id, + }, + { + event: testEvent?._id, + }, + { + new: true, + } + ); + + const updatedTestEvent = await Event.findOneAndUpdate( + { + _id: testEvent?._id, + }, + { + $push: { actionItems: newTestActionItem?._id }, + }, + { + new: true, + } + ) + .select(["actionItems"]) + .lean(); + + expect(updatedTestEvent).toEqual( + expect.objectContaining({ + actionItems: expect.arrayContaining([updatedTestActionItem?._id]), + }) + ); + + const args: MutationRemoveActionItemArgs = { + id: updatedTestActionItem?._id, + }; + + const context = { + userId: testUser2?._id, + }; + + const removedActionItemPayload = await removeActionItemResolver?.( + {}, + args, + context + ); + + expect(removedActionItemPayload).toEqual( + expect.objectContaining({ + assignedTo: randomUser?._id, + }) + ); + + const updatedTestEventInfo = await Event.findOne({ + _id: testEvent?._id, + }) + .select(["actionItems"]) + .lean(); + + expect(updatedTestEventInfo).toEqual( + expect.objectContaining({ + actionItems: expect.not.arrayContaining([updatedTestActionItem?._id]), + }) + ); + }); +}); diff --git a/tests/resolvers/Mutation/removeEvent.spec.ts b/tests/resolvers/Mutation/removeEvent.spec.ts index 120492713f..d54f94801d 100644 --- a/tests/resolvers/Mutation/removeEvent.spec.ts +++ b/tests/resolvers/Mutation/removeEvent.spec.ts @@ -1,7 +1,7 @@ import "dotenv/config"; import type mongoose from "mongoose"; import { Types } from "mongoose"; -import { User, Event } from "../../../src/models"; +import { User, Event, ActionItem } from "../../../src/models"; import type { MutationRemoveEventArgs } from "../../../src/types/generatedGraphQLTypes"; import { connect, disconnect } from "../../helpers/db"; @@ -19,11 +19,14 @@ import type { import type { TestEventType } from "../../helpers/events"; import { createTestEvent } from "../../helpers/events"; import { cacheEvents } from "../../../src/services/EventCache/cacheEvents"; +import { createTestActionItems } from "../../helpers/actionItem"; let MONGOOSE_INSTANCE: typeof mongoose; let testUser: TestUserType; +let newTestUser: TestUserType; let testOrganization: TestOrganizationType; let testEvent: TestEventType; +let newTestEvent: TestEventType; beforeAll(async () => { MONGOOSE_INSTANCE = await connect(); @@ -196,4 +199,26 @@ describe("resolvers -> Mutation -> removeEvent", () => { expect(updatedTestEvent?.status).toEqual("DELETED"); }); + + it(`removes the events and all action items assiciated with it`, async () => { + [newTestUser, newTestEvent] = await createTestActionItems(); + + const args: MutationRemoveEventArgs = { + id: newTestEvent?.id, + }; + + const context = { + userId: newTestUser?.id, + }; + + const removeEventPayload = await removeEventResolver?.({}, args, context); + + expect(removeEventPayload).toEqual(newTestEvent?.toObject()); + + const deletedActionItems = await ActionItem.find({ + event: newTestEvent?._id, + }); + + expect(deletedActionItems).toEqual([]); + }); }); diff --git a/tests/resolvers/Mutation/removeOrganization.spec.ts b/tests/resolvers/Mutation/removeOrganization.spec.ts index 53a6d28fea..00359a5455 100644 --- a/tests/resolvers/Mutation/removeOrganization.spec.ts +++ b/tests/resolvers/Mutation/removeOrganization.spec.ts @@ -6,6 +6,8 @@ import type { InterfaceOrganization, InterfaceComment, InterfacePost, + InterfaceCategory, + InterfaceActionItem, } from "../../../src/models"; import { User, @@ -14,6 +16,7 @@ import { Comment, MembershipRequest, Category, + ActionItem, } from "../../../src/models"; import type { MutationRemoveOrganizationArgs } from "../../../src/types/generatedGraphQLTypes"; import { connect, disconnect } from "../../helpers/db"; @@ -43,6 +46,8 @@ let testOrganization: InterfaceOrganization & Document; let testPost: InterfacePost & Document; let testComment: InterfaceComment & Document; +let testCategory: InterfaceCategory & Document; +let testActionItem: InterfaceActionItem & Document; beforeAll(async () => { MONGOOSE_INSTANCE = await connect(); @@ -107,13 +112,21 @@ beforeAll(async () => { organization: testOrganization._id, }); - const testCategory = await Category.create({ + testCategory = await Category.create({ createdBy: testUsers[0]?._id, updatedBy: testUsers[0]?._id, org: testOrganization?._id, category: "Default", }); + testActionItem = await ActionItem.create({ + createdBy: testUsers[0]?._id, + updatedBy: testUsers[0]?._id, + assignedTo: testUsers[1]?._id, + assignedBy: testUsers[0]?._id, + category: testCategory?._id, + }); + await Organization.updateOne( { _id: testOrganization._id, @@ -334,6 +347,10 @@ describe("resolvers -> Mutation -> removeOrganization", () => { org: testOrganization?._id, }).lean(); + const deteledTestActionItems = await ActionItem.find({ + _id: testActionItem?._id, + }); + expect(deletedMembershipRequests).toEqual([]); expect(deletedTestPosts).toEqual([]); @@ -341,6 +358,8 @@ describe("resolvers -> Mutation -> removeOrganization", () => { expect(deletedTestComments).toEqual([]); expect(deletedTestCategories).toEqual([]); + + expect(deteledTestActionItems).toEqual([]); }); it(`removes the organization with image and returns the updated user's object with _id === context.userId`, async () => { diff --git a/tests/resolvers/Mutation/updateActionItem.spec.ts b/tests/resolvers/Mutation/updateActionItem.spec.ts new file mode 100644 index 0000000000..66b6997ad2 --- /dev/null +++ b/tests/resolvers/Mutation/updateActionItem.spec.ts @@ -0,0 +1,261 @@ +import "dotenv/config"; +import type mongoose from "mongoose"; +import { Types } from "mongoose"; +import type { MutationUpdateActionItemArgs } from "../../../src/types/generatedGraphQLTypes"; +import { connect, disconnect } from "../../helpers/db"; +import { + USER_NOT_FOUND_ERROR, + ACTION_ITEM_NOT_FOUND_ERROR, + USER_NOT_AUTHORIZED_ERROR, + EVENT_NOT_FOUND_ERROR, +} from "../../../src/constants"; +import { beforeAll, afterAll, describe, it, expect, vi } from "vitest"; +import { + createTestUser, + createTestUserAndOrganization, +} from "../../helpers/userAndOrg"; +import { updateActionItem as updateActionItemResolver } from "../../../src/resolvers/Mutation/updateActionItem"; +import type { + TestOrganizationType, + TestUserType, +} from "../../helpers/userAndOrg"; + +import type { TestCategoryType } from "../../helpers/category"; +import { ActionItem, Event, User } from "../../../src/models"; +import type { TestActionItemType } from "../../helpers/actionItem"; +import { createTestActionItem } from "../../helpers/actionItem"; +import type { TestEventType } from "../../helpers/events"; +import { nanoid } from "nanoid"; + +let randomUser: TestUserType; +let assignedTestUser: TestUserType; +let testUser: TestUserType; +let testUser2: TestUserType; +let testOrganization: TestOrganizationType; +let testCategory: TestCategoryType; +let testActionItem: TestActionItemType; +let testEvent: TestEventType; +let MONGOOSE_INSTANCE: typeof mongoose; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + const { requestContext } = await import("../../../src/libraries"); + vi.spyOn(requestContext, "translate").mockImplementation( + (message) => message + ); + + randomUser = await createTestUser(); + + [testUser2] = await createTestUserAndOrganization(); + [testUser, testOrganization, testCategory, testActionItem, assignedTestUser] = + await createTestActionItem(); + + testEvent = await Event.create({ + title: `title${nanoid().toLowerCase()}`, + description: `description${nanoid().toLowerCase()}`, + allDay: true, + startDate: new Date(), + recurring: true, + isPublic: true, + isRegisterable: true, + creator: testUser2?._id, + admins: [testUser2?._id], + organization: testOrganization?._id, + }); +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> Mutation -> updateActionItem", () => { + it(`throws NotFoundError if no user exists with _id === context.userId`, async () => { + try { + const args: MutationUpdateActionItemArgs = { + id: Types.ObjectId().toString(), + data: { + assignedTo: randomUser?._id, + }, + }; + + const context = { + userId: Types.ObjectId().toString(), + }; + + await updateActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(USER_NOT_FOUND_ERROR.MESSAGE); + } + }); + + it(`throws NotFoundError if no action item exists with _id === args.id`, async () => { + try { + const args: MutationUpdateActionItemArgs = { + id: Types.ObjectId().toString(), + data: { + assignedTo: randomUser?._id, + }, + }; + + const context = { + userId: testUser?._id, + }; + + await updateActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(ACTION_ITEM_NOT_FOUND_ERROR.MESSAGE); + } + }); + + it(`throws NotAuthorizedError if the user is not a superadmin/orgAdmin/eventAdmin`, async () => { + try { + const args: MutationUpdateActionItemArgs = { + id: testActionItem?._id, + data: { + assignedTo: randomUser?._id, + }, + }; + + const context = { + userId: testUser2?._id, + }; + + await updateActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(USER_NOT_AUTHORIZED_ERROR.MESSAGE); + } + }); + + it(`updates the action item and returns it as an admin`, async () => { + const args: MutationUpdateActionItemArgs = { + id: testActionItem?._id, + data: { + assignedTo: assignedTestUser?._id, + }, + }; + + const context = { + userId: testUser?._id, + }; + + const updatedActionItemPayload = await updateActionItemResolver?.( + {}, + args, + context + ); + + expect(updatedActionItemPayload).toEqual( + expect.objectContaining({ + assignedTo: assignedTestUser?._id, + category: testCategory?._id, + }) + ); + }); + + it(`updates the action item and returns it as superadmin`, async () => { + const superAdminTestUser = await User.findOneAndUpdate( + { + _id: randomUser?._id, + }, + { + userType: "SUPERADMIN", + }, + { + new: true, + } + ); + + const args: MutationUpdateActionItemArgs = { + id: testActionItem?._id, + data: { + assignedTo: testUser?._id, + }, + }; + + const context = { + userId: superAdminTestUser?._id, + }; + + const updatedActionItemPayload = await updateActionItemResolver?.( + {}, + args, + context + ); + + expect(updatedActionItemPayload).toEqual( + expect.objectContaining({ + assignedTo: testUser?._id, + category: testCategory?._id, + }) + ); + }); + + it(`throws NotFoundError if no event exists to which the action item is associated`, async () => { + const updatedTestActionItem = await ActionItem.findOneAndUpdate( + { + _id: testActionItem?._id, + }, + { + event: Types.ObjectId().toString(), + }, + { + new: true, + } + ); + + try { + const args: MutationUpdateActionItemArgs = { + id: updatedTestActionItem?._id, + data: { + assignedTo: randomUser?._id, + }, + }; + + const context = { + userId: testUser?._id, + }; + + await updateActionItemResolver?.({}, args, context); + } catch (error: any) { + expect(error.message).toEqual(EVENT_NOT_FOUND_ERROR.MESSAGE); + } + }); + + it(`updates the actionItem when the user is authorized as an eventAdmin`, async () => { + const updatedTestActionItem = await ActionItem.findOneAndUpdate( + { + _id: testActionItem?._id, + }, + { + event: testEvent?._id, + }, + { + new: true, + } + ); + + const args: MutationUpdateActionItemArgs = { + data: { + assignedTo: testUser?._id, + }, + id: updatedTestActionItem?._id, + }; + + const context = { + userId: testUser2?._id, + }; + + const updatedActionItemPayload = await updateActionItemResolver?.( + {}, + args, + context + ); + + expect(updatedActionItemPayload).toEqual( + expect.objectContaining({ + category: testCategory?._id, + assignedTo: testUser?._id, + }) + ); + }); +}); diff --git a/tests/resolvers/Mutation/updateCategory.spec.ts b/tests/resolvers/Mutation/updateCategory.spec.ts index 4229844546..84b73087e5 100644 --- a/tests/resolvers/Mutation/updateCategory.spec.ts +++ b/tests/resolvers/Mutation/updateCategory.spec.ts @@ -18,6 +18,7 @@ import type { import { updateCategory as updateCategoryResolver } from "../../../src/resolvers/Mutation/updateCategory"; import type { TestCategoryType } from "../../helpers/category"; import { createTestCategory } from "../../helpers/category"; +import { User } from "../../../src/models"; let randomUser: TestUserType; let testUser: TestUserType; @@ -102,7 +103,7 @@ describe("resolvers -> Mutation -> updateCategoryResolver", () => { } }); - it(`updated the category and returns it`, async () => { + it(`updated the category and returns it as an admin`, async () => { const args: MutationUpdateCategoryArgs = { id: testCategory?._id, data: { @@ -125,4 +126,40 @@ describe("resolvers -> Mutation -> updateCategoryResolver", () => { }) ); }); + + it(`updated the category and returns it as superadmin`, async () => { + const superAdminTestUser = await User.findOneAndUpdate( + { + _id: randomUser?._id, + }, + { + userType: "SUPERADMIN", + }, + { + new: true, + } + ); + + const args: MutationUpdateCategoryArgs = { + id: testCategory?._id, + data: { + category: "updatedDefault", + disabled: false, + }, + }; + + const context = { + userId: superAdminTestUser?._id, + }; + + const updatedCategory = await updateCategoryResolver?.({}, args, context); + + expect(updatedCategory).toEqual( + expect.objectContaining({ + org: testOrganization?._id, + category: "updatedDefault", + disabled: false, + }) + ); + }); }); diff --git a/tests/resolvers/Query/actionItem.spec.ts b/tests/resolvers/Query/actionItem.spec.ts new file mode 100644 index 0000000000..88dbbc4d42 --- /dev/null +++ b/tests/resolvers/Query/actionItem.spec.ts @@ -0,0 +1,51 @@ +import "dotenv/config"; +import { actionItem as actionItemResolver } from "../../../src/resolvers/Query/actionItem"; +import { connect, disconnect } from "../../helpers/db"; +import type mongoose from "mongoose"; +import { Types } from "mongoose"; +import { ACTION_ITEM_NOT_FOUND_ERROR } from "../../../src/constants"; +import type { QueryActionItemArgs } from "../../../src/types/generatedGraphQLTypes"; +import { beforeAll, afterAll, describe, it, expect } from "vitest"; +import type { TestActionItemType } from "../../helpers/actionItem"; +import { createTestActionItem } from "../../helpers/actionItem"; + +let MONGOOSE_INSTANCE: typeof mongoose; +let testActionItem: TestActionItemType; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + const resultArray = await createTestActionItem(); + testActionItem = resultArray[3]; +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> Query -> actionItem", () => { + it(`throws NotFoundError if no actionItem exists with _id === args.id`, async () => { + try { + const args: QueryActionItemArgs = { + id: Types.ObjectId().toString(), + }; + + await actionItemResolver?.({}, args, {}); + } catch (error: any) { + expect(error.message).toEqual(ACTION_ITEM_NOT_FOUND_ERROR.DESC); + } + }); + + it(`returns action item with _id === args.id`, async () => { + const args: QueryActionItemArgs = { + id: testActionItem?._id, + }; + + const actionItemPayload = await actionItemResolver?.({}, args, {}); + + expect(actionItemPayload).toEqual( + expect.objectContaining({ + _id: testActionItem?._id, + }) + ); + }); +}); diff --git a/tests/resolvers/Query/actionItemsByEvents.spec.ts b/tests/resolvers/Query/actionItemsByEvents.spec.ts new file mode 100644 index 0000000000..e9c893e457 --- /dev/null +++ b/tests/resolvers/Query/actionItemsByEvents.spec.ts @@ -0,0 +1,41 @@ +import "dotenv/config"; +import { ActionItem } from "../../../src/models"; +import { connect, disconnect } from "../../helpers/db"; +import type { QueryActionItemsByEventsArgs } from "../../../src/types/generatedGraphQLTypes"; +import { actionItemsByEvents as actionItemsByEventsResolver } from "../../../src/resolvers/Query/actionItemsByEvents"; +import { beforeAll, afterAll, describe, it, expect } from "vitest"; +import type mongoose from "mongoose"; +import { createTestActionItems } from "../../helpers/actionItem"; +import type { TestEventType } from "../../helpers/events"; + +let MONGOOSE_INSTANCE: typeof mongoose; +let testEvent: TestEventType; + +beforeAll(async () => { + MONGOOSE_INSTANCE = await connect(); + [, testEvent] = await createTestActionItems(); +}); + +afterAll(async () => { + await disconnect(MONGOOSE_INSTANCE); +}); + +describe("resolvers -> Query -> actionItemsByEvent", () => { + it(`returns list of all action items associated with an event`, async () => { + const args: QueryActionItemsByEventsArgs = { + eventId: testEvent?._id, + }; + + const actionItemsByEventPayload = await actionItemsByEventsResolver?.( + {}, + args, + {} + ); + + const actionItemsByEventInfo = await ActionItem.find({ + event: testEvent?._id, + }).lean(); + + expect(actionItemsByEventPayload).toEqual(actionItemsByEventInfo); + }); +});