File size: 1,461 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
/**
 * @jest-environment jsdom
 */

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react';
import { when } from 'jest-when';
import { ReactNode } from 'react';
import { useGeoLocationQuery, GeoLocationData } from '../use-geolocation-query';

describe( 'useGeoLocationQuery', () => {
	const wrapper = ( { children }: { children: ReactNode } ) => (
		<QueryClientProvider client={ new QueryClient() }>{ children }</QueryClientProvider>
	);

	const mockGeoData: GeoLocationData = {
		city: 'San Francisco',
		country_long: 'United States',
		country_short: 'US',
		latitude: '37.7749',
		longitude: '-122.4194',
		region: 'California',
	};

	beforeAll( () => {
		when( jest.spyOn( globalThis, 'fetch' ) )
			.calledWith( 'https://public-api.wordpress.com/geo/' )
			.mockResolvedValue( { json: () => Promise.resolve( mockGeoData ) } as Response );
	} );

	it( 'should fetch geolocation data', async () => {
		const { result } = renderHook( () => useGeoLocationQuery(), { wrapper } );

		// Initially loading, no data yet.
		expect( result.current.data ).toBe( undefined );

		// Verify data.
		await waitFor( () => expect( result.current.data ).toEqual( mockGeoData ) );
	} );

	it( 'keeps the data fresh', async () => {
		const { result } = renderHook( () => useGeoLocationQuery(), { wrapper } );

		await waitFor( () => expect( result.current.isStale ).toBe( false ) );
	} );
} );