File size: 5,590 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
 * @jest-environment jsdom
 */

import { renderHook, act } 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 );
	} );

	it( 'should not save performance report when testAgain() returns an invalid URL', async () => {
		// Setup mock for basic metrics query with invalid URL
		const mockRefetch = jest.fn().mockResolvedValue( {
			data: {
				final_url: 'false',
				token: 'new-token',
			},
		} );

		( useUrlBasicMetricsQuery as jest.Mock ).mockReturnValue( {
			data: null,
			isError: false,
			isFetched: true,
			isLoading: false,
			refetch: mockRefetch,
		} );

		const { result } = renderHook( () =>
			usePerformanceReport(
				mockSetIsSaving,
				mockRefetchPages,
				mockSavePerformanceReportUrl,
				'123',
				{ url: 'test.com', hash: 'test-hash' },
				'mobile'
			)
		);

		// Call testAgain and wait for it to complete
		await act( async () => {
			const { data } = await result.current.testAgain();
			// This simulates what happens in site-performance.tsx
			if ( data?.token && data.token !== 'test-hash' ) {
				await mockSavePerformanceReportUrl( '123', {
					url: data.final_url,
					hash: data.token,
				} );
			}
		} );

		// Verify that refetch was called
		expect( mockRefetch ).toHaveBeenCalled();

		// Verify that savePerformanceReportUrl was called with the invalid URL
		expect( mockSavePerformanceReportUrl ).toHaveBeenCalledWith( '123', {
			url: 'false',
			hash: 'new-token',
		} );

		// But because the URL is invalid, the save operation should return early
		const saveResult = await mockSavePerformanceReportUrl.mock.results[ 0 ].value;
		expect( saveResult ).toBeUndefined();

		// And setIsSaving should not have been called
		expect( mockSetIsSaving ).not.toHaveBeenCalled();
	} );

	it( 'should set isLoading=false and isError=true when report fails', () => {
		( useUrlPerformanceInsightsQuery as jest.Mock ).mockReturnValue( {
			data: {
				pagespeed: {
					mobile: 'failed',
					desktop: 'failed',
				},
			},
			status: 'success',
			isError: false,
			isLoading: false,
		} );

		const { result } = renderHook( () =>
			usePerformanceReport(
				mockSetIsSaving,
				mockRefetchPages,
				mockSavePerformanceReportUrl,
				'123',
				{ url: 'test.com', hash: 'test-hash' },
				'mobile'
			)
		);

		expect( result.current.isLoading ).toBe( false );
		expect( result.current.isError ).toBe( true );
	} );

	it( 'should handle successful report correctly', () => {
		( useUrlPerformanceInsightsQuery as jest.Mock ).mockReturnValue( {
			data: {
				pagespeed: {
					mobile: {
						performance: 90,
					},
					desktop: {
						performance: 85,
					},
				},
			},
			status: 'success',
			isError: false,
			isLoading: false,
		} );

		const { result } = renderHook( () =>
			usePerformanceReport(
				mockSetIsSaving,
				mockRefetchPages,
				mockSavePerformanceReportUrl,
				'123',
				{ url: 'test.com', hash: 'test-hash' },
				'mobile'
			)
		);

		expect( result.current.isLoading ).toBe( false );
		expect( result.current.isError ).toBe( false );
		expect( result.current.performanceReport?.performance ).toEqual( 90 );
	} );
} );