Skip to content

Commit

Permalink
Add snippet connection string tests (#1999)
Browse files Browse the repository at this point in the history
  • Loading branch information
siyuniu-ms committed Feb 28, 2023
2 parents fcadeec + 3398b17 commit 67f7ab1
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getGlobal } from "@microsoft/applicationinsights-shims";
import { TelemetryContext } from "@microsoft/applicationinsights-properties-js";

const TestInstrumentationKey = 'b7170927-2d1c-44f1-acec-59f4e1751c11';
const TestConnectionString = 'InstrumentationKey=b7170927-2d1c-44f1-acec-59f4e1751c11';

const _expectedBeforeProperties = [
"config",
Expand Down Expand Up @@ -62,6 +63,63 @@ function getSnippetConfig(sessionPrefix: string) {
};
};

function getSnippetConfigConnectionString(sessionPrefix: string) {
return {
src: "",
cfg: {
connectionString: TestConnectionString,
disableAjaxTracking: false,
disableFetchTracking: false,
enableRequestHeaderTracking: true,
enableResponseHeaderTracking: true,
maxBatchInterval: 500,
disableExceptionTracking: false,
namePrefix: `sessionPrefix`,
enableCorsCorrelation: true,
distributedTracingMode: DistributedTracingModes.AI_AND_W3C,
samplingPercentage: 50
} as IConfig
};
};

function getSnippetConfigWrongConnectionString(sessionPrefix: string) {
return {
src: "",
cfg: {
connectionString: 'wrong connection string'+TestConnectionString,
disableAjaxTracking: false,
disableFetchTracking: false,
enableRequestHeaderTracking: true,
enableResponseHeaderTracking: true,
maxBatchInterval: 500,
disableExceptionTracking: false,
namePrefix: `sessionPrefix`,
enableCorsCorrelation: true,
distributedTracingMode: DistributedTracingModes.AI_AND_W3C,
samplingPercentage: 50
} as IConfig
};
};

function getSnippetConfigNotSetConnectionString(sessionPrefix: string) {
return {
src: "",
cfg: {
connectionString: '',
disableAjaxTracking: false,
disableFetchTracking: false,
enableRequestHeaderTracking: true,
enableResponseHeaderTracking: true,
maxBatchInterval: 500,
disableExceptionTracking: false,
namePrefix: `sessionPrefix`,
enableCorsCorrelation: true,
distributedTracingMode: DistributedTracingModes.AI_AND_W3C,
samplingPercentage: 50
} as IConfig
};
};

export class SnippetInitializationTests extends AITestClass {

// Context
Expand Down Expand Up @@ -114,6 +172,59 @@ export class SnippetInitializationTests extends AITestClass {
}
});

this.testCaseAsync({
name: "checkConnectionString",
stepDelay: 100,
steps: [
() => {
let theSnippet = this._initializeSnippet(snippetCreator(getSnippetConfigConnectionString(this.sessionPrefix)));
theSnippet.trackEvent({ name: 'event', properties: { "prop1": "value1" }, measurements: { "measurement1": 200 } });
}
]
.concat(this.asserts(1)).concat(() => {
const payloadStr: string[] = this.getPayloadMessages(this.successSpy);
if (payloadStr.length > 0) {
const payload = JSON.parse(payloadStr[0]);
const data = payload.data;
Assert.ok(data && data.baseData && data.baseData.properties["prop1"]);
Assert.ok(data && data.baseData && data.baseData.measurements["measurement1"]);
}
})
});

this.testCase({
name: "checkIncorrectConnectionString",
test: () => {
let theSnippet:any = null;
let exception: Error = null;
this.useFakeServer = false;
try {
let snippet:Snippet = snippetCreator(getSnippetConfigWrongConnectionString(this.sessionPrefix));
// Call the initialization
((ApplicationInsightsContainer.getAppInsights(snippet, snippet.version)) as IApplicationInsights);
} catch (e) {
Assert.equal(e.message, "Please provide instrumentation key", "Server would not start when get incorrect connection string");
}
}
});

this.testCase({
name: "checkConnectionStringNotSet",
test: () => {
let theSnippet:any = null;
let exception: Error = null;
this.useFakeServer = false;
try {
let snippet:Snippet = snippetCreator(getSnippetConfigNotSetConnectionString(this.sessionPrefix));
// Call the initialization
((ApplicationInsightsContainer.getAppInsights(snippet, snippet.version)) as IApplicationInsights);
} catch (e) {
Assert.equal(e.message, "Please provide instrumentation key", "Server would not start without connection string");
}
}
});


this.testCaseAsync({
name: "[" + snippetName + "] : Public Members exist",
stepDelay: 100,
Expand Down

0 comments on commit 67f7ab1

Please sign in to comment.