@microsoft/applicationinsights-react-js 3.2.2-nightly.2111-05 → 3.2.2-nightly.2111-09

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/api-extractor.json +361 -0
  2. package/applicationinsights-react-js.build.error.log +40 -0
  3. package/applicationinsights-react-js.build.log +93 -0
  4. package/browser/applicationinsights-react-js.js +10 -3
  5. package/browser/applicationinsights-react-js.js.map +1 -1
  6. package/browser/applicationinsights-react-js.min.js +2 -2
  7. package/browser/applicationinsights-react-js.min.js.map +1 -1
  8. package/dist/applicationinsights-react-js.api.json +2 -2
  9. package/dist/applicationinsights-react-js.d.ts +1 -1
  10. package/dist/applicationinsights-react-js.js +10 -3
  11. package/dist/applicationinsights-react-js.js.map +1 -1
  12. package/dist/applicationinsights-react-js.min.js +2 -2
  13. package/dist/applicationinsights-react-js.min.js.map +1 -1
  14. package/dist/applicationinsights-react-js.rollup.d.ts +1 -1
  15. package/dist-esm/AppInsightsContext.js +1 -1
  16. package/dist-esm/AppInsightsErrorBoundary.js +1 -1
  17. package/dist-esm/Interfaces/IReactExtensionConfig.js +1 -1
  18. package/dist-esm/ReactPlugin.js +12 -3
  19. package/dist-esm/ReactPlugin.js.map +1 -1
  20. package/dist-esm/applicationinsights-react-js.js +1 -1
  21. package/dist-esm/useTrackEvent.js +1 -1
  22. package/dist-esm/useTrackMetric.js +1 -1
  23. package/dist-esm/withAITracking.js +1 -1
  24. package/microsoft-applicationinsights-react-js-3.2.2-nightly.2111-09.tgz +0 -0
  25. package/package.json +14 -13
  26. package/rollup.config.js +148 -0
  27. package/src/ReactPlugin.ts +12 -3
  28. package/temp/applicationinsights-react-js.api.md +98 -0
  29. package/test/AppInsightsErrorBoundary.test.tsx +88 -0
  30. package/test/ReactAI.test.ts +158 -0
  31. package/test/TestComponent.tsx +31 -0
  32. package/test/jestconfig.json +21 -0
  33. package/test/tsconfig.json +16 -0
  34. package/test/withAITracking.test.tsx +72 -0
  35. package/tslint.json +5 -0
  36. package/types/tsdoc-metadata.json +1 -1
@@ -0,0 +1,158 @@
1
+ import { AppInsightsCore, IConfiguration, DiagnosticLogger, ITelemetryItem, IPlugin } from "@microsoft/applicationinsights-core-js";
2
+ import { IPageViewTelemetry } from "@microsoft/applicationinsights-common";
3
+ import ReactPlugin from "../src/ReactPlugin";
4
+ import { IReactExtensionConfig } from "../src/Interfaces/IReactExtensionConfig";
5
+ import { createBrowserHistory } from "history";
6
+
7
+ let reactPlugin: ReactPlugin;
8
+ let core: AppInsightsCore;
9
+ let orgWarn = console && console.warn;
10
+
11
+ describe("ReactAI", () => {
12
+
13
+ beforeEach(() => {
14
+ if (orgWarn) {
15
+ console.warn = (msg) => { /* Swallow */ }
16
+ }
17
+ });
18
+
19
+ afterEach(() => {
20
+ if (orgWarn) {
21
+ console.warn = orgWarn;
22
+ }
23
+ });
24
+
25
+ function init() {
26
+ core = new AppInsightsCore();
27
+ core.logger = new DiagnosticLogger();
28
+ reactPlugin = new ReactPlugin();
29
+ }
30
+
31
+ it("React Configuration: Config options can be passed from root config", () => {
32
+ const history = createBrowserHistory();
33
+ init();
34
+ reactPlugin.initialize({
35
+ instrumentationKey: 'instrumentation_key',
36
+ extensionConfig: {
37
+ [reactPlugin.identifier]: {
38
+ history
39
+ }
40
+ }
41
+ }, core, []);
42
+ const reactConfig: IReactExtensionConfig = reactPlugin['_extensionConfig'];
43
+ expect(reactConfig.history).toEqual(history);
44
+ });
45
+
46
+ it("React PageView using Analytics plugin", () => {
47
+ const history = createBrowserHistory();
48
+ jest.useFakeTimers();
49
+ init();
50
+ const analyticsExtension = {
51
+ initialize: (config, core, extensions) => { },
52
+ trackEvent: (event, customProperties) => { },
53
+ trackPageView: (pageView, customProperties) => { },
54
+ trackException: (exception, customProperties) => { },
55
+ trackTrace: (trace, customProperties) => { },
56
+ trackMetric: (metric, customProperties) => { },
57
+ _onerror: (exception) => { },
58
+ startTrackPage: (name) => { },
59
+ stopTrackPage: (name, properties, measurements) => { },
60
+ startTrackEvent: (name) => { },
61
+ stopTrackEvent: (name, properties, measurements) => { },
62
+ addTelemetryInitializer: (telemetryInitializer) => { },
63
+ trackPageViewPerformance: (pageViewPerformance, customProperties) => { },
64
+ processTelemetry: (env) => { },
65
+ setNextPlugin: (next) => { },
66
+ identifier: "ApplicationInsightsAnalytics"
67
+ };
68
+ const channel = new ChannelPlugin();
69
+ const config: IConfiguration = {
70
+ instrumentationKey: 'instrumentation_key',
71
+ extensionConfig: {
72
+ [reactPlugin.identifier]: {
73
+ history
74
+ },
75
+ }
76
+ };
77
+ core.initialize(config, [reactPlugin, analyticsExtension, channel]);
78
+ // Mock Analytics track
79
+ const analyticsMock = analyticsExtension.trackPageView = jest.fn();
80
+
81
+ // Emulate navigation to different URL-addressed pages
82
+ history.push("/home", { some: "state" });
83
+ history.push("/new-fancy-page");
84
+ jest.runOnlyPendingTimers();
85
+ expect(analyticsExtension.trackPageView).toHaveBeenCalledTimes(2);
86
+ let event: IPageViewTelemetry = analyticsMock.mock.calls[0][0]
87
+ expect(event.uri).toBe("/home");
88
+ event = analyticsMock.mock.calls[1][0]
89
+ expect(event.uri).toBe("/new-fancy-page");
90
+ });
91
+
92
+ it("React plugin with Analytics not available", () => {
93
+ const history = createBrowserHistory();
94
+ jest.useFakeTimers();
95
+ init();
96
+ const channel = new ChannelPlugin();
97
+ const config: IConfiguration = {
98
+ instrumentationKey: 'instrumentation_key',
99
+ extensionConfig: {
100
+ [reactPlugin.identifier]: {
101
+ history
102
+ }
103
+ }
104
+ };
105
+ core.initialize(config, [reactPlugin, channel]);
106
+ // Mock logger
107
+ const loggerMock = core["logger"].throwInternal = jest.fn();
108
+ history.push("/home");
109
+ jest.runOnlyPendingTimers();
110
+ expect(loggerMock).toHaveBeenCalledTimes(1);
111
+ });
112
+ });
113
+
114
+ class ChannelPlugin implements IPlugin {
115
+ public isFlushInvoked = false;
116
+ public isTearDownInvoked = false;
117
+ public isResumeInvoked = false;
118
+ public isPauseInvoked = false;
119
+
120
+ public identifier = "Sender";
121
+
122
+ public priority: number = 1001;
123
+
124
+ constructor() {
125
+ this.processTelemetry = this._processTelemetry.bind(this);
126
+ }
127
+ public pause(): void {
128
+ this.isPauseInvoked = true;
129
+ }
130
+
131
+ public resume(): void {
132
+ this.isResumeInvoked = true;
133
+ }
134
+
135
+ public teardown(): void {
136
+ this.isTearDownInvoked = true;
137
+ }
138
+
139
+ flush(async?: boolean, callBack?: () => void): void {
140
+ this.isFlushInvoked = true;
141
+ if (callBack) {
142
+ callBack();
143
+ }
144
+ }
145
+
146
+ public processTelemetry(env: ITelemetryItem) { }
147
+
148
+ setNextPlugin(next: any) {
149
+ // no next setup
150
+ }
151
+
152
+ public initialize = (config: IConfiguration, core: AppInsightsCore, plugin: IPlugin[]) => {
153
+ }
154
+
155
+ private _processTelemetry(env: ITelemetryItem) {
156
+
157
+ }
158
+ }
@@ -0,0 +1,31 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ import * as React from "react";
5
+
6
+ export interface ITestComponentProps {
7
+ prop1?: number;
8
+ prop2?: string;
9
+ }
10
+
11
+ export class TestComponent extends React.Component<ITestComponentProps> {
12
+ public render() {
13
+ const { prop1, prop2 } = this.props;
14
+ return (
15
+ <div>
16
+ <h1>TestComponent</h1>
17
+ prop1: {prop1}, prop2: {prop2}
18
+ </div>
19
+ );
20
+ }
21
+ }
22
+
23
+ export default TestComponent;
24
+
25
+ export class ErrorTestComponent extends React.Component {
26
+ public render(): React.ReactNode {
27
+ if (true) {
28
+ throw new Error("Error");
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ "transform": {
3
+ "^.+\\.(t|j)sx?$": "ts-jest"
4
+ },
5
+ "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
6
+ "testEnvironment": "jsdom",
7
+ "moduleFileExtensions": [
8
+ "ts",
9
+ "tsx",
10
+ "js",
11
+ "jsx",
12
+ "json",
13
+ "node"
14
+ ],
15
+ "testPathIgnorePatterns": [
16
+ "node_modules",
17
+ "legacy",
18
+ "dist",
19
+ "dist-esm"
20
+ ]
21
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "sourceMap": true,
4
+ "inlineSources": true,
5
+ "noImplicitAny": false,
6
+ "module": "amd",
7
+ "moduleResolution": "Node",
8
+ "target": "es5",
9
+ "alwaysStrict": true,
10
+ "declaration": true,
11
+ "jsx": "react",
12
+ "esModuleInterop": true,
13
+ "allowJs": true
14
+ },
15
+ "files": []
16
+ }
@@ -0,0 +1,72 @@
1
+ import { IPageViewTelemetry, IMetricTelemetry } from "@microsoft/applicationinsights-common";
2
+ import * as React from "react";
3
+ import ReactPlugin from "../src/ReactPlugin";
4
+ import withAITracking from "../src/withAITracking";
5
+ import { TestComponent } from "./TestComponent";
6
+ import { render, screen } from '@testing-library/react';
7
+ import userEvent from '@testing-library/user-event';
8
+ import '@testing-library/jest-dom/extend-expect';
9
+
10
+ let reactPlugin: ReactPlugin;
11
+ let TestComponentWithTracking;
12
+ let trackedTestComponentUnmount;
13
+ let trackMetricSpy;
14
+ let orgWarn = console && console.warn;
15
+
16
+ describe("withAITracking(TestComponent)", () => {
17
+
18
+ beforeEach(() => {
19
+ if (orgWarn) {
20
+ console.warn = (msg) => { /* Swallow */ }
21
+ }
22
+ reactPlugin = new ReactPlugin();
23
+ TestComponentWithTracking = withAITracking(reactPlugin, TestComponent);
24
+ const testComponentWithTracking = render(<TestComponentWithTracking />);
25
+ trackedTestComponentUnmount = testComponentWithTracking.unmount;
26
+ trackMetricSpy = reactPlugin.trackMetric = jest.fn();
27
+ });
28
+
29
+ afterEach(() => {
30
+ if (orgWarn) {
31
+ console.warn = orgWarn;
32
+ }
33
+ });
34
+
35
+ it("should wrap <TestComponent />", () => {
36
+ expect(screen.getByText('TestComponent')).toBeInTheDocument();
37
+ });
38
+
39
+ it("shouldn't call trackMetric if there's no user interaction", () => {
40
+ trackedTestComponentUnmount();
41
+ expect(trackMetricSpy).toHaveBeenCalledTimes(0);
42
+ });
43
+
44
+
45
+ it("should call trackMetric if there is user interaction", () => {
46
+ userEvent.click(screen.getByText('TestComponent'));
47
+ trackedTestComponentUnmount();
48
+ expect(trackMetricSpy).toHaveBeenCalledTimes(1);
49
+ const metricTelemetry: IMetricTelemetry & IPageViewTelemetry = {
50
+ average: expect.any(Number),
51
+ name: "React Component Engaged Time (seconds)",
52
+ sampleCount: 1
53
+ };
54
+ expect(trackMetricSpy).toHaveBeenCalledWith(metricTelemetry, { "Component Name": "TestComponent" });
55
+ });
56
+
57
+ it("should use the passed component name in trackMetric", () => {
58
+ trackedTestComponentUnmount();
59
+ const TestComponentWithTrackingCustomName = withAITracking(reactPlugin, TestComponent, "MyCustomName");
60
+ const { container, unmount } = render(<TestComponentWithTrackingCustomName />);
61
+ userEvent.click(screen.getByText('TestComponent'));
62
+ unmount();
63
+
64
+ expect(trackMetricSpy).toHaveBeenCalledTimes(1);
65
+ const metricTelemetry: IMetricTelemetry & IPageViewTelemetry = {
66
+ average: expect.any(Number),
67
+ name: "React Component Engaged Time (seconds)",
68
+ sampleCount: 1
69
+ };
70
+ expect(trackMetricSpy).toHaveBeenCalledWith(metricTelemetry, { "Component Name": "MyCustomName" });
71
+ });
72
+ });
package/tslint.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": [
3
+ "../../tslint-base.json"
4
+ ]
5
+ }
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.18.17"
8
+ "packageVersion": "7.18.19"
9
9
  }
10
10
  ]
11
11
  }