From 4f41d267b962f9ac08d4fdaef350159711b20bcb Mon Sep 17 00:00:00 2001 From: Rudy Fraser Date: Tue, 10 Nov 2026 16:25:33 -0500 Subject: [PATCH] test(merge): load Reanimated under Jest and move hook tests to the lex clients --- jest/jestSetup.js | 6 +++ .../events/__tests__/rsvp-metrics.test.tsx | 24 ++++++------ src/features/events/queries.ts | 4 +- .../__tests__/community-post-targets.test.tsx | 6 +-- .../queries/__tests__/peer-mod-label.test.tsx | 13 ++++--- .../saved-feeds-placeholder.test.tsx | 2 +- .../notifications/__tests__/unread.test.tsx | 38 ++++++++++++------- .../__tests__/space-engagement.test.tsx | 11 ++---- 8 files changed, 60 insertions(+), 44 deletions(-) diff --git a/jest/jestSetup.js b/jest/jestSetup.js index c4ea5840a..d2a146bfe 100644 --- a/jest/jestSetup.js +++ b/jest/jestSetup.js @@ -17,6 +17,12 @@ jest.mock('react-native-worklets', () => require('react-native-worklets/src/mock'), ) +// Reanimated registers a CSS event handler on import, which its JS-only test +// module rejects, so nothing that imports Reanimated loads under Jest. +jest.mock('react-native-reanimated/lib/module/initializers.native', () => ({ + initializeReanimatedModule: () => {}, +})) + jest.mock('@react-native-async-storage/async-storage', () => require('@react-native-async-storage/async-storage/jest/async-storage-mock'), ) diff --git a/src/features/events/__tests__/rsvp-metrics.test.tsx b/src/features/events/__tests__/rsvp-metrics.test.tsx index f69cc615e..db91f7c9c 100644 --- a/src/features/events/__tests__/rsvp-metrics.test.tsx +++ b/src/features/events/__tests__/rsvp-metrics.test.tsx @@ -10,15 +10,17 @@ jest.mock('#/analytics', () => ({ useAnalytics: () => ({metric: mockMetric}), })) jest.mock('#/state/session', () => ({ - useAgent: () => ({ - com: { - atproto: { - repo: { - createRecord: mockCreateRecord, - putRecord: mockPutRecord, - deleteRecord: mockDeleteRecord, - }, - }, + usePdsClient: () => ({ + call: (method: unknown, input: unknown) => { + const {com} = jest.requireActual('#/lexicons') + if (method === com.atproto.repo.createRecord) { + return mockCreateRecord(input) + } + if (method === com.atproto.repo.putRecord) return mockPutRecord(input) + if (method === com.atproto.repo.deleteRecord) { + return mockDeleteRecord(input) + } + throw new Error('unexpected call') }, }), useSession: () => ({currentAccount: {did: 'did:plc:me'}}), @@ -67,8 +69,8 @@ async function rsvp( describe('RSVP metrics', () => { beforeEach(() => { jest.clearAllMocks() - mockCreateRecord.mockResolvedValue({data: {uri: RSVP_URI}}) - mockPutRecord.mockResolvedValue({data: {uri: RSVP_URI}}) + mockCreateRecord.mockResolvedValue({uri: RSVP_URI}) + mockPutRecord.mockResolvedValue({uri: RSVP_URI}) mockDeleteRecord.mockResolvedValue({}) }) diff --git a/src/features/events/queries.ts b/src/features/events/queries.ts index 978b1cfa4..cbaa4a69d 100644 --- a/src/features/events/queries.ts +++ b/src/features/events/queries.ts @@ -11,7 +11,7 @@ import { import {communityXrpc} from '#/lib/api/community' import {STALE} from '#/state/queries' -import {useAppviewClient, useSession} from '#/state/session' +import {useAppviewClient, usePdsClient, useSession} from '#/state/session' import {useAnalytics} from '#/analytics' import {applyRsvp, type RsvpLogContext, rsvpMetricStatus} from './rsvp-state' import {mergeEventTimeline} from './timeline' @@ -192,7 +192,7 @@ export type RsvpChange = { */ export function useRsvpMutation(event: EventView, logContext: RsvpLogContext) { const ax = useAnalytics() - const client = useAppviewClient() + const client = usePdsClient() const queryClient = useQueryClient() const {currentAccount} = useSession() const queryKey = EVENT_RQKEY(event.uri) diff --git a/src/state/queries/__tests__/community-post-targets.test.tsx b/src/state/queries/__tests__/community-post-targets.test.tsx index 3c3d10ee1..66f339b05 100644 --- a/src/state/queries/__tests__/community-post-targets.test.tsx +++ b/src/state/queries/__tests__/community-post-targets.test.tsx @@ -3,7 +3,7 @@ import {renderHook, waitFor} from '@testing-library/react-native' const mockFetchCommunityFeedTarget = jest.fn() const mockUseSavedFeeds = jest.fn() -const mockAgent = {} +const mockClient = {} jest.mock('#/lib/api/community-feed', () => ({ fetchCommunityFeedTarget: (...args: unknown[]) => @@ -13,7 +13,7 @@ jest.mock('#/state/queries/feed', () => ({ useSavedFeeds: () => mockUseSavedFeeds(), })) jest.mock('#/state/session', () => ({ - useAgent: () => mockAgent, + useAppviewClient: () => mockClient, useSession: () => ({currentAccount: {did: 'did:plc:test'}}), })) @@ -59,7 +59,7 @@ describe('useCommunityPostTargets', () => { expect(mockFetchCommunityFeedTarget).toHaveBeenCalledTimes(1) expect(mockFetchCommunityFeedTarget).toHaveBeenCalledWith( - mockAgent, + mockClient, FEED_URI, ) expect(result.current.data).toEqual([TARGET]) diff --git a/src/state/queries/__tests__/peer-mod-label.test.tsx b/src/state/queries/__tests__/peer-mod-label.test.tsx index 9f9b8593a..39c6e8778 100644 --- a/src/state/queries/__tests__/peer-mod-label.test.tsx +++ b/src/state/queries/__tests__/peer-mod-label.test.tsx @@ -10,8 +10,9 @@ import { const mockQueryLabels = jest.fn() const mockCommunityXrpc = jest.fn() const mockWarn = jest.fn() -const mockAgent = { - com: {atproto: {label: {queryLabels: mockQueryLabels}}}, +const mockClient = { + call: (_method: unknown, params: unknown, opts: unknown) => + mockQueryLabels(params, opts), } jest.mock('#/lib/api/community', () => ({ @@ -21,7 +22,7 @@ jest.mock('#/logger', () => ({ logger: {warn: (...args: unknown[]) => mockWarn(...args)}, })) jest.mock('#/state/session', () => ({ - useAgent: () => mockAgent, + useAppviewClient: () => mockClient, })) jest.mock('#/state/queries/community-feed', () => ({ COMMUNITY_POST_RQKEY: (uri: string) => ['community-post', uri], @@ -47,7 +48,7 @@ const ATTEMPTS = 8 const RENDER_KEYS = [['post', URI], ['post-feed'], ['post-thread-v2']] const labelsResponse = (vals: string[]) => ({ - data: {labels: vals.map(val => ({val, uri: URI, src: 'did:plc:mod'}))}, + labels: vals.map(val => ({val, uri: URI, src: 'did:plc:mod'})), }) function setup() { @@ -147,9 +148,9 @@ describe('peer-mod label reconciliation', () => { expect(BLUESKY_PROXY_HEADER.get()).toBe(BLUESKY_FALLBACK_PROXY_HEADER) expect(mockQueryLabels.mock.calls.length).toBeGreaterThanOrEqual(2) for (const [, opts] of mockQueryLabels.mock.calls as Array< - [unknown, {headers?: unknown}] + [unknown, {service?: unknown}] >) { - expect(opts.headers).toEqual(HOME_APPVIEW_PINNED_OPTS.headers) + expect(opts.service).toBe(HOME_APPVIEW_PINNED_OPTS.service) } }) diff --git a/src/state/queries/__tests__/saved-feeds-placeholder.test.tsx b/src/state/queries/__tests__/saved-feeds-placeholder.test.tsx index 7d1197a42..3f5d77305 100644 --- a/src/state/queries/__tests__/saved-feeds-placeholder.test.tsx +++ b/src/state/queries/__tests__/saved-feeds-placeholder.test.tsx @@ -2,7 +2,7 @@ import {QueryClient, QueryClientProvider} from '@tanstack/react-query' import {renderHook} from '@testing-library/react-native' jest.mock('#/state/session', () => ({ - useAgent: () => ({}), + useAppviewClient: () => ({}), useSession: () => ({currentAccount: undefined}), })) jest.mock('#/state/queries/preferences', () => ({ diff --git a/src/state/queries/notifications/__tests__/unread.test.tsx b/src/state/queries/notifications/__tests__/unread.test.tsx index 54a83eb56..e4b24cee6 100644 --- a/src/state/queries/notifications/__tests__/unread.test.tsx +++ b/src/state/queries/notifications/__tests__/unread.test.tsx @@ -10,13 +10,9 @@ const mockTruncateAndInvalidate = jest.fn() const mockUpdateSeen = jest.fn().mockResolvedValue(undefined) const mockBroadcastPostMessage = jest.fn() let mockBroadcastListener: ((event: MessageEvent) => void) | undefined -const mockAgent = { - session: {did: 'did:plc:viewer'}, - app: { - bsky: { - notification: {updateSeen: mockUpdateSeen}, - }, - }, +const mockClient = { + did: 'did:plc:viewer', + call: (...args: unknown[]) => mockUpdateSeen(...args), } jest.mock('#/lib/api/community-notifications', () => ({ @@ -46,18 +42,23 @@ jest.mock('#/state/preferences/moderation-opts', () => ({ useModerationOpts: () => undefined, })) jest.mock('#/state/queries/util', () => ({ + ...jest.requireActual( + '#/state/queries/util', + ), truncateAndInvalidate: (...args: unknown[]) => mockTruncateAndInvalidate(...args), })) jest.mock('#/state/session', () => ({ - useAgent: () => mockAgent, - useSession: () => ({hasSession: false}), + useAppviewClient: () => mockClient, + useSession: () => ({hasSession: true}), })) jest.mock('../util', () => ({ + ...jest.requireActual('../util'), fetchPage: (...args: unknown[]) => mockFetchPage(...args), })) import {HOME_APPVIEW_PINNED_OPTS} from '#/lib/constants' +import {app} from '#/lexicons' import {type FeedPage} from '../types' import { Provider, @@ -108,16 +109,17 @@ const visiblePage: FeedPage = { ], } +function setAppState(value: 'active' | 'background') { + Object.defineProperty(AppState, 'currentState', {configurable: true, value}) +} + describe('notification unread synchronization', () => { let queryClient: QueryClient beforeEach(() => { jest.clearAllMocks() mockBroadcastListener = undefined - Object.defineProperty(AppState, 'currentState', { - configurable: true, - value: 'active', - }) + setAppState('active') queryClient = new QueryClient({ defaultOptions: {queries: {retry: false}}, }) @@ -131,14 +133,21 @@ describe('notification unread synchronization', () => { ) } + /* + * The provider checks once on mount. Mounting in the background skips that + * check, so each test drives every request itself. + */ function setup() { - return renderHook( + setAppState('background') + const hook = renderHook( () => ({ count: useUnreadNotifications(), api: useUnreadNotificationsApi(), }), {wrapper}, ) + setAppState('active') + return hook } it('clears the badge when the filtered list is empty', async () => { @@ -200,6 +209,7 @@ describe('notification unread synchronization', () => { expect(result.current.count).toBe('') expect(result.current.api.getCachedUnreadPage()).toBeUndefined() expect(mockUpdateSeen).toHaveBeenCalledWith( + app.bsky.notification.updateSeen, {seenAt: expect.any(String)}, HOME_APPVIEW_PINNED_OPTS, ) diff --git a/src/view/com/post-thread/__tests__/space-engagement.test.tsx b/src/view/com/post-thread/__tests__/space-engagement.test.tsx index b654fe45f..1fe406f2d 100644 --- a/src/view/com/post-thread/__tests__/space-engagement.test.tsx +++ b/src/view/com/post-thread/__tests__/space-engagement.test.tsx @@ -4,16 +4,13 @@ import {render, waitFor} from '@testing-library/react-native' const mockGetSpacePostLikes = jest.fn() const mockGetSpacePostQuotes = jest.fn() -const mockAgent = { - getLikes: jest.fn(), - api: {app: {bsky: {feed: {getQuotes: jest.fn()}}}}, -} +const mockClient = {call: jest.fn()} jest.mock('#/lib/api/community', () => ({ getSpacePostLikes: (...args: unknown[]) => mockGetSpacePostLikes(...args), getSpacePostQuotes: (...args: unknown[]) => mockGetSpacePostQuotes(...args), })) -jest.mock('#/state/session', () => ({useAgent: () => mockAgent})) +jest.mock('#/state/session', () => ({useAppviewClient: () => mockClient})) jest.mock('#/state/queries/profile', () => ({ useUnstableProfileViewCache: () => ({getUnstableProfile: jest.fn()}), })) @@ -71,7 +68,7 @@ describe('permissioned-space post engagement components', () => { ).not.toThrow() await waitFor(() => { - expect(mockGetSpacePostLikes).toHaveBeenCalledWith(mockAgent, { + expect(mockGetSpacePostLikes).toHaveBeenCalledWith(mockClient, { uri: SPACE_POST, limit: 30, cursor: undefined, @@ -85,7 +82,7 @@ describe('permissioned-space post engagement components', () => { ).not.toThrow() await waitFor(() => { - expect(mockGetSpacePostQuotes).toHaveBeenCalledWith(mockAgent, { + expect(mockGetSpacePostQuotes).toHaveBeenCalledWith(mockClient, { uri: SPACE_POST, limit: 30, cursor: undefined, -- knotmirror 0.0.1