|
| 1 | +/* global converse */ |
| 2 | +import mock from '../../../tests/mock.js'; |
| 3 | + |
| 4 | +const { stx, Strophe } = converse.env; |
| 5 | + |
| 6 | +describe('pubsub subscribe/unsubscribe API', function () { |
| 7 | + beforeAll(() => jasmine.addMatchers({ toEqualStanza: jasmine.toEqualStanza })); |
| 8 | + |
| 9 | + it( |
| 10 | + 'sends correct IQ for subscribe', |
| 11 | + mock.initConverse([], {}, async function (_converse) { |
| 12 | + await mock.waitForRoster(_converse, 'current', 0); |
| 13 | + const { api } = _converse; |
| 14 | + const sent = api.connection.get().sent_stanzas; |
| 15 | + const service = 'pubsub.example.org'; |
| 16 | + const node = 'testnode'; |
| 17 | + const subscribePromise = api.pubsub.subscribe(service, node); |
| 18 | + |
| 19 | + const stanza = sent.filter((iq) => iq.querySelector('pubsub subscribe')).pop(); |
| 20 | + expect(stanza).toEqualStanza(stx` |
| 21 | + <iq type="set" |
| 22 | + from="${_converse.bare_jid}" |
| 23 | + to="${service}" |
| 24 | + xmlns="jabber:client" |
| 25 | + id="${stanza.getAttribute('id')}"> |
| 26 | + <pubsub xmlns="${Strophe.NS.PUBSUB}"> |
| 27 | + <subscribe node="${node}" jid="${_converse.bare_jid}"/> |
| 28 | + </pubsub> |
| 29 | + </iq>`); |
| 30 | + |
| 31 | + _converse.api.connection.get()._dataRecv( |
| 32 | + mock.createRequest(stx` |
| 33 | + <iq type="result" |
| 34 | + xmlns="jabber:client" |
| 35 | + from="${service}" |
| 36 | + to="${_converse.bare_jid}" |
| 37 | + id="${stanza.getAttribute('id')}"/> |
| 38 | + `) |
| 39 | + ); |
| 40 | + await subscribePromise; |
| 41 | + }) |
| 42 | + ); |
| 43 | + |
| 44 | + it( |
| 45 | + 'sends correct IQ for unsubscribe', |
| 46 | + mock.initConverse([], {}, async function (_converse) { |
| 47 | + await mock.waitForRoster(_converse, 'current', 0); |
| 48 | + const { api } = _converse; |
| 49 | + const sent = api.connection.get().sent_stanzas; |
| 50 | + const service = 'pubsub.example.org'; |
| 51 | + const node = 'testnode'; |
| 52 | + const unsubscribePromise = api.pubsub.unsubscribe(service, node); |
| 53 | + const stanza = sent.filter((iq) => iq.querySelector('pubsub unsubscribe')).pop(); |
| 54 | + _converse.api.connection.get()._dataRecv( |
| 55 | + mock.createRequest(stx` |
| 56 | + <iq type="result" |
| 57 | + xmlns="jabber:client" |
| 58 | + from="${service}" |
| 59 | + to="${_converse.bare_jid}" |
| 60 | + id="${stanza.getAttribute('id')}"/> |
| 61 | + `) |
| 62 | + ); |
| 63 | + await unsubscribePromise; |
| 64 | + expect(stanza).toEqualStanza(stx` |
| 65 | + <iq type="set" |
| 66 | + from="${_converse.bare_jid}" |
| 67 | + to="${service}" |
| 68 | + xmlns="jabber:client" |
| 69 | + id="${stanza.getAttribute('id')}"> |
| 70 | + <pubsub xmlns="${Strophe.NS.PUBSUB}"> |
| 71 | + <unsubscribe node="${node}" jid="${_converse.bare_jid}"/> |
| 72 | + </pubsub> |
| 73 | + </iq>`); |
| 74 | + }) |
| 75 | + ); |
| 76 | +}); |
0 commit comments