Skip to content

Commit 9201907

Browse files
CopilotMSNev
andauthored
Fix unload() to return promise when called without parameters (#2666)
* Initial plan * Fix unload function to return promise by default and add tests Co-authored-by: MSNev <[email protected]> * Use isPromiseLike helper in unload tests Co-authored-by: MSNev <[email protected]> * Fix TypeScript errors in test cases - use testCase instead of testCaseAsync Co-authored-by: MSNev <[email protected]> * Remove unused strSubstring import Co-authored-by: MSNev <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: MSNev <[email protected]>
1 parent 76d20b9 commit 9201907

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "@microsoft/applicationinsights-common";
1414
import { getGlobal } from "@microsoft/applicationinsights-shims";
1515
import { TelemetryContext } from "@microsoft/applicationinsights-properties-js";
16-
import { dumpObj, objHasOwnProperty, strSubstring } from "@nevware21/ts-utils";
16+
import { dumpObj, isPromiseLike, objHasOwnProperty } from "@nevware21/ts-utils";
1717
import { AppInsightsSku } from "../../../src/AISku";
1818

1919
const TestInstrumentationKey = 'b7170927-2d1c-44f1-acec-59f4e1751c11';
@@ -1016,6 +1016,47 @@ export class SnippetInitializationTests extends AITestClass {
10161016
Assert.equal(envelope.sampleRate, 50, "sampleRate is generated");
10171017
}
10181018
})
1019+
1020+
this.testCase({
1021+
name: 'Unload: unload() without parameters should return a promise',
1022+
test: () => {
1023+
let theSnippet = this._initializeSnippet(snippetCreator(getSnippetConfig(this.sessionPrefix)));
1024+
const result = theSnippet.unload();
1025+
Assert.ok(result, "unload() should return a promise when called without parameters");
1026+
Assert.ok(isPromiseLike(result), "returned value should be promise-like");
1027+
}
1028+
});
1029+
1030+
this.testCase({
1031+
name: 'Unload: unload(true) should return a promise',
1032+
test: () => {
1033+
let theSnippet = this._initializeSnippet(snippetCreator(getSnippetConfig(this.sessionPrefix)));
1034+
const result = theSnippet.unload(true);
1035+
Assert.ok(result, "unload(true) should return a promise");
1036+
Assert.ok(isPromiseLike(result), "returned value should be promise-like");
1037+
}
1038+
});
1039+
1040+
this.testCase({
1041+
name: 'Unload: unload(false) should not return a promise',
1042+
test: () => {
1043+
let theSnippet = this._initializeSnippet(snippetCreator(getSnippetConfig(this.sessionPrefix)));
1044+
const result = theSnippet.unload(false);
1045+
Assert.equal(result, undefined, "unload(false) should return undefined");
1046+
}
1047+
});
1048+
1049+
this.testCase({
1050+
name: 'Unload: unload with callback should not return a promise',
1051+
test: () => {
1052+
let theSnippet = this._initializeSnippet(snippetCreator(getSnippetConfig(this.sessionPrefix)));
1053+
let callbackCalled = false;
1054+
const result = theSnippet.unload(true, () => {
1055+
callbackCalled = true;
1056+
});
1057+
Assert.equal(result, undefined, "unload with callback should return undefined");
1058+
}
1059+
});
10191060
}
10201061

10211062
private _initializeSnippet(snippet: Snippet): IApplicationInsights {

AISKU/src/AISku.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ export class AppInsightsSku implements IApplicationInsights {
517517
_self.unload = (isAsync?: boolean, unloadComplete?: (unloadState: ITelemetryUnloadState) => void, cbTimeout?: number): void | IPromise<ITelemetryUnloadState> => {
518518
let unloadDone = false;
519519
let result: IPromise<ITelemetryUnloadState>;
520-
if (isAsync && !unloadComplete) {
520+
if (isAsync !== false && !unloadComplete) {
521521
result = createPromise<ITelemetryUnloadState>((resolve) => {
522522
// Set the callback to the promise resolve callback
523523
unloadComplete = resolve;

0 commit comments

Comments
 (0)