Skip to content

Commit

Permalink
add the correct test file
Browse files Browse the repository at this point in the history
  • Loading branch information
virgilchiriac committed Jan 10, 2025
1 parent 6cb77a0 commit 54f4328
Showing 1 changed file with 158 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { createMock, DeepMocked } from "@golevelup/ts-jest";
import { AxiosPromise, AxiosResponse } from "axios";
import { AxiosPromise } from "axios";
import * as serverApi from "@/serverApi/v3/api";
import { useVideoConference } from "./VideoConference.composable";
import {
VideoConferenceInfoResponse,
// VideoConferenceInfoResponse,
VideoConferenceJoinResponse,
VideoConferenceScope,
VideoConferenceStateResponse,
} from "@/serverApi/v3/api";
import { VideoConferenceState } from "@/store/types/video-conference";

let videoConferenceApi: DeepMocked<serverApi.VideoConferenceApiInterface>;

Expand All @@ -25,42 +27,86 @@ describe("VideoConferenceComposable", () => {
});

describe("fetchVideoConferenceInfo", () => {
it("should call videoConferenceControllerInfo api with params", async () => {
const setup = () => {
const scope = VideoConferenceScope.Room;
const scopeId = "123124";
const { fetchVideoConferenceInfo } = useVideoConference(scope, scopeId);

const FAKE_RESPONSE = {
state: VideoConferenceStateResponse.Running,
options: {
everyAttendeeJoinsMuted: true,
everybodyJoinsAsModerator: false,
moderatorMustApproveJoinRequests: true,
},
};
videoConferenceApi.videoConferenceControllerInfo.mockResolvedValueOnce(
FAKE_RESPONSE as unknown as AxiosPromise<VideoConferenceInfoResponse>
);

const { fetchVideoConferenceInfo, error, videoConferenceInfo } =
useVideoConference(scope, scopeId);

return {
scope,
scopeId,
FAKE_RESPONSE,
fetchVideoConferenceInfo,
error,
videoConferenceInfo,
};
};

it("should call videoConferenceControllerInfo api with params", async () => {
const { scope, scopeId, fetchVideoConferenceInfo } = setup();

await fetchVideoConferenceInfo();

expect(
videoConferenceApi.videoConferenceControllerInfo
).toHaveBeenCalledWith(scope, scopeId);
});

it("should update videoConferenceInfo state with the response data", async () => {
const { fetchVideoConferenceInfo, videoConferenceInfo, FAKE_RESPONSE } =
setup();

await fetchVideoConferenceInfo();

expect(videoConferenceInfo.value.state).toBe(
VideoConferenceState.RUNNING
);
expect(videoConferenceInfo.value.options).toEqual(FAKE_RESPONSE.options);
});

it("should set state to unknown if the response state is not recognized", async () => {
const { fetchVideoConferenceInfo, videoConferenceInfo } = setup();
const FAKE_RESPONSE = {
status: 200,
data: {
state: VideoConferenceStateResponse.Running,
state: "bla-bla",
options: {
everyAttendeeJoinsMuted: true,
everybodyJoinsAsModerator: false,
moderatorMustApproveJoinRequests: true,
},
},
};
videoConferenceApi.videoConferenceControllerStart.mockResolvedValue(
FAKE_RESPONSE as unknown as AxiosPromise
videoConferenceApi.videoConferenceControllerInfo.mockResolvedValueOnce(
FAKE_RESPONSE as unknown as AxiosPromise<VideoConferenceInfoResponse>
);

await fetchVideoConferenceInfo();
expect(
videoConferenceApi.videoConferenceControllerInfo
).toHaveBeenCalledWith(scope, scopeId);

expect(videoConferenceInfo.value.state).toBe(
VideoConferenceState.UNKNOWN
);
});

it("should set error if the API call fails", async () => {
const scope = VideoConferenceScope.Room;
const scopeId = "123124";
const { fetchVideoConferenceInfo, error } = useVideoConference(
scope,
scopeId
);
const { scope, scopeId, fetchVideoConferenceInfo, error } = setup();

const mockError = new Error("API call failed");
videoConferenceApi.videoConferenceControllerInfo.mockRejectedValue(
videoConferenceApi.videoConferenceControllerInfo.mockRejectedValueOnce(
mockError
);

Expand All @@ -74,12 +120,13 @@ describe("VideoConferenceComposable", () => {
});

describe("startVideoConference", () => {
it("should call videoConferenceControllerStart api and update videoConferenceInfo", async () => {
const setup = () => {
const scope = VideoConferenceScope.Room;
const scopeId = "123124";
const { startVideoConference, videoConferenceInfo, error } =
useVideoConference(scope, scopeId);

const url = "https://example.com";
const logoutUrl = "https://logout.url";
const options = {
everyAttendeeJoinsMuted: true,
everybodyJoinsAsModerator: false,
Expand All @@ -89,33 +136,67 @@ describe("VideoConferenceComposable", () => {
const FAKE_RESPONSE = {
status: 200,
data: {
url: "https://example.com",
url,
},
};
videoConferenceApi.videoConferenceControllerStart.mockResolvedValue(
videoConferenceApi.videoConferenceControllerStart.mockResolvedValueOnce(
FAKE_RESPONSE as unknown as AxiosPromise
);
return {
scope,
scopeId,
startVideoConference,
videoConferenceInfo,
error,
url,
logoutUrl,
options,
};
};

await startVideoConference(options, "https://logout.url");
it("should call videoConferenceControllerStart api", async () => {
const { scope, scopeId, startVideoConference, logoutUrl, options } =
setup();

await startVideoConference(options, logoutUrl);

expect(
videoConferenceApi.videoConferenceControllerStart
).toHaveBeenCalledWith(scope, scopeId, {
...options,
logoutUrl: "https://logout.url",
logoutUrl,
});
expect(videoConferenceInfo.value.state).toBe("RUNNING");
});
it("should update videoConferenceInfo", async () => {
const { startVideoConference, videoConferenceInfo, logoutUrl } = setup();
const options = {
everyAttendeeJoinsMuted: true,
everybodyJoinsAsModerator: false,
moderatorMustApproveJoinRequests: true,
};

await startVideoConference(options, logoutUrl);

expect(videoConferenceInfo.value.state).toBe(
VideoConferenceState.RUNNING
);
expect(videoConferenceInfo.value.options).toEqual(options);
expect(error.value).toBeNull();
});
it("should set error to null", async () => {
const { startVideoConference, error, logoutUrl } = setup();

const options = {
everyAttendeeJoinsMuted: true,
everybodyJoinsAsModerator: false,
moderatorMustApproveJoinRequests: true,
};

await startVideoConference(options, logoutUrl);

expect(error.value).toBeNull();
});
it("should set error if the API call fails", async () => {
const scope = VideoConferenceScope.Room;
const scopeId = "123124";
const { startVideoConference, error } = useVideoConference(
scope,
scopeId
);
const { startVideoConference, error, logoutUrl } = setup();

const options = {
everyAttendeeJoinsMuted: true,
Expand All @@ -124,72 +205,97 @@ describe("VideoConferenceComposable", () => {
};

const mockError = new Error("API call failed");
videoConferenceApi.videoConferenceControllerStart.mockRejectedValue(
videoConferenceApi.videoConferenceControllerStart.mockRejectedValueOnce(
mockError
);

await startVideoConference(options, "https://logout.url");
await startVideoConference(options, logoutUrl);

expect(
videoConferenceApi.videoConferenceControllerStart
).toHaveBeenCalledWith(scope, scopeId, {
...options,
logoutUrl: "https://logout.url",
});
expect(error.value).toBe(mockError);
});
});

describe("joinVideoConference", () => {
it("should call videoConferenceControllerJoin api and return the URL", async () => {
const setup = () => {
const scope = VideoConferenceScope.Room;
const scopeId = "123124";
const { joinVideoConference, error } = useVideoConference(scope, scopeId);
const url = "https://example.com";

const FAKE_RESPONSE = {
status: 200,
data: {
url: "https://example.com",
url,
},
};
videoConferenceApi.videoConferenceControllerJoin.mockResolvedValue(
videoConferenceApi.videoConferenceControllerJoin.mockResolvedValueOnce(
FAKE_RESPONSE as unknown as AxiosPromise<VideoConferenceJoinResponse>
);

const url = await joinVideoConference();
const { joinVideoConference, error } = useVideoConference(scope, scopeId);

return {
scope,
scopeId,
joinVideoConference,
error,
url,
};
};
it("should call videoConferenceControllerJoin api with params", async () => {
const { scope, scopeId, joinVideoConference } = setup();

await joinVideoConference();

expect(
videoConferenceApi.videoConferenceControllerJoin
).toHaveBeenCalledWith(scope, scopeId);
expect(url).toBe("https://example.com");
expect(error.value).toBeNull();
});
it("should return the URL", async () => {
const { joinVideoConference, url } = setup();

const returnUrl = await joinVideoConference();

expect(url).toBe(returnUrl);
});
it("should set error to null", async () => {
const { joinVideoConference, error } = setup();

await joinVideoConference();

expect(error.value).toBeNull();
});
it("should set error if the API call fails", async () => {
const scope = VideoConferenceScope.Room;
const scopeId = "123124";
const { scope, scopeId } = setup();
const { joinVideoConference, error } = useVideoConference(scope, scopeId);

const mockError = new Error("API call failed");
videoConferenceApi.videoConferenceControllerJoin.mockRejectedValue(
videoConferenceApi.videoConferenceControllerJoin.mockRejectedValueOnce(
mockError
);

const url = await joinVideoConference();
expect(
videoConferenceApi.videoConferenceControllerJoin
).toHaveBeenCalledWith(scope, scopeId);

expect(url).toBeUndefined();
expect(error.value).toBe(mockError);
});
});

describe("resetError", () => {
it("should set error to null", () => {
const setup = () => {
const scope = VideoConferenceScope.Room;
const scopeId = "123124";
const { error, resetError } = useVideoConference(scope, scopeId);

error.value = new Error("Test error");

return {
error,
resetError,
};
};
it("should set error to null", () => {
const { error, resetError } = setup();

resetError();
expect(error.value).toBeNull();
});
Expand Down

0 comments on commit 54f4328

Please sign in to comment.