/*global mock, converse */ const { u } = converse.env; describe("A Groupchat Message", function () { it("will render an unfurl based on OGP data", mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) { const nick = 'romeo'; const muc_jid = 'lounge@montague.lit'; await mock.openAndEnterChatRoom(_converse, muc_jid, nick); const view = _converse.api.chatviews.get(muc_jid); const message_stanza = u.toStanza(` https://www.youtube.com/watch?v=dQw4w9WgXcQ `); _converse.connection._dataRecv(mock.createRequest(message_stanza)); const el = await u.waitUntil(() => view.querySelector('.chat-msg__text')); expect(el.textContent).toBe('https://www.youtube.com/watch?v=dQw4w9WgXcQ'); const metadata_stanza = u.toStanza(` `); _converse.connection._dataRecv(mock.createRequest(metadata_stanza)); const unfurl = await u.waitUntil(() => view.querySelector('converse-message-unfurl')); expect(unfurl.querySelector('.card-img-top').getAttribute('src')).toBe('https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg'); done(); })); it("will render multiple unfurls based on OGP data", mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) { const nick = 'romeo'; const muc_jid = 'lounge@montague.lit'; await mock.openAndEnterChatRoom(_converse, muc_jid, nick); const view = _converse.api.chatviews.get(muc_jid); const message_stanza = u.toStanza(` Check out https://www.youtube.com/watch?v=dQw4w9WgXcQ and https://duckduckgo.com `); _converse.connection._dataRecv(mock.createRequest(message_stanza)); const el = await u.waitUntil(() => view.querySelector('.chat-msg__text')); expect(el.textContent).toBe('Check out https://www.youtube.com/watch?v=dQw4w9WgXcQ and https://duckduckgo.com'); let metadata_stanza = u.toStanza(` `); _converse.connection._dataRecv(mock.createRequest(metadata_stanza)); await u.waitUntil(() => view.querySelectorAll('converse-message-unfurl').length === 1); metadata_stanza = u.toStanza(` `); _converse.connection._dataRecv(mock.createRequest(metadata_stanza)); await u.waitUntil(() => view.querySelectorAll('converse-message-unfurl').length === 2); done(); })); it("will not render an unfurl received from a MUC participant", mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) { const nick = 'romeo'; const muc_jid = 'lounge@montague.lit'; await mock.openAndEnterChatRoom(_converse, muc_jid, nick); const view = _converse.api.chatviews.get(muc_jid); const message_stanza = u.toStanza(` https://www.youtube.com/watch?v=dQw4w9WgXcQ `); _converse.connection._dataRecv(mock.createRequest(message_stanza)); const el = await u.waitUntil(() => view.querySelector('.chat-msg__text')); expect(el.textContent).toBe('https://www.youtube.com/watch?v=dQw4w9WgXcQ'); spyOn(view.model, 'handleMetadataFastening').and.callThrough(); const metadata_stanza = u.toStanza(` `); _converse.connection._dataRecv(mock.createRequest(metadata_stanza)); await u.waitUntil(() => view.model.handleMetadataFastening.calls.count()); expect(view.model.handleMetadataFastening.calls.first().returnValue).toBe(false); expect(view.querySelector('converse-message-unfurl')).toBe(null); done(); })); it("will not render an unfurl based on OGP data if muc_show_ogp_unfurls is false", mock.initConverse(['chatBoxesFetched'], {'muc_show_ogp_unfurls': false}, async function (done, _converse) { const nick = 'romeo'; const muc_jid = 'lounge@montague.lit'; await mock.openAndEnterChatRoom(_converse, muc_jid, nick); const view = _converse.api.chatviews.get(muc_jid); const message_stanza = u.toStanza(` https://www.youtube.com/watch?v=dQw4w9WgXcQ `); _converse.connection._dataRecv(mock.createRequest(message_stanza)); const el = await u.waitUntil(() => view.querySelector('.chat-msg__text')); expect(el.textContent).toBe('https://www.youtube.com/watch?v=dQw4w9WgXcQ'); spyOn(view.model, 'handleMetadataFastening').and.callThrough(); const metadata_stanza = u.toStanza(` `); _converse.connection._dataRecv(mock.createRequest(metadata_stanza)); await u.waitUntil(() => view.model.handleMetadataFastening.calls.count()); expect(view.model.handleMetadataFastening.calls.first().returnValue).toBe(false); expect(view.querySelector('converse-message-unfurl')).toBe(null); done(); })); it("will only render a single unfurl when receiving the same OGP data multiple times", mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) { const nick = 'romeo'; const muc_jid = 'lounge@montague.lit'; await mock.openAndEnterChatRoom(_converse, muc_jid, nick); const view = _converse.api.chatviews.get(muc_jid); const message_stanza = u.toStanza(` https://www.youtube.com/watch?v=dQw4w9WgXcQ `); _converse.connection._dataRecv(mock.createRequest(message_stanza)); const el = await u.waitUntil(() => view.querySelector('.chat-msg__text')); expect(el.textContent).toBe('https://www.youtube.com/watch?v=dQw4w9WgXcQ'); spyOn(view.model, 'handleMetadataFastening').and.callThrough(); const metadata_stanza = u.toStanza(` `); _converse.connection._dataRecv(mock.createRequest(metadata_stanza)); _converse.connection._dataRecv(mock.createRequest(metadata_stanza)); _converse.connection._dataRecv(mock.createRequest(metadata_stanza)); await u.waitUntil(() => view.model.handleMetadataFastening.calls.count()); const unfurls = await u.waitUntil(() => view.querySelectorAll('converse-message-unfurl')); expect(unfurls.length).toBe(1); done(); })); it("will not render an unfurl image if the domain is not in show_images_inline", mock.initConverse(['chatBoxesFetched'], {'show_images_inline': []}, async function (done, _converse) { const nick = 'romeo'; const muc_jid = 'lounge@montague.lit'; await mock.openAndEnterChatRoom(_converse, muc_jid, nick); const view = _converse.api.chatviews.get(muc_jid); const message_stanza = u.toStanza(` https://www.youtube.com/watch?v=dQw4w9WgXcQ `); _converse.connection._dataRecv(mock.createRequest(message_stanza)); const el = await u.waitUntil(() => view.querySelector('.chat-msg__text')); expect(el.textContent).toBe('https://www.youtube.com/watch?v=dQw4w9WgXcQ'); const metadata_stanza = u.toStanza(` `); _converse.connection._dataRecv(mock.createRequest(metadata_stanza)); const unfurl = await u.waitUntil(() => view.querySelector('converse-message-unfurl')); expect(unfurl.querySelector('.card-img-top')).toBe(null); done(); })); });