forked from SnakeByteDevelopment/angulartics2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx-analytics-gtm.spec.ts
88 lines (78 loc) · 2.83 KB
/
ngx-analytics-gtm.spec.ts
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
import { fakeAsync, inject, ComponentFixture, TestBed } from '@angular/core/testing';
import { NgxAnalytics } from 'ngx-analytics';
import { advance, createRoot, RootCmp, TestModule } from '../../test.mocks';
import { NgxAnalyticsGoogleTagManager } from './ngx-analytics-gtm';
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
declare var window: any;
describe('NgxAnalyticsGoogleTagManager', () => {
let dataLayer: any;
let fixture: ComponentFixture<any>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [TestModule],
providers: [NgxAnalyticsGoogleTagManager],
});
window.dataLayer = dataLayer = [];
});
it('should track pages',
fakeAsync(inject([NgxAnalytics, NgxAnalyticsGoogleTagManager],
(ngxAnalytics: NgxAnalytics, ngxAnalyticsGoogleTagManager: NgxAnalyticsGoogleTagManager) => {
fixture = createRoot(RootCmp);
ngxAnalytics.pageTrack.next({ path: '/abc' });
advance(fixture);
expect(dataLayer).toContain({
event: 'Page View',
'content-name': '/abc',
userId: null,
});
},
)),
);
it('should track events',
fakeAsync(inject([NgxAnalytics, NgxAnalyticsGoogleTagManager],
(ngxAnalytics: NgxAnalytics, ngxAnalyticsGoogleTagManager: NgxAnalyticsGoogleTagManager) => {
fixture = createRoot(RootCmp);
ngxAnalytics.eventTrack.next({ action: 'do', properties: { category: 'cat', gtmCustom: { customKey: 'customValue' } } });
advance(fixture);
expect(dataLayer).toContain({
event: 'interaction',
target: 'cat',
action: 'do',
customKey: 'customValue',
label: undefined,
value: undefined,
interactionType: undefined,
userId: null,
});
}
)),
);
it('should track exceptions',
fakeAsync(inject([NgxAnalytics, NgxAnalyticsGoogleTagManager],
(ngxAnalytics: NgxAnalytics, ngxAnalyticsGoogleTagManager: NgxAnalyticsGoogleTagManager) => {
fixture = createRoot(RootCmp);
ngxAnalytics.exceptionTrack.next({ appId: 'app', appName: 'Test App', appVersion: '0.1' });
advance(fixture);
expect(dataLayer).toContain({
event: 'interaction',
target: 'Exception',
action: 'Exception thrown for Test App <[email protected]>',
label: undefined,
value: undefined,
interactionType: undefined,
userId: null,
});
}
)),
);
it('should set username',
fakeAsync(inject([NgxAnalytics, NgxAnalyticsGoogleTagManager],
(ngxAnalytics: NgxAnalytics, ngxAnalyticsGoogleTagManager: NgxAnalyticsGoogleTagManager) => {
fixture = createRoot(RootCmp);
ngxAnalytics.setUsername.next('testuser');
advance(fixture);
expect(ngxAnalytics.settings.gtm.userId).toBe('testuser');
}
)),
);
});