import { mockClient, promiseSend, mockInput, promiseError } from "@xmpp/test";
test("empty result when the handler returns true", async () => {
const xmpp = mockClient();
const { iqCallee } = xmpp;
iqCallee.get("bar", "foo", () => true);
mockInput(
xmpp,
,
);
expect(await promiseSend(xmpp)).toEqual();
});
test("iqs with text children are valid", async () => {
const xmpp = mockClient();
const { iqCallee } = xmpp;
iqCallee.get("bar", "foo", () => true);
mockInput(
xmpp,
{"\n"}
{"foo"}
,
);
expect(await promiseSend(xmpp)).toEqual();
});
test("iqs with multiple element children are invalid", async () => {
const xmpp = mockClient();
const { iqCallee } = xmpp;
iqCallee.get("bar", "foo", () => true);
mockInput(
xmpp,
,
);
expect(await promiseSend(xmpp)).toEqual(
,
);
});
test("non empty result when the handler returns an xml.Element", async () => {
const xmpp = mockClient();
const { iqCallee } = xmpp;
iqCallee.get("bar", "foo", () => {
return ;
});
mockInput(
xmpp,
,
);
expect(await promiseSend(xmpp)).toEqual(
,
);
});
test("service unavailable error reply when there are no handler", async () => {
const xmpp = mockClient();
xmpp.mockInput(
,
);
expect(await promiseSend(xmpp)).toEqual(
,
);
});
test("internal server error reply when handler throws an error", async () => {
const xmpp = mockClient();
const { iqCallee } = xmpp;
const error = new Error("foobar");
const errorPromise = promiseError(xmpp);
const outputPromise = promiseSend(xmpp);
iqCallee.get("bar", "foo", () => {
throw error;
});
mockInput(
xmpp,
,
);
expect(await errorPromise).toBe(error);
expect(await outputPromise).toEqual(
,
);
});
test("internal server error reply when handler rejects with an error", async () => {
const xmpp = mockClient();
const { iqCallee } = xmpp;
const error = new Error("foobar");
const errorPromise = promiseError(xmpp);
const outputPromise = promiseSend(xmpp);
iqCallee.set("bar", "foo", () => {
return Promise.reject(error);
});
mockInput(
xmpp,
,
);
expect(await errorPromise).toBe(error);
expect(await outputPromise).toEqual(
,
);
});
test("stanza error reply when handler returns an error element", async () => {
const xmpp = mockClient();
const { iqCallee } = xmpp;
const outputPromise = promiseSend(xmpp);
const errorElement = (
);
iqCallee.set("bar", "foo", () => {
return errorElement;
});
mockInput(
xmpp,
,
);
expect(await outputPromise).toEqual(
{errorElement}
,
);
});