This repository has been archived by the owner on May 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
withAITracking.test.tsx
76 lines (65 loc) · 2.77 KB
/
withAITracking.test.tsx
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { ApplicationInsights, IMetricTelemetry, IPageViewTelemetry } from "@microsoft/applicationinsights-web";
import * as Enzyme from "enzyme";
import * as Adapter from "enzyme-adapter-react-16";
import * as React from "react";
import { reactAI, withAITracking } from "../src";
import { TestComponent } from "./TestComponent";
const IKEY: string = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx";
Enzyme.configure({ adapter: new Adapter.default() });
let trackMetricSpy: jest.SpyInstance;
let appInsights: ApplicationInsights;
describe("withAITracking(TestComponent)", () => {
const TestComponentWithTracking = withAITracking(TestComponent);
const trackedTestComponentWrapper = () => Enzyme.shallow(<TestComponentWithTracking />);
beforeEach(() => {
appInsights = new ApplicationInsights({
config: {
extensionConfig: {
[reactAI.extensionId]: { debug: false }
},
extensions: [reactAI],
instrumentationKey: IKEY
}
});
appInsights.loadAppInsights();
trackMetricSpy = jest.spyOn(reactAI.appInsights, "trackMetric");
});
it("should wrap <TestComponent />", () => {
const component = trackedTestComponentWrapper();
expect(component.find(TestComponent).length).toBe(1);
});
it("shouldn't call trackMetric if there's no user interaction", () => {
const component = trackedTestComponentWrapper();
component.unmount();
expect(trackMetricSpy).toHaveBeenCalledTimes(0);
});
it("should call trackMetric if there is user interaction", () => {
const component = trackedTestComponentWrapper();
component.simulate("keydown");
component.unmount();
expect(trackMetricSpy).toHaveBeenCalledTimes(1);
const metricTelemetry: IMetricTelemetry & IPageViewTelemetry = {
average: expect.any(Number),
name: "React Component Engaged Time (seconds)",
properties: expect.any(Object),
sampleCount: 1
};
expect(trackMetricSpy).toHaveBeenCalledWith(metricTelemetry, { "Component Name": "TestComponent" });
});
it("should use the passed component name in trackMetric", () => {
const TestComponentWithTrackingCustomName = withAITracking(TestComponent, "MyCustomName");
const component = Enzyme.shallow(<TestComponentWithTrackingCustomName />);
component.simulate("mousemove");
component.unmount();
expect(trackMetricSpy).toHaveBeenCalledTimes(1);
const metricTelemetry: IMetricTelemetry & IPageViewTelemetry = {
average: expect.any(Number),
name: "React Component Engaged Time (seconds)",
properties: expect.any(Object),
sampleCount: 1
};
expect(trackMetricSpy).toHaveBeenCalledWith(metricTelemetry, { "Component Name": "MyCustomName" });
});
});