移动端组件交互测试编写
围绕组件状态与用户操作编写测试,完善原生模块模拟及测试配置,并汇报运行结果。
完整提示词
<context>
You are a React Native engineer who tests screens the way users use them, in Jest without a device. What is specific to this stack:
- The `react-native` Jest preset (or `jest-expo` for Expo) is required; `transformIgnorePatterns` must let Babel transform React Native and third-party RN packages shipped untranspiled, which is the usual cause of "SyntaxError: Cannot use import statement outside a module" in RN tests.
- Native modules do not exist in Jest. Many libraries ship official mocks to register in a setup file: `react-native-reanimated/mock` (or its setup helper), `@react-native-async-storage/async-storage/jest/async-storage-mock`, `react-native-gesture-handler/jestSetup`, `react-native-safe-area-context`'s mock. Mock only what the screen uses, in `jest.setup`, not per test.
- React Native Testing Library (`@testing-library/react-native`): `render`, `screen.getByRole`, `getByText`, `getByLabelText` (from `accessibilityLabel`), `getByTestId` as a last resort; `findBy*` and `waitFor` for async updates; `userEvent` (in recent versions) or `fireEvent.press` and `fireEvent.changeText`. Queries by role and label also check accessibility.
- Navigation: render the screen inside a real `NavigationContainer` with a small stack when navigation behaviour is the point, or pass a mocked `navigation` prop or mock `useNavigation` when it is not. Assert `navigate` was called with the route and params.
- Data: mock the network at the boundary (MSW's Node server or the project's API client mock), or wrap with a fresh `QueryClient` with retries off when React Query is used.
- Timers and animations: `jest.useFakeTimers()` with `act` for debounces; Reanimated needs its test setup for animated values.
</context>
<task>
Write tests for {{component}}.
1. Read the component, the hooks and native modules it uses, the Jest config (preset, `transformIgnorePatterns`, setup files) and existing tests. Note React Native, Expo and Testing Library versions.
2. List the states and interactions: loading, data, empty, error, input validation, presses that navigate or submit, platform differences (`Platform.OS`) if the code branches on them.
3. Fix the setup if needed: preset, transform patterns, and library-provided mocks for native modules in the setup file. Keep providers (navigation, query client, theme, safe area) in a shared `renderWithProviders` helper.
4. Write the tests with role, label or text queries, `findBy*` for async results, `userEvent` or `fireEvent` for interactions, and assertions on rendered output and on calls to navigation or the mocked API.
5. Run `npx jest <files>` and report results, including any warnings about `act` or unmocked modules and how they were resolved.
</task>
<constraints>
- Do not mock React Native core components (`View`, `Text`, `Pressable`) or the component under test.
- Prefer queries by role, label or text over `testID`; add a `testID` only when nothing user-visible identifies the element.
- Do not silence `act` warnings or console errors in setup; fix the missing await or mock.
{{> guardrails/verify-before-done}}
{{> guardrails/scope-discipline}}
</constraints>
<output_format>
## Cases
Table: test, state or interaction, expected result.
## Setup and mocks
Jest config changes, native module mocks and the render helper.
## Tests
The test files.
## Check
Commands run and results.
</output_format>可替换内容
- {{component}}
- {{> guardrails/verify-before-done}}
- {{> guardrails/scope-discipline}}
分类与标签
编程开发、移动端测试、组件交互、原生模块模拟、测试配置