diff --git a/app/soapbox/queries/__tests__/suggestions.test.ts b/app/soapbox/queries/__tests__/suggestions.test.ts index 3a440d984..15977dbb9 100644 --- a/app/soapbox/queries/__tests__/suggestions.test.ts +++ b/app/soapbox/queries/__tests__/suggestions.test.ts @@ -1,25 +1,24 @@ -import { renderHook } from '@testing-library/react-hooks'; - -import { mock, queryWrapper, waitFor } from 'soapbox/jest/test-helpers'; +import { __stub } from 'soapbox/api'; +import { renderHook, waitFor } from 'soapbox/jest/test-helpers'; import useOnboardingSuggestions from '../suggestions'; describe('useCarouselAvatars', () => { describe('with a successul query', () => { beforeEach(() => { - mock.onGet('/api/v2/suggestions') - .reply(200, [ - { source: 'staff', account: { id: '1', acct: 'a', account_avatar: 'https://example.com/some.jpg' } }, - { source: 'staff', account: { id: '2', acct: 'b', account_avatar: 'https://example.com/some.jpg' } }, - ], { - link: '; rel=\'prev\'', - }); + __stub((mock) => { + mock.onGet('/api/v2/suggestions') + .reply(200, [ + { source: 'staff', account: { id: '1', acct: 'a', account_avatar: 'https://example.com/some.jpg' } }, + { source: 'staff', account: { id: '2', acct: 'b', account_avatar: 'https://example.com/some.jpg' } }, + ], { + link: '; rel=\'prev\'', + }); + }); }); it('is successful', async() => { - const { result } = renderHook(() => useOnboardingSuggestions(), { - wrapper: queryWrapper, - }); + const { result } = renderHook(() => useOnboardingSuggestions()); await waitFor(() => expect(result.current.isFetching).toBe(false)); @@ -29,13 +28,13 @@ describe('useCarouselAvatars', () => { describe('with an unsuccessul query', () => { beforeEach(() => { - mock.onGet('/api/v2/suggestions').networkError(); + __stub((mock) => { + mock.onGet('/api/v2/suggestions').networkError(); + }); }); it('is successful', async() => { - const { result } = renderHook(() => useOnboardingSuggestions(), { - wrapper: queryWrapper, - }); + const { result } = renderHook(() => useOnboardingSuggestions()); await waitFor(() => expect(result.current.isFetching).toBe(false)); diff --git a/app/soapbox/queries/suggestions.ts b/app/soapbox/queries/suggestions.ts index 12acfb895..d5ddf07bf 100644 --- a/app/soapbox/queries/suggestions.ts +++ b/app/soapbox/queries/suggestions.ts @@ -3,8 +3,7 @@ import { useInfiniteQuery } from '@tanstack/react-query'; import { fetchRelationships } from 'soapbox/actions/accounts'; import { importFetchedAccounts } from 'soapbox/actions/importer'; import { getLinks } from 'soapbox/api'; -import { useAppDispatch } from 'soapbox/hooks'; -import API from 'soapbox/queries/client'; +import { useApi, useAppDispatch } from 'soapbox/hooks'; type Account = { acct: string @@ -36,10 +35,14 @@ type Suggestion = { account: Account } -const getV2Suggestions = async(dispatch: any, pageParam: any): Promise<{ data: Suggestion[], link: string | null, hasMore: boolean }> => { - return dispatch(async() => { + +export default function useOnboardingSuggestions() { + const api = useApi(); + const dispatch = useAppDispatch(); + + const getV2Suggestions = async(pageParam: any): Promise<{ data: Suggestion[], link: string | undefined, hasMore: boolean }> => { const link = pageParam?.link || '/api/v2/suggestions'; - const response = await API.get(link); + const response = await api.get(link); const hasMore = !!response.headers.link; const nextLink = getLinks(response).refs.find(link => link.rel === 'next')?.uri; @@ -53,13 +56,9 @@ const getV2Suggestions = async(dispatch: any, pageParam: any): Promise<{ data: S link: nextLink, hasMore, }; - }); -}; + }; -export default function useOnboardingSuggestions() { - const dispatch = useAppDispatch(); - - const result = useInfiniteQuery(['suggestions', 'v2'], ({ pageParam }) => getV2Suggestions(dispatch, pageParam), { + const result = useInfiniteQuery(['suggestions', 'v2'], ({ pageParam }) => getV2Suggestions(pageParam), { keepPreviousData: true, getNextPageParam: (config) => { if (config.hasMore) {