# Testing

Technical reference for testing tools, coverage targets, custom renderer API, and query priority in the Ledger Wallet monorepo.

---

## Testing Requirements

| Scenario                                   | Requirement                                                                      |
| ------------------------------------------ | -------------------------------------------------------------------------------- |
| **New features** in `features/` or `mvvm/` | Must include unit and integration tests                                          |
| **Bug fixes** in new architecture          | Must include a regression test covering the use case                             |
| **New components** in legacy code          | Use new arch patterns to ease future migration                                   |
| **Major feature reworks** in legacy code   | Consider full migration to new architecture with tests (coordinate with Product) |

---

## Testing Tools

| Tool                             | Purpose                                              | Documentation                                                                                  |
| -------------------------------- | ---------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| **Jest**                         | JavaScript test runner with DOM access via jsdom     | [Getting Started](https://jestjs.io/docs/getting-started)                                      |
| **React Testing Library**        | Test React components without implementation details | [Documentation](https://testing-library.com/docs/react-testing-library/intro/)                 |
| **React Native Testing Library** | Test React Native components (for LLM)               | [Documentation](https://callstack.github.io/react-native-testing-library/docs/getting-started) |
| **Mock Service Worker (MSW)**    | API mocking for network requests                     | [Documentation](https://mswjs.io/)                                                             |

---

## Query Priority

When writing tests, follow the query priority recommended by Testing Library:

- **React**: [About Queries](https://testing-library.com/docs/queries/about#priority)
- **React Native**: [How Should I Query](https://callstack.github.io/react-native-testing-library/docs/how-should-i-query)

> **Note:** Use `ByRole` queries when possible. If your component lacks aria attributes,
> consider adding them when it makes sense. Otherwise, use other queries
> following the priority guide.

---

## Coverage Targets

| Type                        | Target | Notes                                                                        |
| --------------------------- | ------ | ---------------------------------------------------------------------------- |
| **Features / User Stories** | >= 80% | Complex features with multiple conditions                                    |
| **Generic Components**      | >= 90% | Reusable components used in different contexts                               |
| **Helper Functions**        | 100%   | Pure functions like date formatting, number parsing                          |
| **Redux Actions/Reducers**  | 100%   | Well-documented at [Redux Testing](https://redux.js.org/usage/writing-tests) |

> **Note:** Do not focus solely on coverage scores. Use coverage as a tool to identify
> untested code paths.

---

## Custom Renderer API

### Mobile (ledger-live-mobile)

Import from `@tests/test-renderer`:

```typescript
import { render, screen, renderHook } from "@tests/test-renderer";
```

#### Available Render Functions

| Function                              | Use Case                             |
| ------------------------------------- | ------------------------------------ |
| `render`                              | Render components with all providers |
| `renderHook`                          | Test custom hooks in isolation       |
| `renderWithReactQuery`                | Components needing React Query       |
| `customRenderHookWithLiveAppProvider` | Hooks needing Live App context       |

#### Providers Included

- `Provider` (Redux)
- `FirebaseFeatureFlagsProvider` (Feature Flags)
- `NavigationContainer` (React Navigation routing)
- `CountervaluesProvider` / `CountervaluesMarketcapBridgedProvider`
- `StyleProvider` (theming)
- `I18nextProvider` (translations)
- `BottomSheetModalProvider`
- `QueuedDrawersContextProvider`
- `AnalyticsContextProvider`
- `QueryClientProvider` (when using `renderWithReactQuery`)

### Desktop (ledger-live-desktop)

Import from `tests/testSetup`:

```typescript
import { render, screen, renderHook, userEvent } from "tests/testSetup";
```

#### Available Render Functions

| Function                                | Use Case                             |
| --------------------------------------- | ------------------------------------ |
| `render`                                | Render components with all providers |
| `renderHook`                            | Test custom hooks in isolation       |
| `renderWithMockedCounterValuesProvider` | Components needing countervalues     |
| `renderHookWithLiveAppProvider`         | Hooks needing Live App context       |

#### Providers Included

- `QueryClientProvider` (React Query)
- `Provider` (Redux)
- `FirebaseFeatureFlagsProvider` (Feature Flags)
- `MemoryRouter` (React Router routing — supports `initialRoute` option)
- `CountervaluesProvider` / `CountervaluesMarketcapProvider`
- `I18nextProvider` (translations)
- `DrawerProvider`
- `DialogProvider`
- `StyleProvider` (theming)
- `ContextMenuWrapper`

---

## Modules That Don't Need Mocking

When using the custom renderer, the following modules are already provided:

| Module                                        | Why No Mock Needed                                        |
| --------------------------------------------- | --------------------------------------------------------- |
| `react-i18next` / `useTranslation`            | `I18nextProvider` is included with real i18n instance     |
| `react-redux` / `useSelector` / `useDispatch` | Real Redux `Provider` with configurable store             |
| `useTheme` / Theme hooks                      | `StyleProvider` is included                               |
| `useFeature` / Feature flags                  | `FirebaseFeatureFlagsProvider` is included                |
| Navigation hooks                              | `NavigationContainer` (Mobile) / `MemoryRouter` (Desktop) |
| `useAnalytics`                                | `AnalyticsContextProvider` is included (Mobile)           |

---

## Notes

- Always prefer the custom render utilities over raw `@testing-library` render
- Use `overrideInitialState` (Mobile) or `initialState` (Desktop) to customize Redux state
- Use `initialRoute` option (Desktop only) to set the starting route for navigation tests
- The `user` object returned provides realistic user interaction simulation
- The `store` object can be used to verify state changes after actions
- Feature flags can be customized via the `getFeature` function in the test setup files

---

## Internal Resources

> **Note:** The following internal resources are available to Ledger team members:* Testing Tools Setup
> * Testing Tips and Tricks
> * Warnings and Errors Policy

---

## See also

- [How to Write Tests](../how-to/write-tests) — Step-by-step guide for writing tests
- [How to Use the Custom Renderer](../how-to/use-custom-renderer) — Practical usage of render functions
- [Testing Strategy](../explanation/testing-strategy) — Why we test the way we do
