2023-01-25 13:15:06 -03:00
|
|
|
|
# Frontend
|
|
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
Welcome to the guide for contributing to the Coder frontend. Whether you’re part
|
|
|
|
|
|
of the community or a Coder team member, this documentation will help you get
|
|
|
|
|
|
started.
|
|
|
|
|
|
|
|
|
|
|
|
If you have any questions, feel free to reach out on our
|
|
|
|
|
|
[Discord server](https://discord.com/invite/coder), and we’ll be happy to assist
|
2023-08-23 12:27:57 +03:00
|
|
|
|
you.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2023-03-08 18:50:55 -03:00
|
|
|
|
## Running the UI
|
|
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
You can run the UI and access the Coder dashboard in two ways:
|
2023-03-08 18:50:55 -03:00
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
1. Build the UI pointing to an external Coder server:
|
|
|
|
|
|
`CODER_HOST=https://mycoder.com pnpm dev` inside of the `site` folder. This
|
|
|
|
|
|
is helpful when you are building something in the UI and already have the
|
|
|
|
|
|
data on your deployed server.
|
|
|
|
|
|
2. Build the entire Coder server + UI locally: `./scripts/develop.sh` in the
|
|
|
|
|
|
root folder. This is useful for contributing to features that are not
|
|
|
|
|
|
deployed yet or that involve both the frontend and backend.
|
2023-03-08 18:50:55 -03:00
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
In both cases, you can access the dashboard on `http://localhost:8080`. If using
|
|
|
|
|
|
`./scripts/develop.sh` you can log in with the default credentials.
|
2023-03-08 18:50:55 -03:00
|
|
|
|
|
2025-01-03 14:13:46 -05:00
|
|
|
|
<blockquote class="admonition note">
|
|
|
|
|
|
|
|
|
|
|
|
**Default Credentials:** `admin@coder.com` and `SomeSecurePassword!`.
|
|
|
|
|
|
|
|
|
|
|
|
</blockquote>
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
## Tech Stack Overview
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2025-01-03 14:13:46 -05:00
|
|
|
|
All our dependencies are described in `site/package.json`, but the following are
|
|
|
|
|
|
the most important.
|
2024-09-10 14:52:05 -05:00
|
|
|
|
|
|
|
|
|
|
- [React](https://reactjs.org/) for the UI framework
|
2023-01-25 13:15:06 -03:00
|
|
|
|
- [Typescript](https://www.typescriptlang.org/) to keep our sanity
|
|
|
|
|
|
- [Vite](https://vitejs.dev/) to build the project
|
2023-08-15 13:03:14 -03:00
|
|
|
|
- [Material V5](https://mui.com/material-ui/getting-started/) for UI components
|
2023-01-25 13:15:06 -03:00
|
|
|
|
- [react-router](https://reactrouter.com/en/main) for routing
|
2023-08-23 12:27:57 +03:00
|
|
|
|
- [TanStack Query v4](https://tanstack.com/query/v4/docs/react/overview) for
|
|
|
|
|
|
fetching data
|
2023-01-25 13:15:06 -03:00
|
|
|
|
- [axios](https://github.com/axios/axios) as fetching lib
|
2023-10-04 08:11:07 -04:00
|
|
|
|
- [Playwright](https://playwright.dev/) for end-to-end (E2E) testing
|
2023-01-25 13:15:06 -03:00
|
|
|
|
- [Jest](https://jestjs.io/) for integration testing
|
2023-08-23 12:27:57 +03:00
|
|
|
|
- [Storybook](https://storybook.js.org/) and
|
|
|
|
|
|
[Chromatic](https://www.chromatic.com/) for visual testing
|
2024-09-10 14:52:05 -05:00
|
|
|
|
- [PNPM](https://pnpm.io/) as the package manager
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
## Structure
|
|
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
All UI-related code is in the `site` folder. Key directories include:
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2023-10-04 08:11:07 -04:00
|
|
|
|
- **e2e** - End-to-end (E2E) tests
|
2023-01-25 13:15:06 -03:00
|
|
|
|
- **src** - Source code
|
|
|
|
|
|
- **mocks** - [Manual mocks](https://jestjs.io/docs/manual-mocks) used by Jest
|
|
|
|
|
|
- **@types** - Custom types for dependencies that don't have defined types
|
2023-10-04 08:11:07 -04:00
|
|
|
|
(largely code that has no server-side equivalent)
|
2024-09-10 14:52:05 -05:00
|
|
|
|
- **api** - API function calls and types
|
2023-11-29 14:27:36 -03:00
|
|
|
|
- **queries** - react-query queries and mutations
|
2024-09-10 14:52:05 -05:00
|
|
|
|
- **components** - Reusable UI components without Coder specific business
|
|
|
|
|
|
logic
|
|
|
|
|
|
- **hooks** - Custom React hooks
|
|
|
|
|
|
- **modules** - Coder-specific UI components
|
|
|
|
|
|
- **pages** - Page-level components
|
|
|
|
|
|
- **testHelpers** - Helper functions for integration testing
|
|
|
|
|
|
- **theme** - theme configuration and color definitions
|
2023-01-25 13:15:06 -03:00
|
|
|
|
- **util** - Helper functions that can be used across the application
|
2024-09-10 14:52:05 -05:00
|
|
|
|
- **static** - Static assets like images, fonts, icons, etc
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
## Routing
|
|
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
We use [react-router](https://reactrouter.com/en/main) as our routing engine.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
- Authenticated routes - Place routes requiring authentication inside the
|
|
|
|
|
|
`<RequireAuth>` route. The `RequireAuth` component handles all the
|
|
|
|
|
|
authentication logic for the routes.
|
|
|
|
|
|
- Dashboard routes - routes that live in the dashboard should be placed under
|
|
|
|
|
|
the `<DashboardLayout>` route. The `DashboardLayout` adds a navbar and passes
|
|
|
|
|
|
down common dashboard data.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
## Pages
|
|
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
Page components are the top-level components of the app and reside in the
|
|
|
|
|
|
`src/pages` folder. Each page should have its own folder to group relevant
|
|
|
|
|
|
views, tests, and utility functions. The page component fetches necessary data
|
|
|
|
|
|
and passes to the view. We explain this decision a bit better in the next
|
|
|
|
|
|
section which talks about where to fetch data.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
> ℹ️ If code within a page becomes reusable across other parts of the app,
|
|
|
|
|
|
> consider moving it to `src/utils`, `hooks`, `components`, or `modules`.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
### Handling States
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
A page typically has three states: **loading**, **ready**/**success**, and
|
|
|
|
|
|
**error**. Ensure you manage these states when developing pages. Use visual
|
|
|
|
|
|
tests for these states with `*.stories.ts` files.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
## Data Fetching
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2023-11-29 14:27:36 -03:00
|
|
|
|
We use [TanStack Query v4](https://tanstack.com/query/v4/docs/react/quick-start)
|
2024-09-10 14:52:05 -05:00
|
|
|
|
to fetch data from the API. Queries and mutation should be placed in the
|
|
|
|
|
|
api/queries folder.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
### Where to fetch data
|
|
|
|
|
|
|
2024-01-03 09:27:33 -03:00
|
|
|
|
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 libraries such as
|
|
|
|
|
|
`react-query`, data fetching can be performed directly in the `UsersHeader`
|
|
|
|
|
|
component, allowing UI elements to declare and consume their data-fetching
|
|
|
|
|
|
dependencies directly, while preventing duplicate server requests
|
|
|
|
|
|
([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 = {
|
2025-01-03 18:12:59 +05:00
|
|
|
|
parameters: {
|
|
|
|
|
|
queries: [
|
|
|
|
|
|
{
|
|
|
|
|
|
key: getWorkspaceQuotaQueryKey(MockUser.username),
|
|
|
|
|
|
data: {
|
|
|
|
|
|
credits_consumed: 2,
|
|
|
|
|
|
budget: 40,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
2024-01-03 09:27:33 -03:00
|
|
|
|
};
|
|
|
|
|
|
```
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
### API
|
|
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
Our project uses [axios](https://github.com/axios/axios) as the HTTP client for
|
|
|
|
|
|
making API requests. The API functions are centralized in `site/src/api/api.ts`.
|
|
|
|
|
|
Auto-generated TypeScript types derived from our Go server are located in
|
|
|
|
|
|
`site/src/api/typesGenerated.ts`.
|
|
|
|
|
|
|
|
|
|
|
|
Typically, each API endpoint corresponds to its own `Request` and `Response`
|
|
|
|
|
|
types. However, some endpoints require additional parameters for successful
|
|
|
|
|
|
execution. Here's an illustrative example:"
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
|
export const getAgentListeningPorts = async (
|
2025-01-03 18:12:59 +05:00
|
|
|
|
agentID: string,
|
2023-01-25 13:15:06 -03:00
|
|
|
|
): Promise<TypesGen.ListeningPortsResponse> => {
|
2025-01-03 18:12:59 +05:00
|
|
|
|
const response = await axiosInstance.get(
|
|
|
|
|
|
`/api/v2/workspaceagents/${agentID}/listening-ports`,
|
|
|
|
|
|
);
|
|
|
|
|
|
return response.data;
|
2023-01-25 13:15:06 -03:00
|
|
|
|
};
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
Sometimes, a frontend operation can have multiple API calls which can be wrapped
|
|
|
|
|
|
as a single function.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
|
export const updateWorkspaceVersion = async (
|
2025-01-03 18:12:59 +05:00
|
|
|
|
workspace: TypesGen.Workspace,
|
2023-01-25 13:15:06 -03:00
|
|
|
|
): Promise<TypesGen.WorkspaceBuild> => {
|
2025-01-03 18:12:59 +05:00
|
|
|
|
const template = await getTemplate(workspace.template_id);
|
|
|
|
|
|
return startWorkspace(workspace.id, template.active_version_id);
|
2023-01-25 13:15:06 -03:00
|
|
|
|
};
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
## Components and Modules
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
Components should be atomic, reusable and free of business logic. Modules are
|
|
|
|
|
|
similar to components except that they can be more complex and can contain
|
|
|
|
|
|
business logic specific to the product.
|
|
|
|
|
|
|
|
|
|
|
|
### MUI
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2023-10-04 08:11:07 -04:00
|
|
|
|
The codebase is currently using MUI v5. Please see the
|
|
|
|
|
|
[official documentation](https://mui.com/material-ui/getting-started/). In
|
|
|
|
|
|
general, favor building a custom component via MUI instead of plain React/HTML,
|
|
|
|
|
|
as MUI's suite of components is thoroughly battle-tested and accessible right
|
|
|
|
|
|
out of the box.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
### Structure
|
|
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
Each component and module gets its own folder. Module folders may group multiple
|
|
|
|
|
|
files in a hierarchical structure. Storybook stories and component tests using
|
|
|
|
|
|
Storybook interactions are required. By keeping these tidy, the codebase will
|
2023-08-23 12:27:57 +03:00
|
|
|
|
remain easy to navigate, healthy and maintainable for all contributors.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
### Accessibility
|
|
|
|
|
|
|
2023-10-04 08:11:07 -04:00
|
|
|
|
We strive to keep our UI accessible.
|
|
|
|
|
|
|
|
|
|
|
|
In general, colors should come from the app theme, but if there is a need to add
|
|
|
|
|
|
a custom color, please ensure that the foreground and background have a minimum
|
|
|
|
|
|
contrast ratio of 4.5:1 to meet WCAG level AA compliance. WebAIM has
|
|
|
|
|
|
[a great tool for checking your colors directly](https://webaim.org/resources/contrastchecker/),
|
|
|
|
|
|
but tools like
|
|
|
|
|
|
[Dequeue's axe DevTools](https://chrome.google.com/webstore/detail/axe-devtools-web-accessib/lhdoppojpmngadmnindnejefpokejbdd)
|
|
|
|
|
|
can also do automated checks in certain situations.
|
|
|
|
|
|
|
|
|
|
|
|
When using any kind of input element, always make sure that there is a label
|
|
|
|
|
|
associated with that element (the label can be made invisible for aesthetic
|
|
|
|
|
|
reasons, but it should always be in the HTML markup). Labels are important for
|
|
|
|
|
|
screen-readers; a placeholder text value is not enough for all users.
|
|
|
|
|
|
|
|
|
|
|
|
When possible, make sure that all image/graphic elements have accompanying text
|
|
|
|
|
|
that describes the image. `<img />` elements should have an `alt` text value. In
|
|
|
|
|
|
other situations, it might make sense to place invisible, descriptive text
|
|
|
|
|
|
inside the component itself using MUI's `visuallyHidden` utility function.
|
|
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
|
import { visuallyHidden } from "@mui/utils";
|
|
|
|
|
|
|
|
|
|
|
|
<Button>
|
2025-01-03 18:12:59 +05:00
|
|
|
|
<GearIcon />
|
|
|
|
|
|
<Box component="span" sx={visuallyHidden}>
|
|
|
|
|
|
Settings
|
|
|
|
|
|
</Box>
|
2023-10-04 08:11:07 -04:00
|
|
|
|
</Button>;
|
|
|
|
|
|
```
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
### Should I create a new component or module?
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
Components could technically be used in any codebase and still feel at home. A
|
|
|
|
|
|
module would only make sense in the Coder codebase.
|
|
|
|
|
|
|
|
|
|
|
|
- Component
|
|
|
|
|
|
- Simple
|
|
|
|
|
|
- Atomic, used in multiple places
|
|
|
|
|
|
- Generic, would be useful as a component outside of the Coder product
|
|
|
|
|
|
- Good Examples: `Badge`, `Form`, `Timeline`
|
|
|
|
|
|
- Module
|
|
|
|
|
|
- Simple or Complex
|
|
|
|
|
|
- Used in multiple places
|
|
|
|
|
|
- Good Examples: `Provisioner`, `DashboardLayout`, `DeploymentBanner`
|
|
|
|
|
|
|
|
|
|
|
|
Our codebase has some legacy components that are being updated to follow these
|
|
|
|
|
|
new conventions, but all new components should follow these guidelines.
|
|
|
|
|
|
|
|
|
|
|
|
## Styling
|
|
|
|
|
|
|
|
|
|
|
|
We use [Emotion](https://emotion.sh/) to handle css styles.
|
|
|
|
|
|
|
|
|
|
|
|
## Forms
|
|
|
|
|
|
|
|
|
|
|
|
We use [Formik](https://formik.org/docs) for forms along with
|
|
|
|
|
|
[Yup](https://github.com/jquense/yup) for schema definition and validation.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
## Testing
|
|
|
|
|
|
|
2023-10-04 08:11:07 -04:00
|
|
|
|
We use three types of testing in our app: **End-to-end (E2E)**, **Integration**
|
|
|
|
|
|
and **Visual Testing**.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2023-10-04 08:11:07 -04:00
|
|
|
|
### End-to-End (E2E)
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2023-10-04 08:11:07 -04:00
|
|
|
|
These are useful for testing complete flows like "Create a user", "Import
|
|
|
|
|
|
template", etc. We use [Playwright](https://playwright.dev/). If you only need
|
|
|
|
|
|
to test if the page is being rendered correctly, you should consider using the
|
|
|
|
|
|
**Visual Testing** approach.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2023-08-23 12:27:57 +03:00
|
|
|
|
> ℹ️ For scenarios where you need to be authenticated, you can use
|
|
|
|
|
|
> `test.use({ storageState: getStatePath("authState") })`.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2023-11-09 13:26:26 +02:00
|
|
|
|
For ease of debugging, it's possible to run a Playwright test in headful mode
|
|
|
|
|
|
running a Playwright server on your local machine, and executing the test inside
|
|
|
|
|
|
your workspace.
|
|
|
|
|
|
|
|
|
|
|
|
You can either run `scripts/remote_playwright.sh` from `coder/coder` on your
|
|
|
|
|
|
local machine, or execute the following command if you don't have the repo
|
|
|
|
|
|
available:
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
bash <(curl -sSL https://raw.githubusercontent.com/coder/coder/main/scripts/remote_playwright.sh) [workspace]
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
The `scripts/remote_playwright.sh` script will start a Playwright server on your
|
|
|
|
|
|
local machine and forward the necessary ports to your workspace. At the end of
|
|
|
|
|
|
the script, you will land _inside_ your workspace with environment variables set
|
|
|
|
|
|
so you can simply execute the test (`pnpm run playwright:test`).
|
|
|
|
|
|
|
2023-01-25 13:15:06 -03:00
|
|
|
|
### Integration
|
|
|
|
|
|
|
2023-08-23 12:27:57 +03:00
|
|
|
|
Test user interactions like "Click in a button shows a dialog", "Submit the form
|
|
|
|
|
|
sends the correct data", etc. For this, we use [Jest](https://jestjs.io/) and
|
|
|
|
|
|
[react-testing-library](https://testing-library.com/docs/react-testing-library/intro/).
|
|
|
|
|
|
If the test involves routing checks like redirects or maybe checking the info on
|
|
|
|
|
|
another page, you should probably consider using the **E2E** approach.
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
### Visual testing
|
|
|
|
|
|
|
2024-01-03 09:27:33 -03:00
|
|
|
|
We use visual tests to test components without user interaction like testing if
|
|
|
|
|
|
a page/component is rendered correctly depending on some parameters, if a button
|
|
|
|
|
|
is showing a spinner, if `loading` props are passed correctly, etc. This should
|
|
|
|
|
|
always be your first option since it is way easier to maintain. For this, we use
|
2023-08-23 12:27:57 +03:00
|
|
|
|
[Storybook](https://storybook.js.org/) and
|
|
|
|
|
|
[Chromatic](https://www.chromatic.com/).
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
2024-01-03 09:27:33 -03:00
|
|
|
|
> ℹ️ To learn more about testing components that fetch API data, refer to the
|
|
|
|
|
|
> [**Where to fetch data**](#where-to-fetch-data) section.
|
|
|
|
|
|
|
2023-01-25 13:15:06 -03:00
|
|
|
|
### What should I test?
|
|
|
|
|
|
|
2023-08-23 12:27:57 +03:00
|
|
|
|
Choosing what to test is not always easy since there are a lot of flows and a
|
|
|
|
|
|
lot of things can happen but these are a few indicators that can help you with
|
|
|
|
|
|
that:
|
2023-01-25 13:15:06 -03:00
|
|
|
|
|
|
|
|
|
|
- Things that can block the user
|
|
|
|
|
|
- Reported bugs
|
|
|
|
|
|
- Regression issues
|
2023-03-14 10:54:17 -03:00
|
|
|
|
|
|
|
|
|
|
### Tests getting too slow
|
|
|
|
|
|
|
2024-09-10 14:52:05 -05:00
|
|
|
|
You may have observed that certain tests in our suite can be notably
|
|
|
|
|
|
time-consuming. Sometimes it is because the test itself is complex and sometimes
|
|
|
|
|
|
it is because of how the test is querying elements.
|
2023-03-14 10:54:17 -03:00
|
|
|
|
|
|
|
|
|
|
#### Using `ByRole` queries
|
|
|
|
|
|
|
2023-08-23 12:27:57 +03:00
|
|
|
|
One thing we figured out that was slowing down our tests was the use of `ByRole`
|
|
|
|
|
|
queries because of how it calculates the role attribute for every element on the
|
|
|
|
|
|
`screen`. You can read more about it on the links below:
|
2023-03-14 10:54:17 -03:00
|
|
|
|
|
2025-01-03 18:12:59 +05:00
|
|
|
|
- <https://stackoverflow.com/questions/69711888/react-testing-library-getbyrole-is-performing-extremely-slowly>
|
|
|
|
|
|
- <https://github.com/testing-library/dom-testing-library/issues/552#issuecomment-625172052>
|
2023-03-14 10:54:17 -03:00
|
|
|
|
|
2023-08-23 12:27:57 +03:00
|
|
|
|
Even with `ByRole` having performance issues we still want to use it but for
|
|
|
|
|
|
that, we have to scope the "querying" area by using the `within` command. So
|
|
|
|
|
|
instead of using `screen.getByRole("button")` directly we could do
|
|
|
|
|
|
`within(form).getByRole("button")`.
|
2023-03-14 10:54:17 -03:00
|
|
|
|
|
2023-08-23 12:27:57 +03:00
|
|
|
|
❌ Not ideal. If the screen has a hundred or thousand elements it can be VERY
|
|
|
|
|
|
slow.
|
2023-03-14 10:54:17 -03:00
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
|
user.click(screen.getByRole("button"));
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
✅ Better. We can limit the number of elements we are querying.
|
|
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
|
const form = screen.getByTestId("form");
|
|
|
|
|
|
user.click(within(form).getByRole("button"));
|
|
|
|
|
|
```
|
2023-05-17 13:56:26 -03:00
|
|
|
|
|
|
|
|
|
|
❌ Does not work
|
|
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
|
import { getUpdateCheck } from "api/api"
|
|
|
|
|
|
|
|
|
|
|
|
createMachine({ ... }, {
|
|
|
|
|
|
services: {
|
|
|
|
|
|
getUpdateCheck,
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
✅ It works
|
|
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
|
import { getUpdateCheck } from "api/api"
|
|
|
|
|
|
|
|
|
|
|
|
createMachine({ ... }, {
|
|
|
|
|
|
services: {
|
|
|
|
|
|
getUpdateCheck: () => getUpdateCheck(),
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
```
|