Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Fix regression with TimelinePanel props updates not taking effect (#9608
Browse files Browse the repository at this point in the history
)

* Fix regression with TimelinePanel props updates not taking effect

* Add test
  • Loading branch information
t3chguy authored Nov 24, 2022
1 parent a8e15eb commit 8b8d24c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/components/structures/TimelinePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ class TimelinePanel extends React.Component<IProps, IState> {
this.initTimeline(this.props);
}

public componentDidUpdate(newProps) {
if (newProps.timelineSet !== this.props.timelineSet) {
public componentDidUpdate(prevProps) {
if (prevProps.timelineSet !== this.props.timelineSet) {
// throw new Error("changing timelineSet on a TimelinePanel is not supported");

// regrettably, this does happen; in particular, when joining a
Expand All @@ -325,13 +325,13 @@ class TimelinePanel extends React.Component<IProps, IState> {
logger.warn("Replacing timelineSet on a TimelinePanel - confusion may ensue");
}

const differentEventId = newProps.eventId != this.props.eventId;
const differentHighlightedEventId = newProps.highlightedEventId != this.props.highlightedEventId;
const differentAvoidJump = newProps.eventScrollIntoView && !this.props.eventScrollIntoView;
const differentEventId = prevProps.eventId != this.props.eventId;
const differentHighlightedEventId = prevProps.highlightedEventId != this.props.highlightedEventId;
const differentAvoidJump = prevProps.eventScrollIntoView && !this.props.eventScrollIntoView;
if (differentEventId || differentHighlightedEventId || differentAvoidJump) {
logger.log(`TimelinePanel switching to eventId ${newProps.eventId} (was ${this.props.eventId}), ` +
`scrollIntoView: ${newProps.eventScrollIntoView} (was ${this.props.eventScrollIntoView})`);
this.initTimeline(newProps);
logger.log(`TimelinePanel switching to eventId ${this.props.eventId} (was ${prevProps.eventId}), ` +
`scrollIntoView: ${this.props.eventScrollIntoView} (was ${prevProps.eventScrollIntoView})`);
this.initTimeline(this.props);
}
}

Expand Down
36 changes: 28 additions & 8 deletions test/components/structures/TimelinePanel-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const newReceipt = (eventId: string, userId: string, readTs: number, fullyReadTs
return new MatrixEvent({ content: receiptContent, type: EventType.Receipt });
};

const renderPanel = (room: Room, events: MatrixEvent[]): RenderResult => {
const getProps = (room: Room, events: MatrixEvent[]): TimelinePanel["props"] => {
const timelineSet = { room: room as Room } as EventTimelineSet;
const timeline = new EventTimeline(timelineSet);
events.forEach((event) => timeline.addEvent(event, true));
Expand All @@ -54,13 +54,16 @@ const renderPanel = (room: Room, events: MatrixEvent[]): RenderResult => {
timelineSet.getPendingEvents = () => events;
timelineSet.room.getEventReadUpTo = () => events[1].getId();

return render(
<TimelinePanel
timelineSet={timelineSet}
manageReadReceipts
sendReadReceiptOnLoad
/>,
);
return {
timelineSet,
manageReadReceipts: true,
sendReadReceiptOnLoad: true,
};
};

const renderPanel = (room: Room, events: MatrixEvent[]): RenderResult => {
const props = getProps(room, events);
return render(<TimelinePanel {...props} />);
};

const mockEvents = (room: Room, count = 2): MatrixEvent[] => {
Expand Down Expand Up @@ -172,4 +175,21 @@ describe('TimelinePanel', () => {
expect(client.setRoomReadMarkers).toHaveBeenCalledWith(room.roomId, "", undefined, events[0]);
});
});

it("should scroll event into view when props.eventId changes", () => {
const client = MatrixClientPeg.get();
const room = mkRoom(client, "roomId");
const events = mockEvents(room);

const props = {
...getProps(room, events),
onEventScrolledIntoView: jest.fn(),
};

const { rerender } = render(<TimelinePanel {...props} />);
expect(props.onEventScrolledIntoView).toHaveBeenCalledWith(undefined);
props.eventId = events[1].getId();
rerender(<TimelinePanel {...props} />);
expect(props.onEventScrolledIntoView).toHaveBeenCalledWith(events[1].getId());
});
});

0 comments on commit 8b8d24c

Please sign in to comment.