-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract usePerformanceReport into own hook and guard agains invalid u…
…rls being saved.
- Loading branch information
Showing
3 changed files
with
207 additions
and
102 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
client/hosting/performance/hooks/usePerformanceReport.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { useState, useCallback, useEffect } from 'react'; | ||
import { useUrlBasicMetricsQuery } from 'calypso/data/site-profiler/use-url-basic-metrics-query'; | ||
import { useUrlPerformanceInsightsQuery } from 'calypso/data/site-profiler/use-url-performance-insights'; | ||
import { TabType } from 'calypso/performance-profiler/components/header'; | ||
|
||
function isValidURL( url: string ) { | ||
return /^(https?:\/\/)?([a-z0-9-]+\.)+[a-z]{2,}(:[0-9]{1,5})?(\/[^\s]*)?$/i.test( url ); | ||
} | ||
|
||
export const usePerformanceReport = ( | ||
setIsSavingPerformanceReportUrl: ( isSaving: boolean ) => void, | ||
refetchPages: () => void, | ||
savePerformanceReportUrl: ( | ||
pageId: string, | ||
wpcom_performance_report_url: { url: string; hash: string } | ||
) => Promise< void >, | ||
currentPageId: string, | ||
wpcom_performance_report_url: { url: string; hash: string } | undefined, | ||
activeTab: TabType | ||
) => { | ||
const { url = '', hash = '' } = wpcom_performance_report_url || {}; | ||
|
||
const [ retestState, setRetestState ] = useState( 'idle' ); | ||
|
||
const { | ||
data: basicMetrics, | ||
isError: isBasicMetricsError, | ||
isFetched: isBasicMetricsFetched, | ||
isLoading: isLoadingBasicMetrics, | ||
refetch: requeueAdvancedMetrics, | ||
} = useUrlBasicMetricsQuery( url, hash, true ); | ||
|
||
const { final_url: finalUrl, token } = basicMetrics || {}; | ||
|
||
useEffect( () => { | ||
if ( token && finalUrl && isValidURL( finalUrl ) ) { | ||
setIsSavingPerformanceReportUrl( true ); | ||
savePerformanceReportUrl( currentPageId, { url: finalUrl, hash: token } ) | ||
.then( () => { | ||
refetchPages(); | ||
} ) | ||
.finally( () => { | ||
setIsSavingPerformanceReportUrl( false ); | ||
} ); | ||
} | ||
// We only want to run this effect when the token changes. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [ token ] ); | ||
|
||
const { | ||
data, | ||
status: insightsStatus, | ||
isError: isInsightsError, | ||
isLoading: isLoadingInsights, | ||
} = useUrlPerformanceInsightsQuery( url, token ?? hash ); | ||
|
||
const performanceInsights = data?.pagespeed; | ||
|
||
const mobileReport = | ||
typeof performanceInsights?.mobile === 'string' ? undefined : performanceInsights?.mobile; | ||
const desktopReport = | ||
typeof performanceInsights?.desktop === 'string' ? undefined : performanceInsights?.desktop; | ||
|
||
const performanceReport = activeTab === 'mobile' ? mobileReport : desktopReport; | ||
|
||
const desktopLoaded = typeof performanceInsights?.desktop === 'object'; | ||
const mobileLoaded = typeof performanceInsights?.mobile === 'object'; | ||
|
||
const getHashOrToken = ( | ||
hash: string | undefined, | ||
token: string | undefined, | ||
isReportLoaded: boolean | ||
) => { | ||
if ( hash ) { | ||
return hash; | ||
} else if ( token && isReportLoaded ) { | ||
return token; | ||
} | ||
return ''; | ||
}; | ||
|
||
const testAgain = useCallback( async () => { | ||
setRetestState( 'queueing-advanced' ); | ||
const result = await requeueAdvancedMetrics(); | ||
setRetestState( 'polling-for-insights' ); | ||
return result; | ||
}, [ requeueAdvancedMetrics ] ); | ||
|
||
if ( | ||
retestState === 'polling-for-insights' && | ||
insightsStatus === 'success' && | ||
( activeTab === 'mobile' ? mobileLoaded : desktopLoaded ) | ||
) { | ||
setRetestState( 'idle' ); | ||
} | ||
|
||
return { | ||
performanceReport, | ||
url: finalUrl ?? url, | ||
hash: getHashOrToken( hash, token, activeTab === 'mobile' ? mobileLoaded : desktopLoaded ), | ||
isLoading: | ||
isLoadingBasicMetrics || | ||
isLoadingInsights || | ||
( activeTab === 'mobile' ? ! mobileLoaded : ! desktopLoaded ), | ||
isError: isBasicMetricsError || isInsightsError, | ||
isBasicMetricsFetched, | ||
testAgain, | ||
isRetesting: retestState !== 'idle', | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
client/hosting/performance/test/use-performance-report.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react'; | ||
import { useUrlBasicMetricsQuery } from 'calypso/data/site-profiler/use-url-basic-metrics-query'; | ||
import { useUrlPerformanceInsightsQuery } from 'calypso/data/site-profiler/use-url-performance-insights'; | ||
import { usePerformanceReport } from '../hooks/usePerformanceReport'; | ||
|
||
jest.mock( 'calypso/data/site-profiler/use-url-basic-metrics-query' ); | ||
jest.mock( 'calypso/data/site-profiler/use-url-basic-metrics-query' ); | ||
jest.mock( 'calypso/data/site-profiler/use-url-performance-insights' ); | ||
|
||
describe( 'usePerformanceReport', () => { | ||
const mockSetIsSaving = jest.fn(); | ||
const mockRefetchPages = jest.fn(); | ||
const mockSavePerformanceReportUrl = jest.fn().mockResolvedValue( undefined ); | ||
|
||
beforeEach( () => { | ||
jest.clearAllMocks(); | ||
|
||
( useUrlBasicMetricsQuery as jest.Mock ).mockReturnValue( { | ||
data: null, | ||
isError: false, | ||
isFetched: false, | ||
isLoading: false, | ||
refetch: jest.fn(), | ||
} ); | ||
|
||
( useUrlPerformanceInsightsQuery as jest.Mock ).mockReturnValue( { | ||
data: null, | ||
status: 'idle', | ||
isError: false, | ||
isLoading: false, | ||
} ); | ||
} ); | ||
|
||
it( 'should not call savePerformanceReportUrl when finalUrl is not a valid URL but token exists', () => { | ||
( useUrlBasicMetricsQuery as jest.Mock ).mockReturnValue( { | ||
data: { | ||
final_url: 'false', | ||
token: 'some-token', | ||
}, | ||
isError: false, | ||
isFetched: true, | ||
isLoading: false, | ||
refetch: jest.fn(), | ||
} ); | ||
|
||
renderHook( () => | ||
usePerformanceReport( | ||
mockSetIsSaving, | ||
mockRefetchPages, | ||
mockSavePerformanceReportUrl, | ||
'123', | ||
{ url: 'test.com', hash: 'test-hash' }, | ||
'mobile' | ||
) | ||
); | ||
|
||
expect( mockSavePerformanceReportUrl ).not.toHaveBeenCalled(); | ||
expect( mockSetIsSaving ).not.toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'should call savePerformanceReportUrl when finalUrl is a valid URL and token exists', () => { | ||
// Mock the basic metrics query to return valid finalUrl and token | ||
( useUrlBasicMetricsQuery as jest.Mock ).mockReturnValue( { | ||
data: { | ||
final_url: 'https://final-url.com', | ||
token: 'valid-token', | ||
}, | ||
isError: false, | ||
isFetched: true, | ||
isLoading: false, | ||
refetch: jest.fn(), | ||
} ); | ||
|
||
renderHook( () => | ||
usePerformanceReport( | ||
mockSetIsSaving, | ||
mockRefetchPages, | ||
mockSavePerformanceReportUrl, | ||
'123', | ||
{ url: 'test.com', hash: 'test-hash' }, | ||
'mobile' | ||
) | ||
); | ||
|
||
expect( mockSavePerformanceReportUrl ).toHaveBeenCalledWith( '123', { | ||
url: 'https://final-url.com', | ||
hash: 'valid-token', | ||
} ); | ||
expect( mockSetIsSaving ).toHaveBeenCalledWith( true ); | ||
} ); | ||
} ); |