diff --git a/src/components/viewmodels/roomlist/RoomListItemViewModel.tsx b/src/components/viewmodels/roomlist/RoomListItemViewModel.tsx index c1b07b1558..e009033875 100644 --- a/src/components/viewmodels/roomlist/RoomListItemViewModel.tsx +++ b/src/components/viewmodels/roomlist/RoomListItemViewModel.tsx @@ -220,7 +220,7 @@ function useRoomMessagePreview(room: Room): string | undefined { room, roomIsDM ? DefaultTagID.DM : DefaultTagID.Untagged, ); - if (messagePreview) setPreviewText(messagePreview.text); + setPreviewText(messagePreview?.text); }, [room, shouldShowMessagePreview]); // MessagePreviewStore and the other AsyncStores need to be converted to TypedEventEmitter diff --git a/test/unit-tests/components/viewmodels/roomlist/RoomListItemViewModel-test.tsx b/test/unit-tests/components/viewmodels/roomlist/RoomListItemViewModel-test.tsx index 342a24366a..c58ae4168c 100644 --- a/test/unit-tests/components/viewmodels/roomlist/RoomListItemViewModel-test.tsx +++ b/test/unit-tests/components/viewmodels/roomlist/RoomListItemViewModel-test.tsx @@ -138,6 +138,28 @@ describe("RoomListItemViewModel", () => { expect(vm.current.messagePreview).toBe(undefined); }); + it("should check message preview when room change", async () => { + const otherRoom = mkStubRoom("roomId2", "roomName2", room.client); + + jest.spyOn(MessagePreviewStore.instance, "getPreviewForRoom").mockResolvedValue({ + text: "Message look like this", + } as MessagePreview); + mocked(useMessagePreviewToggle).mockReturnValue({ + shouldShowMessagePreview: true, + toggleMessagePreview: jest.fn(), + }); + + const { result: vm, rerender } = renderHook((props) => useRoomListItemViewModel(props), { + initialProps: room, + ...withClientContextRenderOptions(room.client), + }); + await waitFor(() => expect(vm.current.messagePreview).toBe("Message look like this")); + + jest.spyOn(MessagePreviewStore.instance, "getPreviewForRoom").mockResolvedValue(null); + rerender(otherRoom); + await waitFor(() => expect(vm.current.messagePreview).toBe(undefined)); + }); + describe("notification", () => { let notificationState: RoomNotificationState; beforeEach(() => {