Skip to content

Commit

Permalink
fixing courses tests (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
keijionga authored Jan 4, 2025
1 parent 83f1f4b commit b0e7a4c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/controllers/courses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const updateCourse = async (req: Request, res: Response) => {
const updatedModuleIds: string[] = [];

const updatedModules = await Promise.all(
modules.map(async (module: { id?: string; title: string; lessons: { _id: string; title: string }[] }) => {
modules.map(async (module: { _id?: string; title: string; lessons: { _id: string; title: string }[] }) => {
if (!module.title || !Array.isArray(module.lessons)) {
throw new Error("Each module must have a title and a valid lessons array");
}
Expand All @@ -99,14 +99,14 @@ export const updateCourse = async (req: Request, res: Response) => {
}),
);

if (module.id?.startsWith("module")) {
if (!module._id?.startsWith("module")) {
const updatedModule = await ModuleModel.findByIdAndUpdate(
module.id,
module._id,
{ title: module.title, lessons: lessonIds },
{ new: true },
);
if (!updatedModule) {
throw new Error(`Failed to update module with ID ${module.id}`);
throw new Error(`Failed to update module with ID ${module._id}`);
}
updatedModuleIds.push(updatedModule._id.toString());
return updatedModule._id;
Expand Down
65 changes: 47 additions & 18 deletions tests/courses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,20 @@ describe("Setting API Server up...", () => {
},
});

const module = await ModuleModel.create({
teamId: team,
title: "Module #1",
lessons: [lesson._id],
});

const courseTitle = "Course #1";
const courseLanguage = "english";
const courseSummary = "This is a summary of Course #1";
const courseModules = [
{
title: "This is a test module",
lessons: [
{
_id: lesson.id,
title: lesson.title,
},
],
},
];

await axios
.post(
Expand All @@ -119,15 +124,15 @@ describe("Setting API Server up...", () => {
title: courseTitle,
language: courseLanguage,
summary: courseSummary,
modules: [module._id],
modules: courseModules,
},
{ headers: adminHeaders },
)
.then((r) => {
expect(r.data.title).toEqual(courseTitle);
expect(r.data.language).toEqual(courseLanguage);
expect(r.data.summary).toEqual(courseSummary);
expect(r.data.modules[0]).toEqual(module._id.toString());
expect(r.data.modules.length).toEqual(courseModules.length);
})
.catch((e) => {
expect(e).toBeUndefined();
Expand All @@ -138,7 +143,12 @@ describe("Setting API Server up...", () => {
const courseTitle = "Course with invalid modules";
const courseLanguage = "english";
const courseSummary = "This course contains invalid modules";
const invalidModuleId = "60e4b68f2f8fb814b56fa181";
// invalid for not having a title
const invalidModules = [
{
lessons: [],
},
];

await axios
.post(
Expand All @@ -147,13 +157,13 @@ describe("Setting API Server up...", () => {
title: courseTitle,
language: courseLanguage,
summary: courseSummary,
modules: [invalidModuleId],
modules: invalidModules,
},
{ headers: adminHeaders },
)
.then(() => {})
.catch((e) => {
expect(e.response.data.error.message).toContain("Some modules not found");
expect(e.response.data.error.message).toContain("Each module must have a title and a valid lessons array");
});
});

Expand Down Expand Up @@ -201,14 +211,36 @@ describe("Setting API Server up...", () => {
const updatedTitle = "Curso atualizado";
const updatedLanguage = "portuguese";
const updatedSummary = "Resumo do curso atualizado";
const updatedModules = [
{
_id: "module-1",
title: "This module will be created",
lessons: [
{
_id: lesson1._id,
title: lesson1.title,
},
],
},
{
_id: module._id,
title: "This module is getting updated",
lessons: [
{
_id: lesson2._id,
title: lesson2.title,
},
],
},
];

await axios.put(
`${API_URL}/course/${team._id}/${course._id}`,
{
title: updatedTitle,
language: updatedLanguage,
summary: updatedSummary,
modules: [module._id],
modules: updatedModules,
},
{ headers: adminHeaders },
);
Expand All @@ -222,6 +254,7 @@ describe("Setting API Server up...", () => {
expect(r.data.modules.some((recordedModule: Module) => recordedModule._id === module._id.toString())).toBe(
true,
);
expect(r.data.modules.length).toEqual(2);
})
.catch((e) => expect(e).toBeUndefined());
});
Expand Down Expand Up @@ -432,13 +465,9 @@ describe("Setting API Server up...", () => {
});

await axios
.post(`${API_URL}/course/duplicate/${team._id}/${course._id}`, {}, { headers: adminHeaders })
.post(`${API_URL}/courses/duplicate/${team._id}`, { courses: [course._id] }, { headers: adminHeaders })
.then((r) => {
expect(r.data.title).toEqual(`${course.title} (Copy)`);
expect(r.data.summary).toEqual(course.summary);
expect(r.data.modules.some((recordedModule: Module) => recordedModule._id === module._id.toString())).toBe(
true,
);
expect(r.data.length).toEqual(1);
})
.catch((e) => expect(e).toBeUndefined());
});
Expand Down

0 comments on commit b0e7a4c

Please sign in to comment.