forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalytics-test.js
75 lines (61 loc) · 2.32 KB
/
Analytics-test.js
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
jest.dontMock('../Analytics');
jest.dontMock('../CoreManager');
const Analytics = require('../Analytics');
const CoreManager = require('../CoreManager');
const defaultController = CoreManager.getAnalyticsController();
describe('Analytics', () => {
beforeEach(() => {
const track = jest.fn();
track.mockReturnValue(Promise.resolve());
CoreManager.setAnalyticsController({ track: track });
});
it('throws when no event name is provided', () => {
expect(Analytics.track).toThrow('A name for the custom event must be provided');
expect(Analytics.track.bind(null, '')).toThrow('A name for the custom event must be provided');
});
it('trims whitespace from event names', () => {
Analytics.track(' before', {});
expect(CoreManager.getAnalyticsController().track.mock.calls[0]).toEqual(['before', {}]);
Analytics.track('after ', {});
expect(CoreManager.getAnalyticsController().track.mock.calls[1]).toEqual(['after', {}]);
Analytics.track(' both ', {});
expect(CoreManager.getAnalyticsController().track.mock.calls[2]).toEqual(['both', {}]);
});
it('passes along event names and dimensions', () => {
Analytics.track('myEvent', { value: 'a' });
expect(CoreManager.getAnalyticsController().track.mock.calls[0]).toEqual([
'myEvent',
{ value: 'a' },
]);
});
it('throws when invalid dimensions are provided', () => {
expect(Analytics.track.bind(null, 'event', { number: 12 })).toThrow(
'track() dimensions expects keys and values of type "string".'
);
expect(Analytics.track.bind(null, 'event', { null: null })).toThrow(
'track() dimensions expects keys and values of type "string".'
);
});
});
describe('AnalyticsController', () => {
beforeEach(() => {
CoreManager.setAnalyticsController(defaultController);
const request = jest.fn();
request.mockReturnValue(
Promise.resolve({
success: true,
result: {},
})
);
const ajax = jest.fn();
CoreManager.setRESTController({ request: request, ajax: ajax });
});
it('passes dimensions along to the appropriate endpoint', () => {
Analytics.track('click', { x: '12', y: '40' });
expect(CoreManager.getRESTController().request.mock.calls[0]).toEqual([
'POST',
'events/click',
{ dimensions: { x: '12', y: '40' } },
]);
});
});