Skip to content

Commit 61ff916

Browse files
langleydsnowping
authored andcommitted
Mock CallStore.getCall rather than individual hooks like useParticipantCount (element-hq#30636)
1 parent 5aa8a4b commit 61ff916

1 file changed

Lines changed: 38 additions & 4 deletions

File tree

test/unit-tests/components/views/rooms/RoomHeader/RoomHeader-test.tsx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import * as ShieldUtils from "../../../../../../src/utils/ShieldUtils";
5252
import { Container, WidgetLayoutStore } from "../../../../../../src/stores/widgets/WidgetLayoutStore";
5353
import MatrixClientContext from "../../../../../../src/contexts/MatrixClientContext";
5454
import { _t } from "../../../../../../src/languageHandler";
55-
import * as UseCall from "../../../../../../src/hooks/useCall";
5655
import { SdkContextClass } from "../../../../../../src/contexts/SDKContext";
5756
import WidgetStore, { type IApp } from "../../../../../../src/stores/WidgetStore";
5857
import { UIFeature } from "../../../../../../src/settings/UIFeature";
@@ -96,6 +95,10 @@ describe("RoomHeader", () => {
9695

9796
setCardSpy = jest.spyOn(RightPanelStore.instance, "setCard");
9897
jest.spyOn(ShieldUtils, "shieldStatusForRoom").mockResolvedValue(ShieldUtils.E2EStatus.Normal);
98+
99+
// Mock CallStore.instance.getCall to return null by default
100+
// Individual tests can override this when they need a specific Call object
101+
jest.spyOn(CallStore.instance, "getCall").mockReturnValue(null);
99102
});
100103

101104
afterEach(() => {
@@ -555,15 +558,17 @@ describe("RoomHeader", () => {
555558

556559
it("join button is shown if there is an ongoing call", async () => {
557560
mockRoomMembers(room, 3);
558-
jest.spyOn(UseCall, "useParticipantCount").mockReturnValue(3);
561+
// Mock CallStore to return a call with 3 participants
562+
jest.spyOn(CallStore.instance, "getCall").mockReturnValue(createMockCall(ROOM_ID, 3));
559563
render(<RoomHeader room={room} />, getWrapper());
560564
const joinButton = getByLabelText(document.body, "Join");
561565
expect(joinButton).not.toHaveAttribute("aria-disabled", "true");
562566
});
563567

564568
it("join button is disabled if there is an other ongoing call", async () => {
565569
mockRoomMembers(room, 3);
566-
jest.spyOn(UseCall, "useParticipantCount").mockReturnValue(3);
570+
// Mock CallStore to return a call with 3 participants
571+
jest.spyOn(CallStore.instance, "getCall").mockReturnValue(createMockCall(ROOM_ID, 3));
567572
jest.spyOn(CallStore.prototype, "connectedCalls", "get").mockReturnValue(
568573
new Set([{ roomId: "some_other_room" } as Call]),
569574
);
@@ -583,7 +588,8 @@ describe("RoomHeader", () => {
583588

584589
it("close lobby button is shown if there is an ongoing call but we are viewing the lobby", async () => {
585590
mockRoomMembers(room, 3);
586-
jest.spyOn(UseCall, "useParticipantCount").mockReturnValue(3);
591+
// Mock CallStore to return a call with 3 participants
592+
jest.spyOn(CallStore.instance, "getCall").mockReturnValue(createMockCall(ROOM_ID, 3));
587593
jest.spyOn(SdkContextClass.instance.roomViewStore, "isViewingCall").mockReturnValue(true);
588594

589595
render(<RoomHeader room={room} />, getWrapper());
@@ -789,6 +795,34 @@ describe("RoomHeader", () => {
789795
});
790796
});
791797

798+
/**
799+
* Creates a mock Call object with stable participants to prevent React dependency errors
800+
*/
801+
function createMockCall(roomId: string = "!1:example.org", participantCount: number = 0): Call {
802+
const participants = new Map();
803+
804+
// Create mock participants with devices
805+
for (let i = 0; i < participantCount; i++) {
806+
const mockMember = {
807+
userId: `@user-${i}:example.org`,
808+
name: `Member ${i}`,
809+
} as RoomMember;
810+
811+
const deviceSet = new Set([`device-${i}`]);
812+
participants.set(mockMember, deviceSet);
813+
}
814+
815+
return {
816+
roomId,
817+
participants,
818+
widget: { id: "test-widget" },
819+
connectionState: "disconnected",
820+
on: jest.fn(),
821+
off: jest.fn(),
822+
emit: jest.fn(),
823+
} as unknown as Call;
824+
}
825+
792826
/**
793827
*
794828
* @param count the number of users to create

0 commit comments

Comments
 (0)