Implementing infinite scrolling with React. Writing E2E tests with Playwright

React Testing

As our application grows, the amount of data we receive through our REST API grows as well. We often need to split the data into parts and load them separately to avoid performance issues. A common pattern is implementing infinite scrolling that loads more content when the user scrolls down.

In this article, we implement infinite scrolling with React using the intersection observer. We also learn how to implement End-to-End tests using Playwright to ensure our application works as expected.

Implementing infinite scrolling with React

In our article, we will use the JSONPlaceholder API to fetch a list of posts. We can use query parameters to specify how many elements we want to fetch:

For example, by fetching the posts from the above URL, we state that we want twenty posts, starting from the post with the number 40 and ending with the post with the number 60.

When we send the query parameter, the API attaches the response header that tells us how many posts there are in total. This will be useful to determine whether we should stop trying to fetch more data.

Making the HTTP request

Let’s start by writing the function that makes the HTTP request to the REST API. It needs to determine the starting point of our data and how many posts we want to fetch. To make it easy to use, we can split the data into pages that users can fetch one by one.

To compose the request URL with the query parameters, we can use the URLSearchParams class provided by the browsers.

fetchPosts.tsx

Fetching pages one by one

Now, let’s implement the logic of fetching the pages one by one. Let’s store the current page number and increase it every time the user fetches a set of posts. Every time additional posts are fetched, we add them to the array.

We can determine if all posts were fetched by comparing the value from the header with the number of posts we have in the state.

usePostPages.tsx

We store the and as references because we don’t want to have to put them into the dependencies array in the .

Detecting that the user scrolled the page

Now, we need to fetch the data at the correct moment when the user scrolls the page to the bottom. One way of doing that is through the Intersection Observer API. With it, we can detect when an element becomes visible or hidden on our website.

Let’s display a list of posts and a loader at the bottom.

Posts.tsx

The loading indicator is always rendered at the bottom of the list of posts, but it is not visible if the user does not scroll to the bottom. The goal is to fetch more data when the user scrolls to the bottom and the loading indicator becomes visible.

We don’t need to render the loading indicator if all posts have been fetched and there is no need to load more.

To detect when the user scrolled down and the loading indicator was visible, we can use the class, which receives a callback as an argument. It is called every time the visibility of the observed element changes.

usePostsLoading.tsx

We should clean up and stop observing the element when it is not necessary anymore.

Thanks to the above, we now have a fully working infinite scrolling functionality.

Writing End-to-End tests for the infinite scrolling feature with Playwright

Let’s start by writing a simple End-to-End test with Playwright that checks if the first twenty posts load without interacting with the user interface. We can achieve that by using the function, which ensures that a given number of elements can be found on the page.

Please notice that we added properties in the component.

posts.test.tsx

We can use to navigate to our website thanks to using the config and environment variables. If you want to know more, check out JavaScript testing #17. Introduction to End-to-End testing with Playwright

Now, we need to simulate the user scrolling down the page. The easiest way is to use the function built into Playwright. However, we must wait for the first twenty posts to load before scrolling down.

posts.test.tsx

Thanks to the function, we could scroll down so the loader would be visible. This triggers the second HTTP request and loads more posts.

Summary

In this article, we’ve implemented infinite scrolling using React and the Intersection Observer. With this approach, we improved the user experience and optimized the performance of our application by loading the data only if needed.

To ensure that everything works as expected, we wrote End-to-End tests using Playwright. To do that, we had to learn how to simulate the user scrolling down on our website. With all that, we helped our frontend application handle a lot of data without slowing down. We also made sure it was bug-free by writing E2E tests.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments