xmpp.chapril.org-conversejs/spec/hats.js
JC Brand 1949356ede Work on turning chat views into custom elements
The eventual goal is to avoid UI-related stanza processing if the relevant chats
aren't in the DOM.

With the current architecture, chatboxes are created (and the stanzas
related to them processed) even if `#conversejs` isn't in the DOM.

* Initial work on making controlbox an element
* Create a shared base class
* Ceate ChatBoxViews proxy
* Update sass now that certain classes are moved to converse-chats element
2021-02-09 15:48:21 +01:00

79 lines
3.7 KiB
JavaScript

/*global mock */
const u = converse.env.utils;
describe("A XEP-0317 MUC Hat", function () {
it("can be included in a presence stanza",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
const muc_jid = 'lounge@montague.lit';
await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.chatboxviews.get(muc_jid);
const hat1_id = u.getUniqueId();
const hat2_id = u.getUniqueId();
_converse.connection._dataRecv(mock.createRequest(u.toStanza(`
<presence from="${muc_jid}/Terry" id="${u.getUniqueId()}" to="${_converse.jid}">
<x xmlns="http://jabber.org/protocol/muc#user">
<item affiliation="member" role="participant"/>
</x>
<hats xmlns="xmpp:prosody.im/protocol/hats:1">
<hat title="Teacher&apos;s Assistant" id="${hat1_id}"/>
<hat title="Dark Mage" id="${hat2_id}"/>
</hats>
</presence>
`)));
await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
"romeo and Terry have entered the groupchat");
let hats = view.model.getOccupant("Terry").get('hats');
expect(hats.length).toBe(2);
expect(hats.map(h => h.title).join(' ')).toBe("Teacher's Assistant Dark Mage");
_converse.connection._dataRecv(mock.createRequest(u.toStanza(`
<message type="groupchat" from="${muc_jid}/Terry" id="${u.getUniqueId()}" to="${_converse.jid}">
<body>Hello world</body>
</message>
`)));
const msg_el = await u.waitUntil(() => view.querySelector('.chat-msg'));
let badges = Array.from(msg_el.querySelectorAll('.badge'));
expect(badges.length).toBe(2);
expect(badges.map(b => b.textContent.trim()).join(' ' )).toBe("Teacher's Assistant Dark Mage");
const hat3_id = u.getUniqueId();
_converse.connection._dataRecv(mock.createRequest(u.toStanza(`
<presence from="${muc_jid}/Terry" id="${u.getUniqueId()}" to="${_converse.jid}">
<x xmlns="http://jabber.org/protocol/muc#user">
<item affiliation="member" role="participant"/>
</x>
<hats xmlns="xmpp:prosody.im/protocol/hats:1">
<hat title="Teacher&apos;s Assistant" id="${hat1_id}"/>
<hat title="Dark Mage" id="${hat2_id}"/>
<hat title="Mad hatter" id="${hat3_id}"/>
</hats>
</presence>
`)));
await u.waitUntil(() => view.model.getOccupant("Terry").get('hats').length === 3);
hats = view.model.getOccupant("Terry").get('hats');
expect(hats.map(h => h.title).join(' ')).toBe("Teacher's Assistant Dark Mage Mad hatter");
await u.waitUntil(() => view.querySelectorAll('.chat-msg .badge').length === 3, 1000);
badges = Array.from(view.querySelectorAll('.chat-msg .badge'));
expect(badges.map(b => b.textContent.trim()).join(' ' )).toBe("Teacher's Assistant Dark Mage Mad hatter");
_converse.connection._dataRecv(mock.createRequest(u.toStanza(`
<presence from="${muc_jid}/Terry" id="${u.getUniqueId()}" to="${_converse.jid}">
<x xmlns="http://jabber.org/protocol/muc#user">
<item affiliation="member" role="participant"/>
</x>
</presence>
`)));
await u.waitUntil(() => view.model.getOccupant("Terry").get('hats').length === 0);
await u.waitUntil(() => view.querySelectorAll('.chat-msg .badge').length === 0);
done();
}));
})