Skip to content

Commit

Permalink
fix(replays): fix 'clearQueryCache' method in 'useReplayData' hook
Browse files Browse the repository at this point in the history
Fix a bug currently causing the 'clearQueryCache' method defined in
'static/app/utils/replays/hooks/useReplayData.tsx' to have no effect
(extra arrow function declaration).
Add a unit test to cover this behavior of the custom hook.
  • Loading branch information
floels committed May 8, 2024
1 parent 6b530ff commit 9944ba4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 18 deletions.
66 changes: 63 additions & 3 deletions static/app/utils/replays/hooks/useReplayData.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ jest.mocked(useProjects).mockReturnValue({
placeholders: [],
});

const mockInvalidateQueries = jest.fn();

function wrapper({children}: {children?: ReactNode}) {
return (
<QueryClientProvider client={makeTestQueryClient()}>{children}</QueryClientProvider>
);
const queryClient = makeTestQueryClient();

queryClient.invalidateQueries = mockInvalidateQueries;

return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
}

function getMockReplayRecord(replayRecord?: Partial<ReplayRecord>) {
Expand All @@ -59,6 +63,7 @@ function getMockReplayRecord(replayRecord?: Partial<ReplayRecord>) {
describe('useReplayData', () => {
beforeEach(() => {
MockApiClient.clearMockResponses();
mockInvalidateQueries.mockClear();
});

it('should hydrate the replayRecord', async () => {
Expand Down Expand Up @@ -505,4 +510,59 @@ describe('useReplayData', () => {
)
);
});

it("should invalidate queries when result's 'onRetry' function is called", async () => {
const {mockReplayResponse} = getMockReplayRecord({
count_errors: 0,
count_segments: 0,
error_ids: [],
});

const replayId = mockReplayResponse.id;

MockApiClient.addMockResponse({
url: `/organizations/${organization.slug}/replays/${replayId}/`,
body: {data: mockReplayResponse},
});

MockApiClient.addMockResponse({
url: `/organizations/${organization.slug}/replays-events-meta/`,
body: {
data: [],
},
headers: {
Link: [
'<http://localhost/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:1:0"',
'<http://localhost/?cursor=0:2:0>; rel="next"; results="false"; cursor="0:1:0"',
].join(','),
},
});

const {result} = renderHook(useReplayData, {
wrapper,
initialProps: {
replayId,
orgSlug: organization.slug,
},
});

// We need this 'await waitFor()' for the following assertions to pass:
await waitFor(() => {
expect(result.current).toBeTruthy();
});

result.current.onRetry();

expect(mockInvalidateQueries).toHaveBeenCalledWith({
queryKey: [`/organizations/${organization.slug}/replays/${replayId}/`],
});
expect(mockInvalidateQueries).toHaveBeenCalledWith({
queryKey: [
`/projects/${organization.slug}/${project.slug}/replays/${replayId}/recording-segments/`,
],
});
expect(mockInvalidateQueries).toHaveBeenCalledWith({
queryKey: [`/organizations/${organization.slug}/replays-events-meta/`],
});
});
});
28 changes: 13 additions & 15 deletions static/app/utils/replays/hooks/useReplayData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,19 @@ function useReplayData({
});

const clearQueryCache = useCallback(() => {
() => {
queryClient.invalidateQueries({
queryKey: [`/organizations/${orgSlug}/replays/${replayId}/`],
});
queryClient.invalidateQueries({
queryKey: [
`/projects/${orgSlug}/${projectSlug}/replays/${replayId}/recording-segments/`,
],
});
// The next one isn't optimized
// This statement will invalidate the cache of fetched error events for all replayIds
queryClient.invalidateQueries({
queryKey: [`/organizations/${orgSlug}/replays-events-meta/`],
});
};
queryClient.invalidateQueries({
queryKey: [`/organizations/${orgSlug}/replays/${replayId}/`],
});
queryClient.invalidateQueries({
queryKey: [
`/projects/${orgSlug}/${projectSlug}/replays/${replayId}/recording-segments/`,
],
});
// The next one isn't optimized
// This statement will invalidate the cache of fetched error events for all replayIds
queryClient.invalidateQueries({
queryKey: [`/organizations/${orgSlug}/replays-events-meta/`],
});
}, [orgSlug, replayId, projectSlug, queryClient]);

return useMemo(() => {
Expand Down

0 comments on commit 9944ba4

Please sign in to comment.