-
Notifications
You must be signed in to change notification settings - Fork 894
docs: update FE fetching data docs #11376
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a0f96c8
docs(site): update fetching data docs
BrunoQuaresma e15c47a
Update workding with Michael suggestions
BrunoQuaresma 47ffd42
update wording
BrunoQuaresma 165e2a5
Update docs/contributing/frontend.md
BrunoQuaresma 8d78545
Fix format
BrunoQuaresma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,11 +99,44 @@ the api/queries folder when it is possible. | |
|
||
### Where to fetch data | ||
|
||
Finding the right place to fetch data in React apps is the million-dollar | ||
question, but we decided to make it only in the page components and pass the | ||
props down to the views. This makes it easier to find where data is being loaded | ||
and easy to test using Storybook. So you will see components like `UsersPage` | ||
and `UsersPageView`. | ||
In the past, our approach involved creating separate components for page and | ||
view, where the page component served as a container responsible for fetching | ||
data and passing it down to the view. | ||
|
||
For instance, when developing a page to display users, we would have a | ||
`UsersPage` component with a corresponding `UsersPageView`. The `UsersPage` | ||
would handle API calls, while the `UsersPageView` managed the presentational | ||
logic. | ||
|
||
Over time, however, we encountered challenges with this approach, particularly | ||
in terms of excessive props drilling. To address this, we opted to fetch data in | ||
proximity to its usage. Taking the example of displaying users, in the past, if | ||
we were creating a header component for that page, we would have needed to fetch | ||
the data in the page component and pass it down through the hierarchy | ||
(`UsersPage -> UsersPageView -> UsersHeader`). Now, with advancements such as | ||
`react-query`, data fetching can be performed directly in the `UsersHeader` | ||
component, eliminating the need for multiple places to handle the same request | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
([more info](https://github.com/TanStack/query/discussions/608#discussioncomment-29735)). | ||
|
||
To simplify visual testing of scenarios where components are responsible for | ||
fetching data, you can easily set the queries' value using `parameters.queries` | ||
within the component's story. | ||
|
||
```tsx | ||
export const WithQuota: Story = { | ||
parameters: { | ||
queries: [ | ||
{ | ||
key: getWorkspaceQuotaQueryKey(MockUser.username), | ||
data: { | ||
credits_consumed: 2, | ||
budget: 40, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
``` | ||
|
||
### API | ||
|
||
|
@@ -237,13 +270,16 @@ another page, you should probably consider using the **E2E** approach. | |
|
||
### Visual testing | ||
|
||
Test components without user interaction like testing if a page view is rendered | ||
correctly depending on some parameters, if the button is showing a spinner if | ||
the `loading` props are passing, etc. This should always be your first option | ||
since it is way easier to maintain. For this, we use | ||
Test components without user interaction like testing if a page/component is | ||
rendered correctly depending on some parameters, if the button is showing a | ||
spinner, if the `loading` props are passing, etc. This should always be your | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
first option since it is way easier to maintain. For this, we use | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I disagree with this, tbh. I think we're running into difficulties when we use visual testing as our first line of defense. Maybe something we can chat about more during the FE variety call! |
||
[Storybook](https://storybook.js.org/) and | ||
[Chromatic](https://www.chromatic.com/). | ||
|
||
> ℹ️ To learn more about testing components that fetch API data, refer to the | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
> [**Where to fetch data**](#where-to-fetch-data) section. | ||
|
||
### What should I test? | ||
|
||
Choosing what to test is not always easy since there are a lot of flows and a | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.