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 cd1ad07
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 23 deletions.
70 changes: 62 additions & 8 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 Down Expand Up @@ -84,11 +88,6 @@ describe('useReplayData', () => {
},
});

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

const {result} = renderHook(useReplayData, {
wrapper,
initialProps: {
Expand Down Expand Up @@ -505,4 +504,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 cd1ad07

Please sign in to comment.