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

import { setGeoLocation } from '@automattic/number-formatters';
import { setupCountryCode } from '../geolocation';

jest.mock( '@automattic/number-formatters', () => ( {
	setGeoLocation: jest.fn(),
} ) );

describe( 'setupCountryCode', () => {
	beforeEach( () => {
		jest.resetAllMocks();
		document.cookie = '';
	} );

	test( 'should use country_code from cookie and set geo location correctly', async () => {
		document.cookie = 'country_code=US';

		await setupCountryCode();

		expect( setGeoLocation ).toHaveBeenCalledWith( 'US' );
	} );

	test( 'should not set location if cookie has unknown country', async () => {
		document.cookie = 'country_code=unknown';

		await setupCountryCode();

		expect( setGeoLocation ).not.toHaveBeenCalled();
	} );

	test( 'should not set location if country_code cookie is not set', async () => {
		document.cookie = '';

		await setupCountryCode();

		expect( setGeoLocation ).not.toHaveBeenCalled();
	} );
} );