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({