Permalink
Browse files

Implement Peer#getTransport

  • Loading branch information...
1 parent ac9ac75 commit 3f48d788624ab75a83dad66c3b361266ca10c3fb @jugglinmike jugglinmike committed Mar 26, 2013
Showing with 37 additions and 1 deletion.
  1. +7 −0 prototype/public/scripts/modules/peer.js
  2. +30 −1 prototype/test/client/tests/peer.js
@@ -22,6 +22,13 @@ define([
} else if (!this.nameRegex.test(attrs.name)) {
return new Error('Invalid username');
}
+ },
+ // getTransport
+ // Return a reference to the model's transport. If the model does not
+ // define a transport, return a reference to its collection's transport (if
+ // available).
+ getTransport: function() {
+ return this.transport || this.collection && this.collection.transport;
}
});
@@ -1,4 +1,4 @@
-define(['modules/peer'], function(Peer) {
+define(['modules/peer', 'backbone'], function(Peer, Backbone) {
'use strict';
suite('Peer', function() {
@@ -27,6 +27,35 @@ define(['modules/peer'], function(Peer) {
assert.instanceOf(peer.validate({ name: '!@#@#$' }), Error);
});
});
+ suite('#getTransport', function() {
+ test('Returns `undefined` when no transport is found', function() {
+ var peer = new Peer.Model();
+ assert.isUndefined(peer.getTransport());
+ });
+ test('Returns a reference to its transport object when available', function() {
+ var peer = new Peer.Model();
+ var transport = peer.transport = {};
+ assert.equal(peer.getTransport(), transport);
+ });
+ test('Returns a reference to its transport object when ' +
+ 'part of a collection that defines a transport', function() {
+ var coll = new Backbone.Collection();
+ var peer = new Peer.Model();
+ var transport = peer.transport = {};
+ coll.transport = {};
+ coll.add(peer);
+ assert.equal(peer.getTransport(), transport);
+ });
+ test('Returns a reference to its collection\'s transport object ' +
+ 'when it does not define one, but is a memeber of a collection ' +
+ 'that does', function() {
+ var coll = new Backbone.Collection();
+ var peer = new Peer.Model();
+ coll.transport = {};
+ coll.add(peer);
+ assert.equal(peer.getTransport(), coll.transport);
+ });
+ });
});
});
});

0 comments on commit 3f48d78

Please sign in to comment.