Skip to content

Commit

Permalink
More usage of the stx tagged template literal
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbrand committed Sep 6, 2024
1 parent e9bd0e5 commit a9e3b23
Show file tree
Hide file tree
Showing 36 changed files with 3,691 additions and 3,756 deletions.
4 changes: 4 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ module.exports = function(config) {
{ pattern: "src/plugins/minimize/tests/minchats.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/actions.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/autocomplete.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/commands.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/component.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/corrections.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/csn.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/disco.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/emojis.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/hats.js", type: 'module' },
Expand All @@ -95,9 +97,11 @@ module.exports = function(config) {
{ pattern: "src/plugins/muc-views/tests/muc-messages.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/muc-registration.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/muc.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/mute.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/nickname.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/occupants-filter.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/occupants.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/probes.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/rai.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/retractions.js", type: 'module' },
{ pattern: "src/plugins/muc-views/tests/styling.js", type: 'module' },
Expand Down
19 changes: 10 additions & 9 deletions src/headless/plugins/muc/tests/muc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*global mock, converse */

const { Strophe, sizzle, u } = converse.env;
const { Strophe, sizzle, stx, u } = converse.env;

describe("Groupchats", function () {

Expand All @@ -13,18 +13,18 @@ describe("Groupchats", function () {
await mock.openAndEnterChatRoom(_converse, muc_jid, nick, [], [], false, {'hidden': true});
const model = _converse.chatboxes.get(muc_jid);

_converse.api.connection.get()._dataRecv(mock.createRequest(u.toStanza(`
_converse.api.connection.get()._dataRecv(mock.createRequest(stx`
<message xmlns="jabber:client" type="groupchat" id="1" to="${_converse.jid}" xml:lang="en" from="${muc_jid}/juliet">
<body>Romeo oh romeo</body>
</message>`)));
</message>`));
await u.waitUntil(() => model.messages.length);
expect(model.get('num_unread_general')).toBe(1);
expect(model.get('num_unread')).toBe(1);

_converse.api.connection.get()._dataRecv(mock.createRequest(u.toStanza(`
_converse.api.connection.get()._dataRecv(mock.createRequest(stx`
<message xmlns="jabber:client" type="groupchat" id="2" to="${_converse.jid}" xml:lang="en" from="${muc_jid}/juliet">
<body>Wherefore art though?</body>
</message>`)));
</message>`));

await u.waitUntil(() => model.messages.length === 2);

Expand Down Expand Up @@ -101,15 +101,15 @@ describe("Groupchats", function () {
expect(model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
model.sendMessage({'body': 'hello world'});

const stanza = u.toStanza(`
const stanza = stx`
<message xmlns='jabber:client'
from='${muc_jid}'
type='error'
to='${_converse.bare_jid}'>
<error type='cancel'>
<not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</message>`);
</message>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(stanza));

let sent_stanzas = _converse.api.connection.get().sent_stanzas;
Expand All @@ -119,15 +119,16 @@ describe("Groupchats", function () {
`<ping xmlns="urn:xmpp:ping"/>`+
`</iq>`);

const result = u.toStanza(`
const result = stx`
<iq from='${muc_jid}'
id='${iq.getAttribute('id')}'
to='${_converse.bare_jid}'
xmlns="jabber:server"
type='error'>
<error type='cancel'>
<not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</iq>`);
</iq>`;
sent_stanzas = _converse.api.connection.get().sent_stanzas;
const index = sent_stanzas.length -1;

Expand Down
122 changes: 67 additions & 55 deletions src/plugins/muc-views/tests/actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*global mock, converse */

const { $msg, u } = converse.env;
const { Strophe, u, stx } = converse.env;

describe("A Groupchat Message", function () {

Expand All @@ -9,16 +9,16 @@ describe("A Groupchat Message", function () {

const muc_jid = '[email protected]';
const model = await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const stanza = $pres({
to: '[email protected]/_converse.js-29092160',
from: '[email protected]/newguy'
})
.c('x', {xmlns: Strophe.NS.MUC_USER})
.c('item', {
'affiliation': 'none',
'jid': '[email protected]/_converse.js-290929789',
'role': 'participant'
}).tree();

const stanza = stx`
<presence
to="[email protected]/_converse.js-29092160"
from="[email protected]/newguy"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="[email protected]/_converse.js-290929789" role="participant"/>
</x>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(stanza));

const view = _converse.chatboxviews.get(muc_jid);
Expand All @@ -28,12 +28,15 @@ describe("A Groupchat Message", function () {

const firstMessageText = 'But soft, what light through yonder airlock breaks?';
const msg_id = u.getUniqueId();
await model.handleMessageStanza($msg({
'from': '[email protected]/newguy',
'to': _converse.api.connection.get().jid,
'type': 'groupchat',
'id': msg_id,
}).c('body').t(firstMessageText).tree());
await model.handleMessageStanza(stx`
<message
from="[email protected]/newguy"
to="${_converse.api.connection.get().jid}"
type="groupchat"
id="${msg_id}"
xmlns="jabber:client">
<body>${firstMessageText}</body>
</message>`);
await u.waitUntil(() => view.querySelectorAll('.chat-msg').length === 1);
let firstAction = view.querySelector('.chat-msg__action-copy');
expect(firstAction).not.toBeNull();
Expand Down Expand Up @@ -61,26 +64,28 @@ describe("A Groupchat Message", function () {

const muc_jid = '[email protected]';
const model = await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const stanza = $pres({
to: '[email protected]/_converse.js-29092160',
from: '[email protected]/newguy'
})
.c('x', {xmlns: Strophe.NS.MUC_USER})
.c('item', {
'affiliation': 'none',
'jid': '[email protected]/_converse.js-290929789',
'role': 'participant'
}).tree();
const stanza = stx`
<presence
to="[email protected]/_converse.js-29092160"
from="[email protected]/newguy"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="[email protected]/_converse.js-290929789" role="participant"/>
</x>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(stanza));

const firstMessageText = 'But soft, what light through yonder airlock breaks?';
const msg_id = u.getUniqueId();
await model.handleMessageStanza($msg({
'from': '[email protected]/newguy',
'to': _converse.api.connection.get().jid,
'type': 'groupchat',
'id': msg_id,
}).c('body').t(firstMessageText).tree());
await model.handleMessageStanza(stx`
<message
from="[email protected]/newguy"
to="${_converse.api.connection.get().jid}"
type="groupchat"
id="${msg_id}"
xmlns="jabber:client">
<body>${firstMessageText}</body>
</message>`);

const view = _converse.chatboxviews.get(muc_jid);
const textarea = await u.waitUntil(() => view.querySelector('textarea.chat-textarea'));
Expand All @@ -107,37 +112,44 @@ describe("A Groupchat Message", function () {
mock.initConverse([], {}, async function (_converse) {
const muc_jid = '[email protected]';
const model = await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo', ['muc_moderated']);
const stanza = $pres({
to: '[email protected]/_converse.js-29092160',
from: '[email protected]/newguy'
})
.c('x', {xmlns: Strophe.NS.MUC_USER})
.c('item', {
'affiliation': 'none',
'jid': '[email protected]/_converse.js-290929789',
'role': 'participant'
}).tree();
const stanza = stx`
<presence
to="[email protected]/_converse.js-29092160"
from="[email protected]/newguy"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="[email protected]/_converse.js-290929789" role="participant"/>
</x>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(stanza));

const view = _converse.chatboxviews.get(muc_jid);

const msg_id = u.getUniqueId();
await model.handleMessageStanza($msg({
'from': '[email protected]/newguy',
'to': _converse.api.connection.get().jid,
'type': 'groupchat',
'id': msg_id,
}).c('body').t('But soft, what light through yonder airlock breaks?').tree());
await model.handleMessageStanza(stx`
<message
from="[email protected]/newguy"
to="${_converse.api.connection.get().jid}"
type="groupchat"
id="${msg_id}"
xmlns="jabber:client">
<body>But soft, what light through yonder airlock breaks?</body>
</message>`);

await u.waitUntil(() => view.querySelectorAll('.chat-msg').length === 1);
// Quoting should be available before losing permission to speak
expect(view.querySelector('.chat-msg__action-quote')).not.toBeNull();

const presence = $pres({
to: '[email protected]/orchard',
from: `${muc_jid}/romeo`
}).c('x', {xmlns: Strophe.NS.MUC_USER})
.c('item', {'affiliation': 'none', 'role': 'visitor'}).up()
.c('status', {code: '110'});
const presence = stx`
<presence
to="[email protected]/orchard"
from="${muc_jid}/romeo"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" role="visitor"/>
</x>
<status code="110"/>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
const occupant = view.model.occupants.findWhere({'jid': _converse.bare_jid});
await u.waitUntil(() => occupant.get('role') === 'visitor');
Expand Down
Loading

0 comments on commit a9e3b23

Please sign in to comment.