Skip to content

Commit

Permalink
fix: communication-sms: remove mocha arrow functions (#26659)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR

`sdk\communication\communication-sms`

### Issues associated with this PR

#13005 

### Describe the problem that is addressed by this PR

The existing mocha tests for the `sdk\communication\communication-sms`
made use of the arrow syntax for callback functions. Mocha recommends
not to do this because you lose access to the mocha context
(https://mochajs.org/#arrow-functions).

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?

The reason for utilizing the function keyword instead of an arrow syntax
to write the callback functions in these mocha tests is to maintain
access to the mocha context.

### Are there test cases added in this PR? _(If not, why?)_

No additional test cases were added in this PR as the change only
required modifying existing test cases.

### Provide a list of related PRs _(if any)_

#23761 - Same fix, but for the `sdk\search\search-documents` package
#23789 - Same fix but for the `sdk\attestation\attestation` package
#23835 - Same fix but for the `sdk\batch\batch` package
#23850 - Same fix but for the
`sdk\cognitivelanguage\ai-language-conversations` package
#23881 - Same fix but for the
`sdk\cognitiveservices\cognitiveservices-luis-authoring` package
#24126 - Same fix but for the
`sdk\cognitiveservices\cognitiveservices-luis-runtime` package
#21470 - Same fix but for the `sdk\communication\communication-chat`
package
#24746 - Same fix but for the `sdk\communication\communication-common`
package
#24747 - Same fix but for the `sdk\communication\communication-email`
package
#24797 - Same fix but for the `sdk\communication\communication-identity`
package
#24800 - Same fix but for the `sdk\communication\communication-rooms`
package
#24865 - Same fix but for the
`sdk\communication\communication-job-router` package
#25148 - Same fix but for the
`sdk\confidentialledger\confidential-ledger-rest` package

### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

**_Not applicable_**

### Checklists
- [x] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
  • Loading branch information
StevanFreeborn authored Jul 28, 2023
1 parent e0d00e9 commit a32bf42
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import { HttpClient } from "@azure/core-rest-pipeline";

import { Uuid } from "../../src/utils/uuid";
import { generateSendMessageRequest } from "../../src/utils/smsUtils";
import { Uuid } from "../../src/utils/uuid";

import { assert } from "chai";
import sinon from "sinon";
Expand All @@ -15,7 +15,7 @@ import { MockHttpClient } from "../public/utils/mockHttpClient";
const API_VERSION = apiVersion.mapper.defaultValue;
const TEST_NUMBER = "+14255550123";

describe("[mocked] SmsClient Internal", async () => {
describe("[mocked] SmsClient Internal", async function () {
const baseUri = "https://contoso.api.fake";
const connectionString = `endpoint=${baseUri};accesskey=banana`;
let sendRequestSpy: sinon.SinonSpy;
Expand All @@ -29,17 +29,17 @@ describe("[mocked] SmsClient Internal", async () => {
message: "message",
};

describe("when sending an SMS", () => {
describe("when sending an SMS", function () {
let smsClient: SmsClient;
beforeEach(() => {
beforeEach(function () {
uuidStub = sinon.stub(Uuid, "generateUuid");
uuidStub.returns(mockedGuid);
sendRequestSpy = sinon.spy(mockHttpClient, "sendRequest");
sinon.useFakeTimers();
smsClient = new SmsClient(connectionString, { httpClient: mockHttpClient });
});

it("sends with the correct request body", async () => {
it("sends with the correct request body", async function () {
await smsClient.send(testSendRequest);

const request = sendRequestSpy.getCall(0).args[0];
Expand All @@ -49,14 +49,14 @@ describe("[mocked] SmsClient Internal", async () => {
assert.deepEqual(JSON.parse(request.body as string), expectedRequestBody);
});

it("generates a new repeatability id each time", async () => {
it("generates a new repeatability id each time", async function () {
await smsClient.send(testSendRequest);
assert.isTrue(uuidStub.calledOnce);
await smsClient.send(testSendRequest);
assert.isTrue(uuidStub.calledTwice);
});

afterEach(() => {
afterEach(function () {
sinon.restore();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
* These tests will be skipped in Live Mode since the public tests run in live mode only.
*/

import { matrix } from "@azure/test-utils";
import { Recorder, isLiveMode, isPlaybackMode } from "@azure-tools/test-recorder";
import { matrix } from "@azure/test-utils";
import { Context } from "mocha";
import * as sinon from "sinon";
import { SmsClient } from "../../src";
import { Uuid } from "../../src/utils/uuid";
import sendSmsSuites from "../public/suites/smsClient.send";
import {
createRecordedSmsClient,
createRecordedSmsClientWithToken,
} from "../public/utils/recordedClient";
import { Context } from "mocha";
import sendSmsSuites from "../public/suites/smsClient.send";
import { SmsClient } from "../../src";

matrix([[true, false]], async function (useAad: boolean) {
describe(`SmsClient [Playback/Record]${useAad ? " [AAD]" : ""}`, async () => {
describe(`SmsClient [Playback/Record]${useAad ? " [AAD]" : ""}`, async function () {
let recorder: Recorder;
let client: SmsClient;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { isNode } from "@azure/core-util";
import { HttpClient } from "@azure/core-rest-pipeline";
import { AzureKeyCredential } from "@azure/core-auth";
import { HttpClient } from "@azure/core-rest-pipeline";
import { isNode } from "@azure/core-util";
import { TokenCredential } from "@azure/identity";
import { assert } from "chai";
import sinon from "sinon";
import { SmsClient, SmsClientOptions, SmsSendRequest } from "../../src";
import { MockHttpClient } from "./utils/mockHttpClient";
import { TokenCredential } from "@azure/identity";

const TEST_NUMBER = "+14255550123";

describe("[mocked] SmsClient", async () => {
describe("[mocked] SmsClient", async function () {
const baseUri = "https://contoso.api.fake";
const connectionString = `endpoint=${baseUri};accesskey=banana`;
const dateHeader = "x-ms-date";
Expand All @@ -25,16 +25,16 @@ describe("[mocked] SmsClient", async () => {
message: "message",
};

describe("when instantiating SMS client", () => {
it("can instantiate with a connection string", async () => {
describe("when instantiating SMS client", function () {
it("can instantiate with a connection string", async function () {
new SmsClient(connectionString);
});

it("can instantiate with a url and KeyCredential ", async () => {
it("can instantiate with a url and KeyCredential ", async function () {
new SmsClient(baseUri, new AzureKeyCredential("banana"));
});

it("can instantiate with a token", async () => {
it("can instantiate with a token", async function () {
const fakeToken: TokenCredential = {
getToken: async (_scopes) => {
return { token: "testToken", expiresOnTimestamp: 11111 };
Expand All @@ -44,9 +44,9 @@ describe("[mocked] SmsClient", async () => {
});
});

describe("when sending an SMS", () => {
describe("when sending an SMS", function () {
let smsClient: SmsClient;
beforeEach(() => {
beforeEach(function () {
sendRequestSpy = sinon.spy(mockHttpClient, "sendRequest");
sinon.useFakeTimers();
// workaround: casting because min testing has issues with httpClient newer versions having extra optional fields
Expand All @@ -55,7 +55,7 @@ describe("[mocked] SmsClient", async () => {
} as SmsClientOptions);
});

it("sends with the correct headers", async () => {
it("sends with the correct headers", async function () {
await smsClient.send(testSendRequest);

const request = sendRequestSpy.getCall(0).args[0];
Expand All @@ -70,7 +70,7 @@ describe("[mocked] SmsClient", async () => {
);
});

it("returns the correct results", async () => {
it("returns the correct results", async function () {
const smsTestResults = await smsClient.send(testSendRequest);

const smsTestResult = smsTestResults[0];
Expand All @@ -79,7 +79,7 @@ describe("[mocked] SmsClient", async () => {
assert.equal(smsTestResult.messageId, "id");
});

afterEach(() => {
afterEach(function () {
sinon.restore();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
* They are duplicated in an internal test which contains workaround logic to record/playback the tests
*/

import { matrix } from "@azure/test-utils";
import { Recorder, env, isPlaybackMode } from "@azure-tools/test-recorder";
import { createRecordedSmsClient, createRecordedSmsClientWithToken } from "./utils/recordedClient";
import { matrix } from "@azure/test-utils";
import { Context } from "mocha";
import sendSmsSuites from "./suites/smsClient.send";
import { SmsClient } from "../../src";
import sinon from "sinon";
import { SmsClient } from "../../src";
import { Uuid } from "../../src/utils/uuid";
import sendSmsSuites from "./suites/smsClient.send";
import { createRecordedSmsClient, createRecordedSmsClientWithToken } from "./utils/recordedClient";

matrix([[true, false]], async function (useAad: boolean) {
describe(`SmsClient [Live]${useAad ? " [AAD]" : ""}`, async () => {
describe(`SmsClient [Live]${useAad ? " [AAD]" : ""}`, async function () {
let recorder: Recorder;
let client: SmsClient;

Expand Down

0 comments on commit a32bf42

Please sign in to comment.