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 <dbkr@users.noreply.github.com>
This commit is contained in:
hayyaksi
2026-08-14 13:36:55 +00:00
committed by GitHub
parent 2ec856d0bf
commit 471e182571
2 changed files with 82 additions and 11 deletions
@@ -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",
}),
),
);
});
});
@@ -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<ViewRoomPayload>({