Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added necessary tests for note specific queries
Browse files Browse the repository at this point in the history
Atharva-Kanherkar committed Apr 22, 2024

Verified

This commit was signed with the committer’s verified signature.
insunaa insuna
1 parent 5f4d02c commit 3c64ea3
Showing 4 changed files with 225 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tests/resolvers/Mutation/deleteNote.spec.ts
Original file line number Diff line number Diff line change
@@ -101,7 +101,7 @@ afterAll(async () => {
await disconnect(MONGOOSE_INSTANCE);
});

describe("resolvers -> Mutation -> removeAgendaItem", () => {
describe("resolvers -> Mutation -> deleteNote", () => {
it("throws unknown error if no user exists with _id === userId", async () => {
try {
const args: MutationDeleteNoteArgs = { id: testNote?._id.toString() };
2 changes: 1 addition & 1 deletion tests/resolvers/Mutation/updateNote.spec.ts
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ afterAll(async () => {
await disconnect(MONGOOSE_INSTANCE);
});

describe("resolvers -> Mutation -> updateAgendaItem", () => {
describe("resolvers -> Mutation -> updateNote", () => {
it("throws NotFoundError if no user exists with _id === userID", async () => {
try {
const args: MutationUpdateNoteArgs = {
113 changes: 113 additions & 0 deletions tests/resolvers/Query/getAllNotesByAgendaItem.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import type mongoose from "mongoose";
import { Types } from "mongoose";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import {
AgendaItemModel,
NoteModel,
Organization,
Event,
} from "../../../src/models";
import { getAllNotesForAgendaItem } from "../../../src/resolvers/Query/getAllNotesForAgendaItem";
import { connect, disconnect } from "../../helpers/db";
import type { TestOrganizationType} from "../../helpers/userAndOrg";
import { createTestUser } from "../../helpers/userAndOrg";
import type { TestUserType } from "../../helpers/user";
import type { TestNoteType } from "../../helpers/note";
import type { TestEventType } from "../../helpers/events";
import type { TestAgendaItemType } from "../../helpers/agendaItem";

let MONGOOSE_INSTANCE: typeof mongoose;
let testUser: TestUserType;
let testAgendaItem: TestAgendaItemType;
let testOrganization: TestOrganizationType;
let testEvent: TestEventType;
let testAdminUser: TestUserType;
let testNote: TestNoteType;
let testNote2: TestNoteType;

beforeAll(async () => {
MONGOOSE_INSTANCE = await connect();
testUser = await createTestUser();
testAdminUser = await createTestUser();
testOrganization = await Organization.create({
name: "name",
description: "description",
isPublic: true,
creator: testUser?._id,
admins: [testAdminUser?._id],
members: [testUser?._id, testAdminUser?._id],
creatorId: testUser?._id,
});
testEvent = await Event.create({
title: "title",
description: "description",
allDay: true,
startDate: new Date(),
recurring: true,
isPublic: true,
isRegisterable: true,
creator: testUser?._id,
admins: [testAdminUser?._id],
registrants: [],
organization: testOrganization?._id,
creatorId: testUser?._id,
});

testAgendaItem = await AgendaItemModel.create({
title: "Test Agenda Item",
description: "This is a test agenda item",
duration: "2 hours",
createdAt: Date.now(),
updatedAt: Date.now(),
attachments: ["attachment1", "attachment2"],
relatedEvent: testEvent?._id,
createdBy: testAdminUser?._id,
urls: ["url1", "url2"],
user: "testuser",
categories: [],
sequence: 1,
itemType: "Regular",
organization: testOrganization?._id,
isNote: false,
});
testNote = (await NoteModel.create({
createdBy: testAdminUser?._id,
updatedBy: testAdminUser?._id,
updatedAt: Date.now(),
createdAt: Date.now(),
content: "Test Note Content",
agendaItemId: testAgendaItem?._id,
})) as unknown as TestNoteType;
testNote2 = (await NoteModel.create({
content: "Test Note 2",
agendaItemId: testAgendaItem?._id,
createdBy: testAdminUser?._id,
updatedBy: testAdminUser?._id,
updatedAt: Date.now(),
createdAt: Date.now(),
})) as unknown as TestNoteType;
console.log(testNote);
console.log(testNote2);
});

afterAll(async () => {
await disconnect(MONGOOSE_INSTANCE);
});

describe("resolvers -> Query -> getAllNotesForAgendaItem", () => {
it("returns an empty array if no notes exist for the provided agendaItemId", async () => {
const args = {
agendaItemId: new Types.ObjectId().toString(),
};
const notes = await getAllNotesForAgendaItem?.({}, args, {});
expect(notes).toEqual([]);
});

it("returns all notes for the provided agendaItemId", async () => {
const args = {
agendaItemId: testAgendaItem?._id,
};
const notes = await getAllNotesForAgendaItem?.({}, args, {});
expect(notes?.length).toEqual(2);
});
});
110 changes: 110 additions & 0 deletions tests/resolvers/Query/getNoteById.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import type mongoose from "mongoose";
import { Types } from "mongoose";
import { connect, disconnect } from "../../helpers/db";
import {
AgendaItemModel,
Organization,
Event,
NoteModel,
} from "../../../src/models";
import { NOTE_NOT_FOUND_ERROR } from "../../../src/constants";
import { beforeAll, afterAll, describe, it, expect } from "vitest";
import type {
TestOrganizationType,
TestUserType,
} from "../../helpers/userAndOrg";
import { createTestUser } from "../../helpers/userAndOrg";
import type { TestEventType } from "../../helpers/events";
import type { TestAgendaItemType } from "../../helpers/agendaItem";
import { getNoteById } from "../../../src/resolvers/Query/getNoteById";
import type { TestNoteType } from "../../helpers/note";

let testUser: TestUserType;
let testAdminUser: TestUserType;
let testOrganization: TestOrganizationType;
let testEvent: TestEventType;
let testAgendaItem: TestAgendaItemType;
let MONGOOSE_INSTANCE: typeof mongoose;
let testNote: TestNoteType;
beforeAll(async () => {
MONGOOSE_INSTANCE = await connect();
testUser = await createTestUser();
testAdminUser = await createTestUser();
testOrganization = await Organization.create({
name: "name",
description: "description",
isPublic: true,
creator: testUser?._id,
creatorId: testUser?._id,
admins: [testAdminUser?._id],
members: [testUser?._id, testAdminUser?._id],
});
testEvent = await Event.create({
title: "title",
description: "description",
allDay: true,
startDate: new Date(),
recurring: true,
isPublic: true,
isRegisterable: true,
creator: testUser?._id,
creatorId: testUser?._id,
admins: [testAdminUser?._id],
registrants: [],
organization: testOrganization?._id,
});
testAgendaItem = await AgendaItemModel.create({
title: "Test Agenda Item",
description: "This is a test agenda item",
duration: "2 hours",
createdAt: Date.now(),
updatedAt: Date.now(),
attachments: ["attachment1", "attachment2"],
relatedEvent: testEvent?._id,
createdBy: testAdminUser?._id,
urls: ["url1", "url2"],
user: "testuser",
categories: [],
sequence: 1,
itemType: "Regular",
organization: testOrganization?._id,
isNote: false,
});
testNote = (await NoteModel.create({
createdBy: testAdminUser?._id,
updatedBy: testAdminUser?._id,
updatedAt: Date.now(),
createdAt: Date.now(),
content: "Test Note Content",
agendaItemId: testAgendaItem?._id,
})) as unknown as TestNoteType;
});

afterAll(async () => {
await disconnect(MONGOOSE_INSTANCE);
});

describe("resolvers -> Query -> getNoteById", () => {
it("throws NotFoundError if no note exists with the given ID", async () => {
try {
const args = {
id: new Types.ObjectId().toString(),
};

await getNoteById?.({}, args, {});
} catch (error: unknown) {
expect((error as Error).message).toEqual(NOTE_NOT_FOUND_ERROR.MESSAGE);
}
});

it("returns the note successfully if the user is authorized", async () => {
const args = {
id: testNote._id.toString(),
};

const result = await getNoteById?.({}, args, {});

expect(result).toBeDefined();
expect(result?._id).toEqual(testNote._id);
});
});

0 comments on commit 3c64ea3

Please sign in to comment.