-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy path_closeStream.js
More file actions
77 lines (60 loc) · 2.02 KB
/
Copy path_closeStream.js
File metadata and controls
77 lines (60 loc) · 2.02 KB
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
import { jest } from "@jest/globals";
import { EventEmitter, promise, TimeoutError } from "@xmpp/events";
import { xml } from "@xmpp/test";
import Connection from "../index.js";
test("resets properties on socket close event", () => {
const conn = new Connection();
conn._attachSocket(new EventEmitter());
conn.status = "online";
conn.socket.emit("connect");
conn.socket.emit("close");
expect(conn.status).toBe("disconnect");
});
test("timeout on parser end", async () => {
const conn = new Connection();
conn.parser = new EventEmitter();
jest.spyOn(conn, "footerElement").mockImplementation(() => xml("hello"));
jest.spyOn(conn, "write").mockImplementation(async () => {});
await expect(conn._closeStream()).rejects.toThrow(new TimeoutError());
});
test("error on status closing", async () => {
const conn = new Connection();
conn.parser = new EventEmitter();
conn.footerElement = () => {
return xml("hello");
};
conn.socket = new EventEmitter();
conn.socket.write = (data, cb) => {
return cb();
};
conn.status = "closing";
conn.parser.emit("end");
await expect(conn._closeStream()).rejects.toThrow(
new Error("Connection is closing"),
);
});
test("resolves", async () => {
const conn = new Connection();
conn.parser = new EventEmitter();
jest.spyOn(conn, "footerElement").mockImplementation(() => xml("hello"));
jest.spyOn(conn, "write").mockImplementation(async () => {});
process.nextTick(() => {
conn.parser.emit("end", xml("goodbye"));
});
const el = await conn._closeStream();
expect(el.toString()).toBe(`<goodbye/>`);
});
test("emits closing status", async () => {
const conn = new Connection();
conn.parser = new EventEmitter();
jest.spyOn(conn, "footerElement").mockImplementation(() => xml("hello"));
jest.spyOn(conn, "write").mockImplementation(async () => {});
process.nextTick(() => {
conn.parser.emit("end");
});
const [status] = await Promise.all([
promise(conn, "status"),
conn._closeStream(),
]);
expect(status).toBe("closing");
});