Commit 14c09c50 authored by JC Brand's avatar JC Brand

Add some consistency to how errors are thrown.

Add tests for contacts.add API method and make it a bit more compact.
Add entry in changelog.

update #354
parent 90641a61
...@@ -74,7 +74,7 @@ ...@@ -74,7 +74,7 @@
} else if (typeof attr === 'string') { } else if (typeof attr === 'string') {
return item.get(attr).toLowerCase().indexOf(query.toLowerCase()) !== -1; return item.get(attr).toLowerCase().indexOf(query.toLowerCase()) !== -1;
} else { } else {
throw new Error('Wrong attribute type. Must be string or array.'); throw new TypeError('contains: wrong attribute type. Must be string or array.');
} }
}; };
}; };
...@@ -848,7 +848,7 @@ ...@@ -848,7 +848,7 @@
} }
break; break;
default: default:
throw new Error('Unknown type.'); throw new TypeError('ChatBox.onSMP: Unknown type for SMP');
} }
}, },
...@@ -5321,14 +5321,14 @@ ...@@ -5321,14 +5321,14 @@
this.onConnected(); this.onConnected();
} else { } else {
if (!this.bosh_service_url && ! this.websocket_url) { if (!this.bosh_service_url && ! this.websocket_url) {
throw("Error: you must supply a value for the bosh_service_url or websocket_url"); throw new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
} }
if (('WebSocket' in window || 'MozWebSocket' in window) && this.websocket_url) { if (('WebSocket' in window || 'MozWebSocket' in window) && this.websocket_url) {
this.connection = new Strophe.Connection(this.websocket_url); this.connection = new Strophe.Connection(this.websocket_url);
} else if (this.bosh_service_url) { } else if (this.bosh_service_url) {
this.connection = new Strophe.Connection(this.bosh_service_url); this.connection = new Strophe.Connection(this.bosh_service_url);
} else { } else {
throw("Error: this browser does not support websockets and no bosh_service_url specified."); throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
} }
this.setUpXMLLogging(); this.setUpXMLLogging();
...@@ -5338,7 +5338,7 @@ ...@@ -5338,7 +5338,7 @@
jid = this.session.get('jid'); jid = this.session.get('jid');
if (this.prebind) { if (this.prebind) {
if (!this.jid) { if (!this.jid) {
throw("When using 'keepalive' with 'prebind, you must supply the JID of the current user."); throw new Error("initConnection: when using 'keepalive' with 'prebind, you must supply the JID of the current user.");
} }
if (rid && sid && jid && Strophe.getBareJidFromJid(jid) === Strophe.getBareJidFromJid(this.jid)) { if (rid && sid && jid && Strophe.getBareJidFromJid(jid) === Strophe.getBareJidFromJid(this.jid)) {
this.session.save({rid: rid}); // The RID needs to be increased with each request. this.session.save({rid: rid}); // The RID needs to be increased with each request.
...@@ -5362,7 +5362,7 @@ ...@@ -5362,7 +5362,7 @@
if (this.jid && this.sid && this.rid) { if (this.jid && this.sid && this.rid) {
this.connection.attach(this.jid, this.sid, this.rid, this.onConnect); this.connection.attach(this.jid, this.sid, this.rid, this.onConnect);
} else { } else {
throw("If you use prebind and don't use keepalive, "+ throw new Error("initConnection: If you use prebind and not keepalive, "+
"then you MUST supply JID, RID and SID values"); "then you MUST supply JID, RID and SID values");
} }
} }
...@@ -5484,17 +5484,10 @@ ...@@ -5484,17 +5484,10 @@
return _.map(jids, _transform); return _.map(jids, _transform);
}, },
'add': function (jid, name) { 'add': function (jid, name) {
if (typeof jid === "undefined") { if (typeof jid !== "string" || jid.indexOf('@') < 0) {
throw new Error("Error: you must supply a jid"); throw new TypeError('contacts.add: invalid jid');
} }
if (typeof jid !== "string") { converse.connection.roster.add(jid, _.isEmpty(name)? jid: name, [], function (iq) {
throw new Error('Error: wrong attribute (jid) type. Must be string.');
}
if (jid.indexOf('@') < 0) {
throw new Error('Error: invalid jid ');
}
name = _.isEmpty(name)? jid: name;
converse.connection.roster.add(jid, name, [], function (iq) {
converse.connection.roster.subscribe(jid, null, converse.xmppstatus.get('fullname')); converse.connection.roster.subscribe(jid, null, converse.xmppstatus.get('fullname'));
}); });
return true; return true;
......
Changelog Changelog
========= =========
0.9.2 (Undefined)
-----------------
* Bugfix. Prevent attaching twice during initialization. [jcbrand]
* Add API method contacts.add. [pzia]
0.9.1 (2015-03-26) 0.9.1 (2015-03-26)
------------------ ------------------
......
...@@ -57,26 +57,32 @@ ...@@ -57,26 +57,32 @@
it("has a method 'get' which returns wrapped contacts", $.proxy(function () { it("has a method 'get' which returns wrapped contacts", $.proxy(function () {
// Check that it returns nothing if a non-existing JID is given // Check that it returns nothing if a non-existing JID is given
expect(converse_api.contacts.get('non-existing@jabber.org')).toBeFalsy(); expect(converse_api.contacts.get('non-existing@jabber.org')).toBeFalsy();
// Check when a single jid is given // Check when a single jid is given
var jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
var attrs = converse_api.contacts.get(jid); var attrs = converse_api.contacts.get(jid);
expect(typeof attrs).toBe('object'); expect(typeof attrs).toBe('object');
expect(attrs.fullname).toBe(mock.cur_names[0]); expect(attrs.fullname).toBe(mock.cur_names[0]);
expect(attrs.jid).toBe(jid); expect(attrs.jid).toBe(jid);
// You can retrieve multiple contacts by passing in an array // You can retrieve multiple contacts by passing in an array
var jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost'; var jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
var list = converse_api.contacts.get([jid, jid2]); var list = converse_api.contacts.get([jid, jid2]);
expect(Array.isArray(list)).toBeTruthy(); expect(Array.isArray(list)).toBeTruthy();
expect(list[0].fullname).toBe(mock.cur_names[0]); expect(list[0].fullname).toBe(mock.cur_names[0]);
expect(list[1].fullname).toBe(mock.cur_names[1]); expect(list[1].fullname).toBe(mock.cur_names[1]);
// Check that all JIDs are returned if you call without any parameters // Check that all JIDs are returned if you call without any parameters
list = converse_api.contacts.get(); list = converse_api.contacts.get();
expect(list.length).toBe(mock.cur_names.length); expect(list.length).toBe(mock.cur_names.length);
}, converse)); }, converse));
it("has a method 'add' with which contacts can be added", $.proxy(function () {
var error = new TypeError('contacts.add: invalid jid');
expect(converse_api.contacts.add).toThrow(error);
expect(converse_api.contacts.add.bind(converse_api, "invalid jid")).toThrow(error);
spyOn(converse.connection.roster, 'add');
converse_api.contacts.add("newcontact@example.org");
expect(converse.connection.roster.add).toHaveBeenCalled();
}, converse));
}, converse)); }, converse));
describe("The \"chats\" API", $.proxy(function() { describe("The \"chats\" API", $.proxy(function() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment