File size: 3,849 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
import {
	trackCustomFacebookConversionEvent,
	trackCustomAdWordsRemarketingEvent,
} from 'calypso/lib/analytics/ad-tracking';
import { gaRecordPageView, gaRecordEvent } from 'calypso/lib/analytics/ga';
import { addHotJarScript } from 'calypso/lib/analytics/hotjar';
import { bumpStat as mcBumpStat } from 'calypso/lib/analytics/mc';
import { recordPageView as pageviewRecordPageView } from 'calypso/lib/analytics/page-view';
import {
	setTracksOptOut as tracksSetTracksOptOut,
	recordTracksEvent as tracksRecordTracksEvent,
} from 'calypso/lib/analytics/tracks';
import {
	withAnalytics,
	bumpStat,
	recordCustomAdWordsRemarketingEvent,
	recordCustomFacebookConversionEvent,
	recordGoogleEvent,
	recordGooglePageView,
	recordTracksEvent,
	recordPageView,
	setTracksOptOut,
	loadTrackingTool,
} from '../actions';
import { analyticsMiddleware } from '../middleware.js';

const noop = () => {};

jest.mock( 'calypso/lib/analytics/page-view', () => ( {
	recordPageView: jest.fn(),
} ) );

jest.mock( 'calypso/lib/analytics/tracks', () => ( {
	setTracksOptOut: jest.fn(),
	recordTracksEvent: jest.fn(),
} ) );

jest.mock( 'calypso/lib/analytics/ad-tracking', () => ( {
	trackCustomFacebookConversionEvent: jest.fn(),
	trackCustomAdWordsRemarketingEvent: jest.fn(),
} ) );

jest.mock( 'calypso/lib/analytics/mc', () => ( {
	bumpStat: jest.fn(),
} ) );

jest.mock( 'calypso/lib/analytics/ga', () => ( {
	gaRecordPageView: jest.fn(),
	gaRecordEvent: jest.fn(),
} ) );

jest.mock( 'calypso/lib/analytics/hotjar', () => ( {
	addHotJarScript: jest.fn(),
} ) );

const dispatch = analyticsMiddleware()( noop );

describe( 'middleware', () => {
	describe( 'analytics dispatching', () => {
		beforeEach( () => {
			jest.resetAllMocks();
		} );

		test( 'should call mc.bumpStat', () => {
			dispatch( bumpStat( 'test', 'value' ) );

			expect( mcBumpStat ).toHaveBeenCalledWith( 'test', 'value' );
		} );

		test( 'should call tracks.recordEvent', () => {
			dispatch( recordTracksEvent( 'test', { name: 'value' } ) );

			expect( tracksRecordTracksEvent ).toHaveBeenCalledWith( 'test', {
				name: 'value',
			} );
		} );

		test( 'should call pageView.record', () => {
			dispatch( recordPageView( 'path', 'title', 'default', { name: 'value' } ) );

			expect( pageviewRecordPageView ).toHaveBeenCalledWith(
				'path',
				'title',
				{
					name: 'value',
				},
				{}
			);
		} );

		test( 'should call gaRecordEvent', () => {
			dispatch( recordGoogleEvent( 'category', 'action', 'label', 'value' ) );

			expect( gaRecordEvent ).toHaveBeenCalledWith( 'category', 'action', 'label', 'value' );
		} );

		test( 'should call ga.recordPageView', () => {
			dispatch( recordGooglePageView( 'path', 'title' ) );

			expect( gaRecordPageView ).toHaveBeenCalledWith( 'path', 'title' );
		} );

		test( 'should call trackCustomFacebookConversionEvent', () => {
			dispatch( recordCustomFacebookConversionEvent( 'event', { name: 'value' } ) );

			expect( trackCustomFacebookConversionEvent ).toHaveBeenCalledWith( 'event', {
				name: 'value',
			} );
		} );

		test( 'should call trackCustomAdWordsRemarketingEvent', () => {
			dispatch( recordCustomAdWordsRemarketingEvent( { name: 'value' } ) );

			expect( trackCustomAdWordsRemarketingEvent ).toHaveBeenCalledWith( { name: 'value' } );
		} );

		test( 'should call analytics events with wrapped actions', () => {
			dispatch( withAnalytics( bumpStat( 'name', 'value' ), { type: 'TEST_ACTION' } ) );

			expect( mcBumpStat ).toHaveBeenCalledWith( 'name', 'value' );
		} );

		test( 'should call `setOptOut`', () => {
			dispatch( setTracksOptOut( false ) );

			expect( tracksSetTracksOptOut ).toHaveBeenCalledWith( false );
		} );

		test( 'should call hotjar.addHotJarScript', () => {
			dispatch( loadTrackingTool( 'HotJar' ) );
			expect( addHotJarScript ).toHaveBeenCalledTimes( 1 );
		} );
	} );
} );