Skip to content

Commit

Permalink
Add tests for Action Items
Browse files Browse the repository at this point in the history
  • Loading branch information
meetulr committed Jan 7, 2024
1 parent 1fe4c9f commit 991ea31
Show file tree
Hide file tree
Showing 23 changed files with 1,450 additions and 5 deletions.
2 changes: 2 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions locales/hi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"user.alreadyMember": "उपयोगकर्ता पहले से ही एक सदस्य है",
"user.profileImage.notFound": "उपयोगकर्ता प्रोफ़ाइल छवि नहीं मिली",
"task.notFound": "कार्य नहीं मिला",
"category.notFound": "श्रेणी नहीं मिली",
"actionItem.notFound": "कार्रवाई का मद नहीं मिला",
"advertisement.notFound": "विज्ञापन नहीं मिला",
"event.notFound": "घटना नहीं मिली",
"eventProject.notFound": "इवेंट प्रोजेक्ट नहीं मिला",
Expand Down
2 changes: 2 additions & 0 deletions locales/sp.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"user.alreadyMember": "用戶已經是會員",
"user.profileImage.notFound": "未找到用戶個人資料圖像",
"task.notFound": "找不到任務",
"category.notFound": "找不到类别",
"actionItem.notFound": "找不到操作项",
"event.notFound": "未找到事件",
"eventProject.notFound": "未找到事件項目",
"organization.notFound": "未找到組織",
Expand Down
5 changes: 5 additions & 0 deletions src/resolvers/Mutation/updateActionItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -138,6 +142,7 @@ export const updateActionItem: MutationResolvers["updateActionItem"] = async (
...(args.data as UpdateActionItemInputType),
assignmentDate: updatedAssignmentDate,
updatedBy: context.userId,
assignedBy: updatedAssignedBy,
},
{
new: true,
Expand Down
124 changes: 124 additions & 0 deletions tests/helpers/actionItem.ts
Original file line number Diff line number Diff line change
@@ -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<TestActionItemType> => {
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];
};
36 changes: 36 additions & 0 deletions tests/resolvers/ActionItem/assignedBy.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
36 changes: 36 additions & 0 deletions tests/resolvers/ActionItem/assignedTo.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
36 changes: 36 additions & 0 deletions tests/resolvers/ActionItem/category.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
36 changes: 36 additions & 0 deletions tests/resolvers/ActionItem/createdBy.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading

0 comments on commit 991ea31

Please sign in to comment.