Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d74e7ca

Browse files
docs: update FE fetching data docs (coder#11376)
1 parent 5d76210 commit d74e7ca

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

docs/contributing/frontend.md

+46-9
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,45 @@ the api/queries folder when it is possible.
9999

100100
### Where to fetch data
101101

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+
```
107141

108142
### API
109143

@@ -237,13 +271,16 @@ another page, you should probably consider using the **E2E** approach.
237271

238272
### Visual testing
239273

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
244278
[Storybook](https://storybook.js.org/) and
245279
[Chromatic](https://www.chromatic.com/).
246280

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+
247284
### What should I test?
248285

249286
Choosing what to test is not always easy since there are a lot of flows and a

0 commit comments

Comments
 (0)