File size: 7,637 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
 * @jest-environment jsdom
 */

import url from 'url';
import { loadScript } from '@automattic/load-script';
import cookie from 'cookie';
import { recordAliasInFloodlight } from 'calypso/lib/analytics/ad-tracking';
import { identifyUser } from 'calypso/lib/analytics/identify-user';
import { initializeAnalytics } from 'calypso/lib/analytics/init';
import { bumpStat, bumpStatWithPageView } from 'calypso/lib/analytics/mc';
import { recordTracksEvent, tracksEvents } from 'calypso/lib/analytics/tracks';

jest.mock( '@automattic/calypso-config', () => require( './mocks/config' ) );
jest.mock( 'calypso/lib/analytics/ad-tracking', () => ( {
	retarget: () => {},
	recordAliasInFloodlight: jest.fn(),
} ) );

jest.mock( '@automattic/load-script', () => ( {
	loadScript: jest.fn( () => Promise.reject() ),
} ) );

jest.mock( 'cookie', () => ( {
	parse: jest.fn(),
	serialize: jest.fn(),
} ) );

jest.mock( 'calypso/lib/jetpack/is-jetpack-cloud', () => () => false );

function logImageLoads() {
	const imagesLoaded = [];
	let originalImage;

	beforeAll( function spyOnImage() {
		imagesLoaded.length = 0;
		originalImage = global.Image;

		global.Image = function () {
			this._src = '';
		};

		Object.defineProperty( global.Image.prototype, 'src', {
			get: function () {
				return this._src;
			},
			set: function ( value ) {
				this._src = value;
				imagesLoaded.push( url.parse( value, true, true ) );
			},
		} );
	} );

	afterAll( function tearDownSpy() {
		global.Image = originalImage;
	} );

	return imagesLoaded;
}

describe( 'Analytics', () => {
	const imagesLoaded = logImageLoads();

	beforeEach( () => {
		// this seems really weird. but we need to keep the same array reference, but trim the array
		imagesLoaded.length = 0;
	} );

	describe( 'mc', () => {
		test( 'bumpStat with group and stat', () => {
			bumpStat( 'go', 'time' );
			expect( imagesLoaded[ 0 ].query.v ).toEqual( 'wpcom-no-pv' );
			expect( imagesLoaded[ 0 ].query.x_go ).toEqual( 'time' );
			expect( imagesLoaded[ 0 ].query.t ).toBeTruthy();
		} );

		test( 'bumpStat with value object', () => {
			bumpStat( {
				go: 'time',
				another: 'one',
			} );
			expect( imagesLoaded[ 0 ].query.v ).toEqual( 'wpcom-no-pv' );
			expect( imagesLoaded[ 0 ].query.x_go ).toEqual( 'time' );
			expect( imagesLoaded[ 0 ].query.x_another ).toEqual( 'one' );
			expect( imagesLoaded[ 0 ].query.t ).toBeTruthy();
		} );

		test( 'bumpStatWithPageView with group and stat', () => {
			bumpStatWithPageView( 'go', 'time' );
			expect( imagesLoaded[ 0 ].query.v ).toEqual( 'wpcom' );
			expect( imagesLoaded[ 0 ].query.go ).toEqual( 'time' );
			expect( imagesLoaded[ 0 ].query.t ).toBeTruthy();
		} );

		test( 'bumpStatWithPageView with value object', () => {
			bumpStatWithPageView( {
				go: 'time',
				another: 'one',
			} );
			expect( imagesLoaded[ 0 ].query.v ).toEqual( 'wpcom' );
			expect( imagesLoaded[ 0 ].query.go ).toEqual( 'time' );
			expect( imagesLoaded[ 0 ].query.another ).toEqual( 'one' );
			expect( imagesLoaded[ 0 ].query.t ).toBeTruthy();
		} );
	} );

	describe( 'identifyUser', () => {
		beforeEach( () => {
			window._tkq = {
				push: jest.fn(),
			};
			cookie.parse.mockImplementation( () => ( { tk_ai: true } ) );
		} );

		afterEach( () => {
			recordAliasInFloodlight.mockReset();
		} );

		test( 'should not call window._tkq.push or recordAliasInFloodlight when there is no user data', () => {
			identifyUser( {} );
			expect( window._tkq.push ).not.toHaveBeenCalled();
			expect( recordAliasInFloodlight ).not.toHaveBeenCalled();
		} );

		test( 'should not call window._tkq.push and recordAliasInFloodlight when user ID is missing', () => {
			identifyUser( { ID: undefined, username: 'eight', email: 'eight@example.com' } );
			expect( window._tkq.push ).not.toHaveBeenCalled();
			expect( recordAliasInFloodlight ).not.toHaveBeenCalled();
		} );

		test( 'should not call window._tkq.push and recordAliasInFloodlight when username is missing', () => {
			identifyUser( { ID: 8, username: undefined, email: 'eight@example.com' } );
			expect( window._tkq.push ).not.toHaveBeenCalled();
			expect( recordAliasInFloodlight ).not.toHaveBeenCalled();
		} );

		test( 'should not call window._tkq.push and recordAliasInFloodlight when email is missing', () => {
			identifyUser( { ID: 8, username: 'eight', email: undefined } );
			expect( window._tkq.push ).not.toHaveBeenCalled();
			expect( recordAliasInFloodlight ).not.toHaveBeenCalled();
		} );

		test( 'should call window._tkq.push and recordAliasInFloodlight when user ID, username, and email are given', () => {
			identifyUser( { ID: '8', username: 'eight', email: 'eight@example.com' } );
			expect( recordAliasInFloodlight ).toHaveBeenCalled();
			expect( window._tkq.push ).toHaveBeenCalledWith( [ 'identifyUser', 8, 'eight' ] );
		} );

		test( 'should not call recordAliasInFloodlight when anonymousUserId does not exist', () => {
			cookie.parse.mockImplementationOnce( () => ( {} ) );
			identifyUser( { ID: 8, username: 'eight', email: 'eight@example.com' } );
			expect( recordAliasInFloodlight ).not.toHaveBeenCalled();
			expect( window._tkq.push ).toHaveBeenCalledWith( [ 'identifyUser', 8, 'eight' ] );
		} );
	} );
	describe( 'initialize', () => {
		beforeEach( () => {
			loadScript.mockReset();

			cookie.parse.mockImplementation( () => ( {} ) );
			cookie.serialize.mockImplementation( () => {} );
		} );
		test( 'if stat.js load fails, should load nostat.js', () => {
			return initializeAnalytics().then( () => {
				expect( loadScript ).toHaveBeenCalledWith( expect.stringMatching( /\/nostats.js/ ) );
			} );
		} );
	} );
	describe( 'tracks', () => {
		describe( 'recordEvent', () => {
			beforeEach( () => {
				window._tkq = {
					push: jest.fn(),
				};
				cookie.parse.mockImplementation( () => ( { tk_ai: true } ) );
				global.console.error = jest.fn();
			} );

			test( 'should log error if event name does not match regex', () => {
				recordTracksEvent( 'calypso_!!!' );
				expect( global.console.error ).toHaveBeenCalledWith( expect.any( String ), 'calypso_!!!' );
			} );

			test( 'should log error if nested property', () => {
				recordTracksEvent( 'calypso_abc_def', {
					nested: {},
				} );
				expect( global.console.error ).not.toHaveBeenCalledWith(
					expect.any( String ),
					'calypso_abc_def'
				);
				expect( global.console.error ).toHaveBeenCalledWith( expect.any( String ), { nested: {} } );
			} );

			test( 'should log error if property names do not match regex', () => {
				recordTracksEvent( 'calypso_abc_def', {
					'incorrect!': 'property',
				} );
				expect( global.console.error ).toHaveBeenCalledWith(
					expect.any( String ),
					expect.any( String ),
					'incorrect!'
				);
			} );

			test( 'should log error if using a special reserved property name', () => {
				recordTracksEvent( 'calypso_abc_def', {
					geo: 'property',
				} );
				expect( global.console.error ).toHaveBeenCalledWith(
					expect.any( String ),
					'geo',
					expect.any( String )
				);
			} );

			test( 'should add _superProps if they are initialized', () => {
				initializeAnalytics( {}, () => {
					return { super: 'prop' };
				} );
				recordTracksEvent( 'calypso_abc_def' );

				expect( window._tkq.push ).toHaveBeenCalledWith( [
					'recordEvent',
					'calypso_abc_def',
					{ super: 'prop' },
				] );
			} );

			test( 'should notify listeners when event is recorded', () => {
				let numCalled = 0;
				tracksEvents.addListener( 'record-event', () => {
					numCalled++;
				} );
				recordTracksEvent( 'calypso_abc_def' );
				expect( numCalled ).toEqual( 1 );
			} );
		} );
	} );
} );