diff --git a/src/components/Post/KnownLikers.tsx b/src/components/Post/KnownLikers.tsx index 5a9c01def..97e550ed6 100644 --- a/src/components/Post/KnownLikers.tsx +++ b/src/components/Post/KnownLikers.tsx @@ -1,9 +1,8 @@ import {type StyleProp, type ViewStyle} from 'react-native' -import {AtUri} from '@atproto/syntax' import {moderateProfile} from '@bsky/sdk/moderation' import {Trans, useLingui} from '@lingui/react/macro' -import {makeProfileLink} from '#/lib/routes/links' +import {makeProfileLink, postPermalink} from '#/lib/routes/links' import {sanitizeDisplayName} from '#/lib/strings/display-names' import {useModerationOpts} from '#/state/preferences/moderation-opts' import {atoms as a, useTheme} from '#/alf' @@ -46,8 +45,7 @@ export function KnownLikers({ return null } - const urip = new AtUri(post.uri) - const likesHref = makeProfileLink(post.author, 'post', urip.rkey, 'liked-by') + const likesHref = postPermalink(post.author, post.uri, 'liked-by') const aviStackProfiles = knownLikersAndModeration .slice(0, 3) .map(({actor}) => actor) diff --git a/src/state/queries/trending/__tests__/useGetTrendsQuery.test.tsx b/src/state/queries/trending/__tests__/useGetTrendsQuery.test.tsx new file mode 100644 index 000000000..709d02345 --- /dev/null +++ b/src/state/queries/trending/__tests__/useGetTrendsQuery.test.tsx @@ -0,0 +1,63 @@ +import {type PropsWithChildren} from 'react' +import {QueryClient, QueryClientProvider} from '@tanstack/react-query' +import {renderHook, waitFor} from '@testing-library/react-native' + +import {useGetTrendsQuery} from '../useGetTrendsQuery' + +jest.mock('#/state/queries/preferences', () => ({ + usePreferencesQuery: () => ({data: {moderationPrefs: {mutedWords: []}}}), +})) +jest.mock('#/state/preferences/languages', () => ({ + getContentLanguages: () => ['en'], +})) +jest.mock('#/lib/api/feed/utils', () => ({ + aggregateUserInterests: () => '', + createBskyTopicsHeader: () => ({}), +})) +jest.mock('#/logger', () => ({logger: {debug: jest.fn()}})) + +const trends = Array.from({length: 12}, (_, i) => ({ + topic: `topic-${i}`, + displayName: `Topic ${i}`, + link: `/topic/${i}`, +})) + +describe('useGetTrendsQuery', () => { + const originalFetch = globalThis.fetch + const mockFetch = jest.fn, [string]>() + + beforeEach(() => { + mockFetch.mockReset().mockImplementation(() => + Promise.resolve( + new Response(JSON.stringify({recIdStr: 'rec', trends}), { + status: 200, + }), + ), + ) + globalThis.fetch = mockFetch as unknown as typeof fetch + }) + + afterAll(() => { + globalThis.fetch = originalFetch + }) + + it('asks for the shared fetch limit, so a small display limit does not starve a larger one', async () => { + const queryClient = new QueryClient({ + defaultOptions: {queries: {retry: false, gcTime: Infinity}}, + }) + const wrapper = ({children}: PropsWithChildren) => ( + {children} + ) + + const small = renderHook(() => useGetTrendsQuery({limit: 3}), {wrapper}) + await waitFor(() => expect(small.result.current.data).toBeDefined()) + const large = renderHook(() => useGetTrendsQuery({limit: 10}), {wrapper}) + await waitFor(() => expect(large.result.current.data).toBeDefined()) + + expect(mockFetch).toHaveBeenCalledTimes(1) + const url = new URL(mockFetch.mock.calls[0][0]) + expect(url.searchParams.get('limit')).toBe('20') + expect(small.result.current.data?.trends).toHaveLength(3) + expect(large.result.current.data?.trends).toHaveLength(10) + }) +}) diff --git a/src/state/queries/trending/useGetTrendsQuery.ts b/src/state/queries/trending/useGetTrendsQuery.ts index 8ad811161..48b08fcb9 100644 --- a/src/state/queries/trending/useGetTrendsQuery.ts +++ b/src/state/queries/trending/useGetTrendsQuery.ts @@ -65,7 +65,7 @@ export function useGetTrendsQuery(props: QueryProps = {}) { queryKey: createGetTrendsQueryKey(fetchLimit), queryFn: async () => { const contentLangs = getContentLanguages().join(',') - const params = new URLSearchParams({limit: String(limit)}) + const params = new URLSearchParams({limit: String(fetchLimit)}) const res = await fetch( `${PUBLIC_API}/xrpc/app.bsky.unspecced.getTrends?${params}`, {