EdsonBueno / infinite_scroll_pagination

Flutter package to help you lazily load and display pages of items as the user scrolls down your screen.
https://pub.dev/packages/infinite_scroll_pagination
MIT License
624 stars 211 forks source link

Cannot scroll PagedListView in widget test #156

Closed BayesTech closed 2 years ago

BayesTech commented 2 years ago

Hi, I am trying to add a widget test to my flutter application with infinite_scroll_pagination. However, I fail to scroll to view more items. I have 15 items in the list, 9 items are out of the view and requires scroll to view them. The widget test can only see 6 items. I would like to scroll infinite_scroll_pagination to test all the items are in the list. Can you please help on how to scroll infinite_scroll_pagination for widget test?

Thank you.

Here is the code how I use infinite_scroll_pagination.

PagedListView<int, ListPostResponseItem>(
          key: Key('paged_list_view'),
          pagingController: _pagingController,
          builderDelegate: PagedChildBuilderDelegate<ListPostResponseItem>(
            itemBuilder: (context, item, index) =>
                ListPostItemWidget(
                  key: Key('list_post_item_widget_' + index.toString()),
                  item: item
                ),
          ),
        )

and here is my widget test

testWidgets('Test widget', (WidgetTester tester) async {
    await tester.pumpWidget(MultiProvider(
      providers: providers,
      child: MainPage(),
    ),);

    expect(find.byKey(Key('ListPostPage')), findsOneWidget);  // This can be found
    expect(find.byKey(Key('paged_list_view')), findsOneWidget); // This can be found
    // expect(find.byType(PagedListView), findsOneWidget); // This cannot be found
    // expect(find.byType(BoxScrollView), findsOneWidget); // This cannot be found

    await tester.pump(Duration(seconds: 10));

    expect(find.byType(ListPostItemWidget), findsNWidgets(6));  // This passed

    await tester.scrollUntilVisible(
      find.byKey(Key('list_post_item_widget_6')),
        1500,
      scrollable: find.byKey(Key('paged_list_view'))
    );
  });

The error of the test is that

The following _CastError was thrown running a test:
type 'PagedListView<int, ListPostResponseItem>' is not a subtype of type 'Scrollable' in type cast
clragon commented 2 years ago

you can programmatically request loading the next page with the paging controller, or you can pass a scroll controller and scroll the paged view.

I would recommend the first method.

EdsonBueno commented 2 years ago

Thanks for doing this @clragon. I'm closing the issue due to inactivity.