From 54f4328411ef63e31cacdf0473a9d0fe5ab78835 Mon Sep 17 00:00:00 2001 From: virgilchiriac Date: Fri, 10 Jan 2025 18:38:56 +0100 Subject: [PATCH] add the correct test file --- ....ts => VideoConference.composable.unit.ts} | 210 +++++++++++++----- 1 file changed, 158 insertions(+), 52 deletions(-) rename src/modules/feature/board-video-conference-element/composables/{test.unit.ts => VideoConference.composable.unit.ts} (51%) diff --git a/src/modules/feature/board-video-conference-element/composables/test.unit.ts b/src/modules/feature/board-video-conference-element/composables/VideoConference.composable.unit.ts similarity index 51% rename from src/modules/feature/board-video-conference-element/composables/test.unit.ts rename to src/modules/feature/board-video-conference-element/composables/VideoConference.composable.unit.ts index 1b8ff86539..6d868b2a6c 100644 --- a/src/modules/feature/board-video-conference-element/composables/test.unit.ts +++ b/src/modules/feature/board-video-conference-element/composables/VideoConference.composable.unit.ts @@ -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; @@ -25,15 +27,63 @@ 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 + ); + + 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, @@ -41,26 +91,22 @@ describe("VideoConferenceComposable", () => { }, }, }; - videoConferenceApi.videoConferenceControllerStart.mockResolvedValue( - FAKE_RESPONSE as unknown as AxiosPromise + videoConferenceApi.videoConferenceControllerInfo.mockResolvedValueOnce( + FAKE_RESPONSE as unknown as AxiosPromise ); 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 ); @@ -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, @@ -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, @@ -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 ); - 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(); });