@@ -99,11 +99,45 @@ the api/queries folder when it is possible.
99
99
100
100
### Where to fetch data
101
101
102
- Finding the right place to fetch data in React apps is the million-dollar
103
- question, but we decided to make it only in the page components and pass the
104
- props down to the views. This makes it easier to find where data is being loaded
105
- and easy to test using Storybook. So you will see components like ` UsersPage `
106
- and ` UsersPageView ` .
102
+ In the past, our approach involved creating separate components for page and
103
+ view, where the page component served as a container responsible for fetching
104
+ data and passing it down to the view.
105
+
106
+ For instance, when developing a page to display users, we would have a
107
+ ` UsersPage ` component with a corresponding ` UsersPageView ` . The ` UsersPage `
108
+ would handle API calls, while the ` UsersPageView ` managed the presentational
109
+ logic.
110
+
111
+ Over time, however, we encountered challenges with this approach, particularly
112
+ in terms of excessive props drilling. To address this, we opted to fetch data in
113
+ proximity to its usage. Taking the example of displaying users, in the past, if
114
+ we were creating a header component for that page, we would have needed to fetch
115
+ the data in the page component and pass it down through the hierarchy
116
+ (` UsersPage -> UsersPageView -> UsersHeader ` ). Now, with libraries such as
117
+ ` react-query ` , data fetching can be performed directly in the ` UsersHeader `
118
+ component, allowing UI elements to declare and consume their data-fetching
119
+ dependencies directly, while preventing duplicate server requests
120
+ ([ more info] ( https://github.com/TanStack/query/discussions/608#discussioncomment-29735 ) ).
121
+
122
+ To simplify visual testing of scenarios where components are responsible for
123
+ fetching data, you can easily set the queries' value using ` parameters.queries `
124
+ within the component's story.
125
+
126
+ ``` tsx
127
+ export const WithQuota: Story = {
128
+ parameters: {
129
+ queries: [
130
+ {
131
+ key: getWorkspaceQuotaQueryKey (MockUser .username ),
132
+ data: {
133
+ credits_consumed: 2 ,
134
+ budget: 40 ,
135
+ },
136
+ },
137
+ ],
138
+ },
139
+ };
140
+ ```
107
141
108
142
### API
109
143
@@ -237,13 +271,16 @@ another page, you should probably consider using the **E2E** approach.
237
271
238
272
### Visual testing
239
273
240
- Test components without user interaction like testing if a page view is rendered
241
- correctly depending on some parameters, if the button is showing a spinner if
242
- the ` loading ` props are passing , etc. This should always be your first option
243
- since it is way easier to maintain. For this, we use
274
+ We use visual tests to test components without user interaction like testing if
275
+ a page/component is rendered correctly depending on some parameters, if a button
276
+ is showing a spinner, if ` loading ` props are passed correctly , etc. This should
277
+ always be your first option since it is way easier to maintain. For this, we use
244
278
[ Storybook] ( https://storybook.js.org/ ) and
245
279
[ Chromatic] ( https://www.chromatic.com/ ) .
246
280
281
+ > ℹ️ To learn more about testing components that fetch API data, refer to the
282
+ > [ ** Where to fetch data** ] ( #where-to-fetch-data ) section.
283
+
247
284
### What should I test?
248
285
249
286
Choosing what to test is not always easy since there are a lot of flows and a
0 commit comments