/*jshint sub:true*/
/*eslint dot-notation: "off"*/
(function (root, factory) {
define([
"jasmine",
"jquery",
"converse-core",
"mock",
"test-utils",
"lodash"], factory);
} (this, function (jasmine, $, converse, mock, test_utils, _) {
"use strict";
var Strophe = converse.env.Strophe;
var $iq = converse.env.$iq;
var $pres = converse.env.$pres;
// See: https://xmpp.org/rfcs/rfc3921.html
describe("A sent presence stanza", function () {
it("has a given priority", mock.initConverse(function (_converse) {
var pres = _converse.xmppstatus.constructPresence('online', 'Hello world');
expect(pres.toLocaleString()).toBe(
""+
"Hello world"+
"0"+
""
);
_converse.priority = 2;
pres = _converse.xmppstatus.constructPresence('away', 'Going jogging');
expect(pres.toLocaleString()).toBe(
""+
"away"+
"Going jogging"+
"2"+
""
);
delete _converse.priority;
pres = _converse.xmppstatus.constructPresence('dnd', 'Doing taxes');
expect(pres.toLocaleString()).toBe(
""+
"dnd"+
"Doing taxes"+
"0"+
""
);
}));
});
describe("A received presence stanza", function () {
it("has its priority taken into account", mock.initConverse(function (_converse) {
test_utils.openControlBox();
test_utils.createContacts(_converse, 'current'); // Create some contacts so that we can test positioning
var contact_jid = mock.cur_names[8].replace(/ /g,'.').toLowerCase() + '@localhost';
var contact = _converse.roster.get(contact_jid);
var stanza = $(
''+
' 1'+
' '+
' '+
' ce51d94f7f22b87a21274abb93710b9eb7cc1c65'+
' '+
' '+
'');
_converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
expect(contact.get('chat_status')).toBe('online');
expect(_.keys(contact.get('resources')).length).toBe(1);
expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
stanza = $(
''+
' '+
' 0'+
' xa'+
' '+
' '+
'');
_converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('online');
expect(_.keys(contact.get('resources')).length).toBe(2);
expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
stanza = $(
''+
' 2'+
' dnd'+
'');
_converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('dnd');
expect(_.keys(contact.get('resources')).length).toBe(3);
expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
stanza = $(
''+
' 3'+
' away'+
'');
_converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('away');
expect(_.keys(contact.get('resources')).length).toBe(4);
expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
expect(contact.get('resources')['priority-3-resource']['priority']).toBe(3);
expect(contact.get('resources')['priority-3-resource']['status']).toBe('away');
stanza = $(
''+
' 1'+
' dnd'+
' '+
'');
_converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('away');
expect(_.keys(contact.get('resources')).length).toBe(5);
expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
expect(contact.get('resources')['priority-3-resource']['priority']).toBe(3);
expect(contact.get('resources')['priority-3-resource']['status']).toBe('away');
stanza = $(
''+
'');
_converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('dnd');
expect(_.keys(contact.get('resources')).length).toBe(4);
expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
stanza = $(
''+
'');
_converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('online');
expect(_.keys(contact.get('resources')).length).toBe(3);
expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
stanza = $(
''+
'');
_converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('dnd');
expect(_.keys(contact.get('resources')).length).toBe(2);
expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
stanza = $(
''+
'');
_converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('xa');
expect(_.keys(contact.get('resources')).length).toBe(1);
expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
stanza = $(
''+
'');
_converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('offline');
expect(_.keys(contact.get('resources')).length).toBe(0);
}));
});
}));