File size: 1,847 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
/**
 * @jest-environment jsdom
 */
import { isRegionInCcpaZone } from '@automattic/calypso-analytics';
import { mayWeTrackUserGpcInCcpaRegion } from '../utils';

// Return a predictable value for whether the user is in a CCPA region.
jest.mock( '@automattic/calypso-analytics' );

let windowSpy = jest.SpyInstance;

// Return a predictable value for window.navigator.globalPrivacyControl.
const setMockGpcValue = ( gpcValue ) => {
	windowSpy.mockImplementation( () => ( {
		navigator: {
			globalPrivacyControl: gpcValue,
		},
	} ) );
};

describe( 'global privacy control', () => {
	beforeEach( () => {
		windowSpy = jest.spyOn( global, 'window', 'get' );
	} );

	afterEach( () => {
		windowSpy.mockRestore();
	} );

	describe( 'mayWeTrackUserGpcInCcpaRegion', () => {
		test( 'we may track a user in CCPA if GPC is not set', () => {
			setMockGpcValue( undefined );
			isRegionInCcpaZone.mockReturnValue( true );

			expect( mayWeTrackUserGpcInCcpaRegion() ).toBe( true );
		} );

		test( 'we may track a user in CCPA if GPC is set to false', () => {
			setMockGpcValue( false );
			isRegionInCcpaZone.mockReturnValue( true );

			expect( mayWeTrackUserGpcInCcpaRegion() ).toBe( true );
		} );

		test( 'we may not track a user in CCPA if GPC is set', () => {
			setMockGpcValue( true );
			isRegionInCcpaZone.mockReturnValue( true );

			expect( mayWeTrackUserGpcInCcpaRegion() ).toBe( false );
		} );

		test( 'we may track a user outside CCPA if GPC is set', () => {
			setMockGpcValue( true );
			isRegionInCcpaZone.mockReturnValue( false );

			expect( mayWeTrackUserGpcInCcpaRegion() ).toBe( true );
		} );

		test( 'we may track a user outside CCPA if GPC is undefined', () => {
			setMockGpcValue( undefined );
			isRegionInCcpaZone.mockReturnValue( false );

			expect( mayWeTrackUserGpcInCcpaRegion() ).toBe( true );
		} );
	} );
} );