Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ class Connection extends EventEmitter {
throw new Error("Connection is closing");
}

return new Promise((resolve, reject) => {
this.socket.write(string, (err) => (err ? reject(err) : resolve()));
return new Promise((resolve) => {
this.socket.write(string, resolve);
});
}

Expand Down
2 changes: 2 additions & 0 deletions packages/test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import xml from "@xmpp/xml";
import clone from "ltx/lib/clone.js";
import jid from "@xmpp/jid";
import { delay, promise, timeout } from "@xmpp/events";
import id from "@xmpp/id";
Expand All @@ -20,6 +21,7 @@ export {
timeout,
id,
mockSocket,
clone,
};

export function mockInput(entity, el) {
Expand Down
17 changes: 4 additions & 13 deletions packages/websocket/lib/Socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,9 @@ export default class Socket extends EventEmitter {
}

write(data, fn) {
function done(err) {
if (!fn) return;
// eslint-disable-next-line promise/catch-or-return, promise/no-promise-in-callback
Promise.resolve().then(() => fn(err));
}

try {
this.socket.send(data);
} catch (err) {
done(err);
return;
}
done();
this.socket.send(data);
Promise.resolve()
.then(fn)
.catch(() => {});
}
}
14 changes: 10 additions & 4 deletions packages/websocket/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import xml from "@xmpp/xml";
import ConnectionWebSocket from "../lib/Connection.js";
import Socket from "../lib/Socket.js";

test("send()", () => {
test("send()", async () => {
const connection = new ConnectionWebSocket();
connection.write = () => {};
connection.socket = new Socket();
connection.socket.socket = {
send: jest.fn(),
};
connection.root = xml("root");

const element = xml("presence");

expect(element.attrs.xmlns).toBe(undefined);
expect(element.parent).toBe(null);
connection.send(element);
await connection.send(element);
expect(element.attrs.xmlns).toBe("jabber:client");
expect(element.parent).toBe(connection.root);
});
Expand Down Expand Up @@ -70,6 +73,9 @@ test("socket close", () => {
test("sendMany", async () => {
const conn = new ConnectionWebSocket();
conn.socket = new Socket();
conn.socket.socket = {
send: jest.fn(),
};
const spy_write = jest.spyOn(conn.socket, "write");
conn.root = xml("root");

Expand All @@ -82,7 +88,7 @@ test("sendMany", async () => {
expect(element.parent).toBe(null);
}

conn.sendMany(elements);
await conn.sendMany(elements);

for (const element of elements) {
expect(element.attrs.xmlns).toBe("jabber:client");
Expand Down
Loading