dataconnection.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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._dc.onmessage({ data: msg });
  16. }
  17. };
  18. it('constructor', function() {
  19. // Test without 'new' keyword.
  20. dc = DataConnection('peer', null,
  21. { serialization: 'json',
  22. metadata: { message: 'I\'m testing!'},
  23. reliable: true });
  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. it('inherits from EventEmitter');
  31. it('_configureDataChannel', function() {
  32. dc._configureDataChannel();
  33. if (util.browserisms === 'Firefox') {
  34. expect(dc._dc.binaryType).to.be('arraybuffer');
  35. } else {
  36. expect(dc._reliable).not.to.be(undefined);
  37. }
  38. });
  39. it('should fire an `open` event', function(done) {
  40. dc.on('open', function() {
  41. expect(dc.open).to.be(true)
  42. done();
  43. });
  44. dc._dc.onopen();
  45. });
  46. it('_handleDataMessage', function() {
  47. });
  48. it('addDC', function() {
  49. pdc = new DataConnection('ignore', null, { serialization: 'json', reliable: true });
  50. pdc.addDC(new DataChannelStub());
  51. expect(pdc._dc).not.to.be(null);
  52. });
  53. it('send', function(done) {
  54. pdc.on('data', function(data) {
  55. expect(data.hello).to.be('peer-tester');
  56. done();
  57. });
  58. dc.send({ hello: 'peer-tester' });
  59. });
  60. it('_cleanup', function(done) {
  61. var first = true;
  62. dc.on('close', function() {
  63. expect(dc.open).to.be(false)
  64. // Calling it twice should be fine.
  65. if (first) {
  66. first = false;
  67. dc._cleanup();
  68. }
  69. done();
  70. });
  71. dc._cleanup();
  72. });
  73. it('close', function() {
  74. dc._cleanup = function() {
  75. throw Error();
  76. }
  77. // Should not call _cleanup again.
  78. dc.close();
  79. });
  80. } else {
  81. it('should not work.', function() {});
  82. }
  83. });