tests: add unit tests for zsa-react-query
## Summary
Adds a dedicated Jest test suite for the `zsa-react-query` package at `tests/jest/__tests__/react-query.test.tsx`. The package previously had **no dedicated test file** (the least-covered module in the repo). Its three hooks were only indirectly exercised by a few happy-path UI pages in `client.test.tsx`, which left several branches uncovered:
- `createServerActionsKeyFactory` had no direct assertions.
- The `returnError: true` branch of `useServerActionMutation` (which returns a `[data, err]` tuple instead of throwing) was never tested.
- The redirect / `notFound()` path (`if (!result) return`) was never tested for any of the three hooks.
- Pagination via `fetchNextPage` was not asserted to actually invoke the action with the next page param.
- Error pass-through (`[null, err]`) was never tested for `useServerActionQuery` or `useServerActionInfiniteQuery`.
The new suite renders each hook with `renderHook` from `@testing-library/react` and mocks zsa actions directly with `jest.fn`, so the tests are fast, hermetic, and do not depend on the Next.js app fixtures:
- `createServerActionsKeyFactory`: returns the factory unchanged and produces expected keys.
- `setupServerActionHooks`: returns the three hooks as functions; accepts `undefined` key factories.
- `useServerActionQuery`: happy path with input forwarding, error pass-through, action invocation is memoized across renders with the same input and re-runs on input change.
- `useServerActionMutation`: happy path with variable forwarding, throws on `[null, err]` when `returnError` is unset, returns `[data, err]` without setting `isError` when `returnError: true`, resolves to `undefined` when the action result is falsy (redirect / notFound).
- `useServerActionInfiniteQuery`: initial page load with computed input, `fetchNextPage` paginates correctly and `hasNextPage` flips to false when `getNextPageParam` returns `undefined`, error pass-through, falsy action result becomes an `undefined` page.
All 14 new tests pass. Total repo jest count goes from 130 → 144.
## Review & Testing Checklist for Human
- [ ] Run `npm run test:jest` and confirm all 144 tests pass, including the new `react-query.test.tsx` suite.
- [ ] Sanity-check the "redirect / falsy result" assertions match the intended behavior of `zsa-react-query` when a server action issues `redirect()` / `notFound()`. In particular, `useServerActionQuery` returns `undefined` from its `queryFn`, which in React Query v5 is surfaced as an error; the test here targets the mutation + infinite-query variants where the behavior is well-defined.
- [ ] Confirm the `returnError: true` behavior captured in the test matches the documented contract (mutation resolves with `[null, err]` and `isSuccess` stays `true`).
### Notes
- No production code changes — this is a tests-only PR.
- The existing repo-wide `npm run lint` fails on `main` with pre-existing errors in `zsa-react` source files that are unrelated to this PR.
Link to Devin session: https://localhost:3000/sessions/655c70d580e1427db7ab1fea37c603e5
Requested by: @IdoPesok
合并状态:未合并 3 条评论