Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement scrollToLine by checking "overridden" layouts #89

Merged
merged 4 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions __tests__/usecase/buffers/ui/Buffer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import {
fireEvent,
render,
screen,
waitFor
waitFor,
within
} from '@testing-library/react-native';
import React from 'react';
import { ScrollView } from 'react-native';
import Buffer from '../../../../src/usecase/buffers/ui/Buffer';
import { CellContainer } from '@shopify/flash-list';

jest.useFakeTimers();

Expand All @@ -15,7 +17,7 @@ describe(Buffer, () => {
const nickWidthText = screen.getByText('aaaaaaaa', { hidden: true });
fireEvent(nickWidthText, 'layout', {
nativeEvent: {
layout: { height: 26.5, width: 68, x: 0, y: 0 }
layout: { height: 16.7, width: 68, x: 0, y: 0 }
}
});
};
Expand All @@ -33,7 +35,8 @@ describe(Buffer, () => {
date_printed: '2024-04-06T17:20:30.000Z',
displayed: 1,
highlight: 0,
message: 'Second message',
message:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
pointers: ['86c417600', '8580eeec0', '8580dcc40', '86c2ff040'],
prefix: 'user',
tags_array: ['irc_privmsg', 'notify_message']
Expand All @@ -60,53 +63,65 @@ describe(Buffer, () => {

measureNickWidth();

// Simulate layout event for the FlatList
const listElement = screen.getByLabelText('Message list');
fireEvent(listElement, 'layout', {
// Simulate layout event for the ScrollView
const scrollView = screen.UNSAFE_getByType(ScrollView);
fireEvent(scrollView, 'layout', {
nativeEvent: {
layout: { height: 26.5, width: 1024, x: 0, y: 0 }
}
});

// Simulate layout event for first line
let message = screen.getByTestId('renderCell(0)');
fireEvent(message, 'layout', {
let messageCell = screen
.UNSAFE_getAllByType(CellContainer)
.find((container) =>
within(container).queryByText(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
)
);
expect(messageCell).toBeDefined();
fireEvent(messageCell, 'layout', {
nativeEvent: {
layout: { height: 26.5, width: 1024, x: 0, y: 0 }
layout: { height: 43, width: 1024, x: 0, y: 0 }
}
});

jest.advanceTimersToNextTimer();

bufferRef.current?.scrollToLine('86c2fefd0');

expect(ScrollView.prototype.scrollTo).toHaveBeenNthCalledWith(1, {
animated: false,
y: 0
x: 0,
y: 43
});

// This is effectively a no-op, we are already at 0, 0. However, scrollTo
// triggers this and the VirtualizedList will update internal state based
// on the layout properties, so fire it here as well.
fireEvent.scroll(listElement, {
// Simulate scroll event in response to scrollTo
fireEvent.scroll(scrollView, {
nativeEvent: {
contentInset: { bottom: 0, left: 0, right: 0, top: 0 },
contentOffset: { x: 0, y: 0 },
contentSize: { height: 26.5, width: 1024 },
contentOffset: { x: 0, y: 43 },
contentSize: { height: 43, width: 1024 },
layoutMeasurement: { height: 26.5, width: 1024 }
}
});

// Simulate layout event for second line
message = screen.getByTestId('renderCell(1)');
fireEvent(message, 'layout', {
messageCell = screen
.UNSAFE_getAllByType(CellContainer)
.find((container) => within(container).queryByText('First message'));
expect(messageCell).toBeDefined();
fireEvent(messageCell, 'layout', {
nativeEvent: {
layout: { height: 26.5, width: 1024, x: 0, y: 26.5 }
layout: { height: 26.5, width: 1024, x: 0, y: 43 }
}
});

await waitFor(() => {
expect(ScrollView.prototype.scrollTo).toHaveBeenNthCalledWith(2, {
animated: false,
y: 26.5
x: 0,
y: 43
});
});
});
Expand Down
Loading