Skip to content

Commit

Permalink
fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psachmann committed Dec 16, 2024
1 parent 81d4faa commit 0578f51
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
38 changes: 21 additions & 17 deletions src/store/course-room-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import { Action, Module, Mutation, VuexModule } from "vuex-module-decorators";
import { BusinessError } from "./types/commons";
import { HttpStatusCode } from "./types/http-status-code.enum";
import { Course } from "./types/room";
import { CommonCartridgeApiFactory } from "@/commonCartridgeApi/v3";
import {
CommonCartridgeApiFactory,
CommonCartridgeApiInterface,
} from "@/commonCartridgeApi/v3";

@Module({
name: "courseRoomDetailsModule",
Expand Down Expand Up @@ -53,6 +56,10 @@ export default class CourseRoomDetailsModule extends VuexModule {
return LessonApiFactory(undefined, "/v3", $axios);
}

public getCommonCartridgeApi(): CommonCartridgeApiInterface {
return CommonCartridgeApiFactory(undefined, "/v3", $axios);
}

@Action
async fetchCourse(courseId: string): Promise<Course | null> {
this.setLoading(true);
Expand Down Expand Up @@ -199,22 +206,19 @@ export default class CourseRoomDetailsModule extends VuexModule {
}): Promise<void> {
this.resetBusinessError();
try {
const response = await CommonCartridgeApiFactory(
undefined,
"v3",
$axios
).commonCartridgeControllerExportCourse(
this.roomData.roomId,
exportSettings.version,
{
topics: exportSettings.topics,
tasks: exportSettings.tasks,
columnBoards: exportSettings.columnBoards,
},
{
responseType: "blob",
}
);
const response =
await this.getCommonCartridgeApi().commonCartridgeControllerExportCourse(
this.roomData.roomId,
exportSettings.version,
{
topics: exportSettings.topics,
tasks: exportSettings.tasks,
columnBoards: exportSettings.columnBoards,
},
{
responseType: "blob",
}
);
// const response = await CoursesApiFactory(
// undefined,
// "v3",
Expand Down
34 changes: 16 additions & 18 deletions src/store/course-room-details.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,14 @@ describe("course-room module", () => {
describe("downloadCommonCartridgeCourse", () => {
it("should call backend api", async () => {
const courseRoomDetailsModule = new CourseRoomDetailsModule({});
const spy = jest
.spyOn(
CommonCartridgeApiFactory(),
"commonCartridgeControllerExportCourse"
)
.mockReturnValue(Promise.resolve() as unknown as AxiosPromise<void>);

jest
.spyOn(courseRoomDetailsModule, "getCommonCartridgeApi")
.mockReturnValue({
commonCartridgeControllerExportCourse: jest.fn(
() => Promise.resolve() as unknown as AxiosPromise<void>
),
});

await expect(
courseRoomDetailsModule.downloadCommonCartridgeCourse({
Expand All @@ -402,20 +404,18 @@ describe("course-room module", () => {
columnBoards: [],
})
).resolves.not.toBeDefined();

spy.mockRestore();
});
it("should catch error in catch block", async () => {
const courseRoomDetailsModule = new CourseRoomDetailsModule({});
const error = { statusCode: 418, message: "I'm a teapot" };
const spy = jest
.spyOn(
CommonCartridgeApiFactory(),
"commonCartridgeControllerExportCourse"
)
.mockReturnValue(
Promise.reject(error) as unknown as AxiosPromise<void>
);

jest
.spyOn(courseRoomDetailsModule, "getCommonCartridgeApi")
.mockReturnValue({
commonCartridgeControllerExportCourse: jest.fn(() =>
Promise.reject(error)
),
});

await courseRoomDetailsModule.downloadCommonCartridgeCourse({
version: "1.1.0",
Expand All @@ -425,8 +425,6 @@ describe("course-room module", () => {
});

expect(courseRoomDetailsModule.businessError).toStrictEqual(error);

spy.mockRestore();
});
});

Expand Down

0 comments on commit 0578f51

Please sign in to comment.