From 471e1825716e9bb0323a7736824162b01e0619fa Mon Sep 17 00:00:00 2001 From: hayyaksi <193020925+hayaksi1@users.noreply.github.com> Date: Fri, 14 Aug 2026 13:36:55 +0000 Subject: [PATCH] Let the next and previous room shortcuts work before a room is opened (#34506) * Let the next and previous room shortcuts work before a room is opened * Wait for the navigation these shortcuts dispatch The three cases drained the microtask queue twice and then asserted, because the delta handler dispatches its navigation from inside the first dispatch's callback and one flush leaves that follow-up queued. Counting flushes encodes how far the implementation happens to defer the work; waiting for the assertion itself does not, and reads as what the case is actually waiting for. The unread case also restored its notification-state spy by hand, on the belief that nothing else would. The suite has restored all mocks from a top-level afterEach since well before this branch, so the hand restoration was redundant and the note explaining it was wrong. --------- Co-authored-by: David Baker --- .../room-list/RoomListViewModel.test.ts | 76 +++++++++++++++++-- .../viewmodels/room-list/RoomListViewModel.ts | 17 +++-- 2 files changed, 82 insertions(+), 11 deletions(-) diff --git a/apps/web/src/viewmodels/room-list/RoomListViewModel.test.ts b/apps/web/src/viewmodels/room-list/RoomListViewModel.test.ts index 7e0a8f40df..f576fff5eb 100644 --- a/apps/web/src/viewmodels/room-list/RoomListViewModel.test.ts +++ b/apps/web/src/viewmodels/room-list/RoomListViewModel.test.ts @@ -813,7 +813,7 @@ describe("RoomListViewModel", () => { ); }); - it("should not navigate when no room is selected", async () => { + it("should navigate to the first room when no room is selected and delta is 1", async () => { viewModel = new RoomListViewModel({ client: matrixClient, spaceStore: SDKContextClass.instance.spaceStore, @@ -832,12 +832,76 @@ describe("RoomListViewModel", () => { }); dispatchSpy.mockImplementation(() => {}); - await flushPromises(); + await waitFor(() => + expect(dispatchSpy).toHaveBeenCalledWith( + expect.objectContaining({ + action: Action.ViewRoom, + room_id: "!room1:server", + }), + ), + ); + }); - expect(dispatchSpy).not.toHaveBeenCalledWith( - expect.objectContaining({ - action: Action.ViewRoom, - }), + it("should navigate to the last room when no room is selected and delta is -1", async () => { + viewModel = new RoomListViewModel({ + client: matrixClient, + spaceStore: SDKContextClass.instance.spaceStore, + roomViewStore: SDKContextClass.instance.roomViewStore, + }); + + vi.spyOn(SDKContextClass.instance.roomViewStore, "getRoomId").mockReturnValue(null); + + const dispatchSpy = vi.spyOn(dispatcher, "dispatch"); + + dispatcher.dispatch({ + action: Action.ViewRoomDelta, + delta: -1, + unread: false, + }); + + await waitFor(() => + expect(dispatchSpy).toHaveBeenCalledWith( + expect.objectContaining({ + action: Action.ViewRoom, + room_id: "!room3:server", + }), + ), + ); + }); + + it("should navigate to the first unread room when no room is selected", async () => { + vi.spyOn(RoomNotificationStateStore.instance, "getRoomState").mockImplementation( + (room) => + ({ + isUnread: room.roomId === "!room2:server", + on: vi.fn(), + off: vi.fn(), + }) as unknown as RoomNotificationState, + ); + + viewModel = new RoomListViewModel({ + client: matrixClient, + spaceStore: SDKContextClass.instance.spaceStore, + roomViewStore: SDKContextClass.instance.roomViewStore, + }); + + vi.spyOn(SDKContextClass.instance.roomViewStore, "getRoomId").mockReturnValue(null); + + const dispatchSpy = vi.spyOn(dispatcher, "dispatch"); + + dispatcher.dispatch({ + action: Action.ViewRoomDelta, + delta: 1, + unread: true, + }); + + await waitFor(() => + expect(dispatchSpy).toHaveBeenCalledWith( + expect.objectContaining({ + action: Action.ViewRoom, + room_id: "!room2:server", + }), + ), ); }); }); diff --git a/apps/web/src/viewmodels/room-list/RoomListViewModel.ts b/apps/web/src/viewmodels/room-list/RoomListViewModel.ts index 52962ba776..d4c94b45ca 100644 --- a/apps/web/src/viewmodels/room-list/RoomListViewModel.ts +++ b/apps/web/src/viewmodels/room-list/RoomListViewModel.ts @@ -566,7 +566,6 @@ export class RoomListViewModel */ private handleViewRoomDelta(payload: ViewRoomDeltaPayload): void { const currentRoomId = this.props.roomViewStore.getRoomId(); - if (!currentRoomId) return; const { delta, unread } = payload; const rooms = this.sections.flatMap((section) => section.rooms); @@ -579,14 +578,22 @@ export class RoomListViewModel }) : rooms; - const currentIndex = filteredRooms.findIndex((room) => room.roomId === currentRoomId); - if (currentIndex === -1) return; - // Get the next/previous new room according to the delta // Use slice to loop on the list // If delta is -1 at the start of the list, it will go to the end // If delta is 1 at the end of the list, it will go to the start - const [newRoom] = filteredRooms.slice((currentIndex + delta) % filteredRooms.length); + let start: number; + if (currentRoomId) { + const currentIndex = filteredRooms.findIndex((room) => room.roomId === currentRoomId); + if (currentIndex === -1) return; + start = (currentIndex + delta) % filteredRooms.length; + } else { + // No room is open, which is the case right after the app loads. There is nothing to + // move relative to, so start from whichever end of the list the delta points at. + start = delta > 0 ? 0 : -1; + } + + const [newRoom] = filteredRooms.slice(start); if (!newRoom) return; dispatcher.dispatch({