You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XmppConnection.spec.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { $iq, Strophe } from 'strophe.js';
  2. import { nextTick } from '../util/TestUtils';
  3. import { MockStropheConnection } from './MockClasses';
  4. import { default as XmppConnection } from './XmppConnection';
  5. /**
  6. * Creates any IQ.
  7. * @returns {Element}
  8. */
  9. function testIQ() {
  10. return $iq({
  11. to: 'remoteJid',
  12. type: 'set'
  13. })
  14. .c('jingle', { xmlns: 'urn:xmpp:jingle:1',
  15. action: 'session-info',
  16. initiator: 'blabla',
  17. sid: '1234' })
  18. .up();
  19. }
  20. describe('XmppConnection', () => {
  21. let connection;
  22. let mockStropheConnection;
  23. let sendIQSpy;
  24. beforeEach(() => {
  25. jasmine.clock().install();
  26. spyOn(Strophe, 'Connection').and.callFake((...args) => {
  27. mockStropheConnection = new MockStropheConnection(...args);
  28. return mockStropheConnection;
  29. });
  30. connection = new XmppConnection({
  31. serviceUrl: 'wss://localhost/xmpp-websocket'
  32. });
  33. sendIQSpy = spyOn(mockStropheConnection, 'sendIQ').and.callThrough();
  34. // eslint-disable-next-line no-empty-function
  35. connection.connect('jid', undefined, () => { });
  36. });
  37. afterEach(() => {
  38. jasmine.clock().uninstall();
  39. });
  40. describe('sendIQ2', () => {
  41. it('will send the IQ immediately if connected', () => {
  42. mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTED);
  43. return connection.sendIQ2(testIQ(), { timeout: 15000 })
  44. .then(() => {
  45. expect(sendIQSpy).toHaveBeenCalled();
  46. });
  47. });
  48. it('will send the IQ on reconnect', () => {
  49. mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTING);
  50. let resolved = false;
  51. connection
  52. .sendIQ2(testIQ(), { timeout: 15000 })
  53. .then(() => {
  54. resolved = true;
  55. });
  56. jasmine.clock().tick(10000);
  57. return nextTick()
  58. .then(() => {
  59. expect(resolved).toBe(false);
  60. expect(sendIQSpy).not.toHaveBeenCalled();
  61. mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTED);
  62. return nextTick();
  63. })
  64. .then(() => {
  65. expect(resolved).toBe(true);
  66. expect(sendIQSpy).toHaveBeenCalled();
  67. });
  68. });
  69. it('will timeout the operation if not connected in time', () => {
  70. mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTING);
  71. let rejected = false, resolved = false;
  72. connection
  73. .sendIQ2(testIQ(), { timeout: 15000 })
  74. .then(() => {
  75. resolved = true;
  76. }, () => {
  77. rejected = true;
  78. });
  79. jasmine.clock().tick(10000);
  80. return nextTick()
  81. .then(() => {
  82. expect(sendIQSpy).not.toHaveBeenCalled();
  83. expect(resolved).toBe(false);
  84. expect(rejected).toBe(false);
  85. jasmine.clock().tick(10000);
  86. return nextTick();
  87. })
  88. .then(() => {
  89. expect(sendIQSpy).not.toHaveBeenCalled();
  90. expect(resolved).toBe(false);
  91. expect(rejected).toBe(true);
  92. });
  93. });
  94. it('will reject the promise on explicit disconnect', () => {
  95. mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTING);
  96. let rejected = false, resolved = false;
  97. connection
  98. .sendIQ2(testIQ(), { timeout: 15000 })
  99. .then(() => {
  100. resolved = true;
  101. }, error => {
  102. rejected = error;
  103. });
  104. jasmine.clock().tick(10000);
  105. return nextTick()
  106. .then(() => {
  107. expect(sendIQSpy).not.toHaveBeenCalled();
  108. expect(resolved).toBe(false);
  109. expect(rejected).toBe(false);
  110. connection.disconnect();
  111. return nextTick();
  112. })
  113. .then(() => {
  114. expect(sendIQSpy).not.toHaveBeenCalled();
  115. expect(resolved).toBe(false);
  116. expect(rejected).toEqual(new Error('disconnect'));
  117. });
  118. });
  119. });
  120. });