-
Notifications
You must be signed in to change notification settings - Fork 191
/
appinsights.spec.ts
193 lines (177 loc) · 6.85 KB
/
appinsights.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
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
import { fakeAsync, inject, ComponentFixture, TestBed } from '@angular/core/testing';
import { Title } from '@angular/platform-browser';
import { RouterTestingModule } from '@angular/router/testing';
import { Angulartics2 } from '../../angulartics2-core';
import { advance, createRoot, RootCmp, TestModule } from '../../test.mocks';
import { Angulartics2AppInsights } from './appinsights';
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
declare let window: any;
describe('Angulartics2AppInsights', () => {
let appInsights: Microsoft.ApplicationInsights.IAppInsights;
let fixture: ComponentFixture<any>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [TestModule, RouterTestingModule],
providers: [Title, Angulartics2AppInsights],
});
window.appInsights = appInsights = jasmine.createSpyObj('appInsights', [
'trackPageView',
'trackEvent',
'trackException',
'setAuthenticatedUserContext',
]);
const provider: Angulartics2AppInsights = TestBed.inject(Angulartics2AppInsights);
provider.startTracking();
});
it('should track pages', fakeAsync(
inject(
[Angulartics2, Angulartics2AppInsights, Title],
(
angulartics2: Angulartics2,
angulartics2AppInsights: Angulartics2AppInsights,
title: Title,
) => {
fixture = createRoot(RootCmp);
const metrics = {};
const dimensions = {};
const loadTime = 123;
spyOn(title, 'getTitle').and.returnValue('the title');
angulartics2AppInsights.metrics = metrics;
angulartics2AppInsights.dimensions = dimensions;
angulartics2AppInsights.loadTime = loadTime;
angulartics2.pageTrack.next({ path: '/abc' });
advance(fixture);
expect(appInsights.trackPageView).toHaveBeenCalledWith(
'the title',
'/abc',
metrics,
dimensions,
loadTime,
);
},
),
));
it('should track events', fakeAsync(
inject(
[Angulartics2, Angulartics2AppInsights],
(angulartics2: Angulartics2, angulartics2AppInsights: Angulartics2AppInsights) => {
fixture = createRoot(RootCmp);
const action = 'the action';
const properties = {};
const measurements = {};
angulartics2AppInsights.measurements = measurements;
angulartics2.eventTrack.next({
action,
properties,
});
advance(fixture);
expect(appInsights.trackEvent).toHaveBeenCalledWith(action, properties, measurements);
},
),
));
it('should track exceptions (string)', fakeAsync(
inject(
[Angulartics2, Angulartics2AppInsights],
(angulartics2: Angulartics2, angulartics2AppInsights: Angulartics2AppInsights) => {
fixture = createRoot(RootCmp);
const str = 'test string';
angulartics2.exceptionTrack.next(str);
advance(fixture);
// @ts-expect-error
expect(appInsights.trackException).toHaveBeenCalledWith(str);
},
),
));
it('should track exceptions (event)', fakeAsync(
inject(
[Angulartics2, Angulartics2AppInsights],
(angulartics2: Angulartics2, angulartics2AppInsights: Angulartics2AppInsights) => {
fixture = createRoot(RootCmp);
const event = { event: true };
angulartics2.exceptionTrack.next({ event });
advance(fixture);
// @ts-expect-error
expect(appInsights.trackException).toHaveBeenCalledWith(event);
},
),
));
it('should track exceptions (description)', fakeAsync(
inject(
[Angulartics2, Angulartics2AppInsights],
(angulartics2: Angulartics2, angulartics2AppInsights: Angulartics2AppInsights) => {
fixture = createRoot(RootCmp);
const description = 'test description';
angulartics2.exceptionTrack.next({ description });
advance(fixture);
// @ts-expect-error
expect(appInsights.trackException).toHaveBeenCalledWith(description);
},
),
));
it('should set userId in setAuthenticatedUserContext', fakeAsync(
inject(
[Angulartics2, Angulartics2AppInsights],
(angulartics2: Angulartics2, angulartics2AppInsights: Angulartics2AppInsights) => {
fixture = createRoot(RootCmp);
const userId = 'test_userId';
angulartics2AppInsights.setUsername(userId);
advance(fixture);
expect(angulartics2.settings.appInsights.userId).toBe(userId);
expect(appInsights.setAuthenticatedUserContext).toHaveBeenCalledWith(userId);
},
),
));
it('should set userId and accountId in setAuthenticatedUserContext', fakeAsync(
inject(
[Angulartics2, Angulartics2AppInsights],
(angulartics2: Angulartics2, angulartics2AppInsights: Angulartics2AppInsights) => {
fixture = createRoot(RootCmp);
const userId = 'test_userId';
const accountId = 'test_accountId';
angulartics2AppInsights.setUserProperties({ userId, accountId });
advance(fixture);
expect(angulartics2.settings.appInsights.userId).toBe(userId);
expect(appInsights.setAuthenticatedUserContext).toHaveBeenCalledWith(userId, accountId);
},
),
));
it('should user existing userId and set accountId in setAuthenticatedUserContext', fakeAsync(
inject(
[Angulartics2, Angulartics2AppInsights],
(angulartics2: Angulartics2, angulartics2AppInsights: Angulartics2AppInsights) => {
fixture = createRoot(RootCmp);
const userId = 'test_userId';
const accountId = 'test_accountId';
angulartics2AppInsights.setUsername(userId);
advance(fixture);
expect(angulartics2.settings.appInsights.userId).toBe(userId);
angulartics2AppInsights.setUserProperties({ accountId });
advance(fixture);
expect(appInsights.setAuthenticatedUserContext).toHaveBeenCalledWith(userId, accountId);
},
),
));
it('should set the start time on start', fakeAsync(
inject(
[Angulartics2, Angulartics2AppInsights],
(angulartics2: Angulartics2, angulartics2AppInsights: Angulartics2AppInsights) => {
angulartics2AppInsights.startTimer();
expect(angulartics2AppInsights.loadStartTime).toBeLessThanOrEqual(Date.now());
expect(angulartics2AppInsights.loadTime).toBe(null);
},
),
));
it('should set the total time on stop', fakeAsync(
inject(
[Angulartics2, Angulartics2AppInsights],
(angulartics2: Angulartics2, angulartics2AppInsights: Angulartics2AppInsights) => {
angulartics2AppInsights.loadStartTime = Date.now() - 1000;
angulartics2AppInsights.stopTimer();
// 50ms time difference for testing to ensure timing is correct
expect(angulartics2AppInsights.loadTime).toBeLessThanOrEqual(1150);
expect(angulartics2AppInsights.loadTime).toBeGreaterThanOrEqual(1000);
expect(angulartics2AppInsights.loadStartTime).toBe(null);
},
),
));
});