Skip to content

Commit 423e4a0

Browse files
authored
fix: made timeline refresh when switching accounts - [CU-869b1pyqk] (#86)
1 parent 1ca3e67 commit 423e4a0

3 files changed

Lines changed: 110 additions & 1 deletion

File tree

src/__tests__/hooks/useFollowingFeed.test.tsx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,38 @@ jest.mock('@/services/timeline', () => ({
1111
getFollowingFeed: jest.fn(),
1212
}));
1313

14+
// Mock the userStore
15+
jest.mock('@/stores/userStore', () => ({
16+
useUserStore: jest.fn((selector) => {
17+
const state = {
18+
user: {
19+
username: 'testuser',
20+
displayName: 'Test User',
21+
bio: null,
22+
bioEntities: null,
23+
avatarUrl: null,
24+
bannerUrl: null,
25+
location: null,
26+
websiteUrl: null,
27+
birthDate: null,
28+
joinedAt: null,
29+
relationship: {
30+
blocking: false,
31+
blockedBy: false,
32+
muted: false,
33+
following: false,
34+
follower: false,
35+
},
36+
followingCount: 0,
37+
followersCount: 0,
38+
mutualsCount: 0,
39+
mutualNames: [],
40+
},
41+
};
42+
return selector ? selector(state) : state;
43+
}),
44+
}));
45+
1446
const mockGetFollowingFeed = timelineService.getFollowingFeed as jest.MockedFunction<
1547
typeof timelineService.getFollowingFeed
1648
>;
@@ -329,4 +361,45 @@ describe('useFollowingFeed', () => {
329361
expect(allData).toHaveLength(2);
330362
expect(allData).toEqual(mockTweets);
331363
});
364+
365+
it('should include username in query key for account-specific caching', async () => {
366+
mockGetFollowingFeed.mockResolvedValueOnce({
367+
success: true,
368+
message: 'Success',
369+
data: mockTweets,
370+
pagination: { cursor: null, nextCursor: null, hasNextPage: false },
371+
});
372+
373+
const queryClient = new QueryClient({
374+
defaultOptions: {
375+
queries: {
376+
retry: false,
377+
gcTime: 0,
378+
},
379+
},
380+
});
381+
382+
const Wrapper = ({ children }: { children: ReactNode }) => (
383+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
384+
);
385+
386+
const { result } = renderHook(() => useFollowingFeed(), {
387+
wrapper: Wrapper,
388+
});
389+
390+
await waitFor(() => {
391+
expect(result.current.isSuccess).toBe(true);
392+
});
393+
394+
// Verify the query was created with the correct key including username
395+
const queries = queryClient.getQueryCache().getAll();
396+
expect(queries.length).toBeGreaterThan(0);
397+
398+
const followingQuery = queries.find(
399+
(q) => Array.isArray(q.queryKey) && q.queryKey[0] === 'following-feed'
400+
);
401+
402+
expect(followingQuery).toBeDefined();
403+
expect(followingQuery?.queryKey).toEqual(['following-feed', 'testuser']);
404+
});
332405
});

src/__tests__/screens/following_screen.test.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,38 @@ const mockGetFollowingFeed = timelineService.getFollowingFeed as jest.MockedFunc
1515
typeof timelineService.getFollowingFeed
1616
>;
1717

18+
// Mock the userStore
19+
jest.mock('@/stores/userStore', () => ({
20+
useUserStore: jest.fn((selector) => {
21+
const state = {
22+
user: {
23+
username: 'testuser',
24+
displayName: 'Test User',
25+
bio: null,
26+
bioEntities: null,
27+
avatarUrl: null,
28+
bannerUrl: null,
29+
location: null,
30+
websiteUrl: null,
31+
birthDate: null,
32+
joinedAt: null,
33+
relationship: {
34+
blocking: false,
35+
blockedBy: false,
36+
muted: false,
37+
following: false,
38+
follower: false,
39+
},
40+
followingCount: 0,
41+
followersCount: 0,
42+
mutualsCount: 0,
43+
mutualNames: [],
44+
},
45+
};
46+
return selector ? selector(state) : state;
47+
}),
48+
}));
49+
1850
// Mock FlashList with simple string mock
1951
jest.mock('@shopify/flash-list', () => ({
2052
FlashList: 'FlashList',

src/hooks/useFollowingFeed.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import { useInfiniteQuery } from '@tanstack/react-query';
22

33
import { getFollowingFeed } from '@/services/timeline';
4+
import { useUserStore } from '@/stores/userStore';
45

56
const PAGE_SIZE = 5;
67

78
export const useFollowingFeed = () => {
9+
const username = useUserStore((state) => state.user.username);
10+
811
return useInfiniteQuery({
9-
queryKey: ['following-feed'],
12+
queryKey: ['following-feed', username],
1013
queryFn: async ({ pageParam }) => {
1114
const res = await getFollowingFeed({ cursor: pageParam, limit: PAGE_SIZE });
1215
return res;
1316
},
1417
initialPageParam: null as string | null,
1518
getNextPageParam: (lastPage) => lastPage.pagination.nextCursor,
19+
enabled: !!username, // Only fetch if we have a username
1620
});
1721
};

0 commit comments

Comments
 (0)