-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathbind2.test.js
115 lines (91 loc) · 2.74 KB
/
bind2.test.js
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
113
114
115
import { mockClient, id, promiseError } from "@xmpp/test";
function mockFeatures(entity) {
entity.mockInput(
<features xmlns="http://etherx.jabber.org/streams">
<authentication xmlns="urn:xmpp:sasl:2">
<mechanism>PLAIN</mechanism>
<inline>
<bind xmlns="urn:xmpp:bind:0" />
</inline>
</authentication>
</features>,
);
}
function catchAuthenticate(entity) {
return entity.catchOutgoing((stanza) => {
if (stanza.is("authenticate", "urn:xmpp:sasl:2")) return true;
});
}
test("without tag", async () => {
const { entity } = mockClient();
mockFeatures(entity);
const stanza = await catchAuthenticate(entity);
await expect(stanza.getChild("bind", "urn:xmpp:bind:0").toString()).toEqual(
(<bind xmlns="urn:xmpp:bind:0" />).toString(),
);
});
test("with string tag", async () => {
const resource = id();
const { entity } = mockClient({ resource });
mockFeatures(entity);
const stanza = await catchAuthenticate(entity);
expect(stanza.getChild("bind", "urn:xmpp:bind:0").toString()).toEqual(
(
<bind xmlns="urn:xmpp:bind:0">
<tag>{resource}</tag>
</bind>
).toString(),
);
});
test("with function resource returning string", async () => {
// eslint-disable-next-line unicorn/consistent-function-scoping
function resource() {
return "1k2k3";
}
const { entity } = mockClient({ resource });
mockFeatures(entity);
const stanza = await catchAuthenticate(entity);
expect(stanza.getChild("bind", "urn:xmpp:bind:0").toString()).toEqual(
(
<bind xmlns="urn:xmpp:bind:0">
<tag>{resource()}</tag>
</bind>
).toString(),
);
});
test("with function resource throwing", async () => {
const error = new Error("foo");
function resource() {
throw error;
}
const { entity } = mockClient({ resource });
const willError = promiseError(entity);
mockFeatures(entity);
expect(await willError).toBe(error);
});
test("with function resource returning resolved promise", async () => {
// eslint-disable-next-line unicorn/consistent-function-scoping
async function resource() {
return "1k2k3";
}
const { entity } = mockClient({ resource });
mockFeatures(entity);
const stanza = await catchAuthenticate(entity);
expect(stanza.getChild("bind", "urn:xmpp:bind:0").toString()).toEqual(
(
<bind xmlns="urn:xmpp:bind:0">
<tag>{await resource()}</tag>
</bind>
).toString(),
);
});
test("with function resource returning rejected promise", async () => {
const error = new Error("foo");
async function resource() {
throw error;
}
const { entity } = mockClient({ resource });
const willError = promiseError(entity);
mockFeatures(entity);
expect(await willError).toBe(error);
});