-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsender.spec.ts
112 lines (102 loc) · 3.81 KB
/
sender.spec.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import chai from "chai";
import { ServiceBusMessageBatchImpl } from "../../src/serviceBusMessageBatch";
import { ClientEntityContext } from "../../src/clientEntityContext";
import { ServiceBusMessage } from "../../src";
import { isServiceBusMessageBatch, SenderImpl } from "../../src/sender";
import { DefaultDataTransformer } from "@azure/core-amqp";
const assert = chai.assert;
describe("sender unit tests", () => {
it("isServiceBusMessageBatch", () => {
assert.isTrue(
isServiceBusMessageBatch(new ServiceBusMessageBatchImpl({} as ClientEntityContext, 100))
);
assert.isFalse(isServiceBusMessageBatch(undefined));
assert.isFalse(isServiceBusMessageBatch((4 as any) as ServiceBusMessage));
assert.isFalse(isServiceBusMessageBatch(({} as any) as ServiceBusMessage));
});
["hello", {}, 123, null, undefined, ["hello"]].forEach((invalidValue) => {
it(`don't allow Sender.sendMessages(${invalidValue})`, async () => {
const sender = new SenderImpl(createClientEntityContextForTests());
let expectedErrorMsg =
"Provided value for 'messages' must be of type ServiceBusMessage, ServiceBusMessageBatch or an array of type ServiceBusMessage.";
if (invalidValue === null || invalidValue === undefined) {
expectedErrorMsg = `Missing parameter "messages"`;
}
try {
await sender.sendMessages(
// @ts-expect-error
invalidValue
);
} catch (err) {
assert.equal(err.name, "TypeError");
assert.equal(err.message, expectedErrorMsg);
}
});
});
["hello", {}, null, undefined].forEach((invalidValue) => {
it(`don't allow tryAdd(${invalidValue})`, async () => {
const sender = new SenderImpl(createClientEntityContextForTests());
const batch = await sender.createBatch();
let expectedErrorMsg = "Provided value for 'message' must be of type ServiceBusMessage.";
if (invalidValue === null || invalidValue === undefined) {
expectedErrorMsg = `Missing parameter "message"`;
}
try {
batch.tryAdd(
// @ts-expect-error
invalidValue
);
} catch (err) {
assert.equal(err.name, "TypeError");
assert.equal(err.message, expectedErrorMsg);
}
});
});
["hello", {}, null, undefined, ["hello"]].forEach((invalidValue) => {
it(`don't allow Sender.scheduleMessages(${invalidValue})`, async () => {
const sender = new SenderImpl(createClientEntityContextForTests());
let expectedErrorMsg =
"Provided value for 'messages' must be of type ServiceBusMessage or an array of type ServiceBusMessage.";
if (invalidValue === null || invalidValue === undefined) {
expectedErrorMsg = `Missing parameter "messages"`;
}
try {
await sender.scheduleMessages(
new Date(),
// @ts-expect-error
invalidValue
);
} catch (err) {
assert.equal(err.name, "TypeError");
assert.equal(err.message, expectedErrorMsg);
}
});
});
});
function createClientEntityContextForTests(): ClientEntityContext & { initWasCalled: boolean } {
let initWasCalled = false;
const fakeClientEntityContext = {
entityPath: "queue",
sender: {
credit: 999,
createBatch: () => {
return new ServiceBusMessageBatchImpl(fakeClientEntityContext as any, 100);
}
},
namespace: {
config: { endpoint: "my.service.bus" },
connectionId: "connection-id",
dataTransformer: new DefaultDataTransformer(),
cbsSession: {
cbsLock: "cbs-lock",
async init() {
initWasCalled = true;
}
}
},
initWasCalled
};
return (fakeClientEntityContext as any) as ReturnType<typeof createClientEntityContextForTests>;
}