dataconnection.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. describe('DataConnection', function() {
  2. // Only run tests on compatible browser.
  3. if (util.isBrowserCompatible()) {
  4. var dc, pdc;
  5. function DataChannelStub() {};
  6. DataChannelStub.prototype = {
  7. close: function() {
  8. if (this.readyState === 'closed') {
  9. throw Error();
  10. }
  11. this.readyState = 'closed';
  12. },
  13. // Only sends to peer's dc.
  14. send: function(msg) {
  15. pdc._handleDataMessage({ data: msg });
  16. }
  17. };
  18. describe('constructor', function() {
  19. it('should accept options properly', function() {
  20. // Test without 'new' keyword.
  21. dc = DataConnection('peer', null,
  22. { serialization: 'json',
  23. metadata: { message: 'I\'m testing!'} });
  24. expect(dc.peer).to.be('peer');
  25. expect(dc.serialization).to.be('json');
  26. expect(dc.metadata.message).to.be('I\'m testing!');
  27. expect(dc._dc).to.be(null);
  28. dc._dc = new DataChannelStub();
  29. });
  30. });
  31. it('inherits from EventEmitter');
  32. before(function() {
  33. dc = DataConnection('peer', null,
  34. { serialization: 'json',
  35. metadata: { message: 'I\'m testing!'} });
  36. dc._dc = new DataChannelStub();
  37. });
  38. describe('#_configureDataChannel', function() {
  39. it('should set the correct binaryType', function() {
  40. dc._configureDataChannel();
  41. if (util.browserisms === 'Firefox') {
  42. expect(dc._dc.binaryType).to.be('arraybuffer');
  43. } else {
  44. expect(dc._reliable).to.be(undefined);
  45. }
  46. });
  47. it('should fire an `open` event', function(done) {
  48. dc.on('open', function() {
  49. expect(dc.open).to.be(true)
  50. done();
  51. });
  52. dc._dc.onopen();
  53. });
  54. });
  55. describe('#_handleDataMessage', function() {
  56. });
  57. describe('#addDC', function() {
  58. it('should add a DataConnection properly', function() {
  59. pdc = new DataConnection('ignore', null, { serialization: 'json', reliable: true });
  60. pdc.addDC(new DataChannelStub());
  61. expect(pdc._dc).not.to.be(null);
  62. });
  63. });
  64. describe('#send', function() {
  65. it('should send data to the peer', function(done) {
  66. pdc = new DataConnection('ignore', null, { serialization: 'json' });
  67. pdc.on('data', function(data) {
  68. expect(data.hello).to.be('peer-tester');
  69. done();
  70. });
  71. dc.send({ hello: 'peer-tester' });
  72. });
  73. });
  74. describe('#_cleanup', function() {
  75. it('should emit a `close` event', function(done) {
  76. var first = true;
  77. dc.on('close', function() {
  78. expect(dc.open).to.be(false)
  79. // Calling it twice should be fine.
  80. if (first) {
  81. first = false;
  82. dc._cleanup();
  83. }
  84. done();
  85. });
  86. dc._cleanup();
  87. });
  88. });
  89. // Hacks hacks
  90. describe('#close', function() {
  91. it('should not call _cleanup', function() {
  92. dc._cleanup = function() {
  93. throw Error();
  94. }
  95. // Should not call _cleanup again.
  96. dc.close();
  97. });
  98. });
  99. } else {
  100. it('should not work.', function() {});
  101. }
  102. });