您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

XmppConnection.spec.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { default as XmppConnection } from './XmppConnection';
  2. import { $iq, Strophe } from 'strophe.js';
  3. import { nextTick } from '../util/TestUtils';
  4. /**
  5. * Mock Strophe connection.
  6. */
  7. class MockStropheConnection {
  8. /**
  9. * XMPP service URL.
  10. *
  11. * @returns {string}
  12. */
  13. get service() {
  14. return 'wss://localhost/xmpp-websocket';
  15. }
  16. /**
  17. * {@see Strophe.Connection.connect}
  18. */
  19. connect(jid, pass, callback) {
  20. this._connectCb = callback;
  21. }
  22. /**
  23. * {@see Strophe.Connection.disconnect}
  24. */
  25. disconnect() {
  26. this.simulateConnectionState(Strophe.Status.DISCONNECTING);
  27. this.simulateConnectionState(Strophe.Status.DISCONNECTED);
  28. }
  29. /**
  30. * Simulates transition to the new connection status.
  31. *
  32. * @param {Strophe.Status} newState - The new connection status to set.
  33. * @returns {void}
  34. */
  35. simulateConnectionState(newState) {
  36. this._connectCb(newState);
  37. }
  38. /**
  39. * {@see Strophe.Connection.sendIQ}.
  40. */
  41. sendIQ(iq, resultCb) {
  42. resultCb();
  43. }
  44. }
  45. /**
  46. * Creates any IQ.
  47. * @returns {Element}
  48. */
  49. function testIQ() {
  50. return $iq({
  51. to: 'remoteJid',
  52. type: 'set'
  53. })
  54. .c('jingle', { xmlns: 'urn:xmpp:jingle:1',
  55. action: 'session-info',
  56. initiator: 'blabla',
  57. sid: '1234' })
  58. .up();
  59. }
  60. describe('XmppConnection', () => {
  61. let connection;
  62. let mockStropheConnection;
  63. let sendIQSpy;
  64. beforeEach(() => {
  65. jasmine.clock().install();
  66. spyOn(Strophe, 'Connection').and.callFake((...args) => {
  67. mockStropheConnection = new MockStropheConnection(...args);
  68. return mockStropheConnection;
  69. });
  70. connection = new XmppConnection({
  71. serviceUrl: 'wss://localhost/xmpp-websocket'
  72. });
  73. sendIQSpy = spyOn(mockStropheConnection, 'sendIQ').and.callThrough();
  74. // eslint-disable-next-line no-empty-function
  75. connection.connect('jid', undefined, () => { });
  76. });
  77. afterEach(() => {
  78. jasmine.clock().uninstall();
  79. });
  80. describe('sendIQ2', () => {
  81. it('will send the IQ immediately if connected', () => {
  82. mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTED);
  83. return connection.sendIQ2(testIQ(), { timeout: 15000 })
  84. .then(() => {
  85. expect(sendIQSpy).toHaveBeenCalled();
  86. });
  87. });
  88. it('will send the IQ on reconnect', () => {
  89. mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTING);
  90. let resolved = false;
  91. connection
  92. .sendIQ2(testIQ(), { timeout: 15000 })
  93. .then(() => {
  94. resolved = true;
  95. });
  96. jasmine.clock().tick(10000);
  97. return nextTick()
  98. .then(() => {
  99. expect(resolved).toBe(false);
  100. expect(sendIQSpy).not.toHaveBeenCalled();
  101. mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTED);
  102. return nextTick();
  103. })
  104. .then(() => {
  105. expect(resolved).toBe(true);
  106. expect(sendIQSpy).toHaveBeenCalled();
  107. });
  108. });
  109. it('will timeout the operation if not connected in time', () => {
  110. mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTING);
  111. let rejected = false, resolved = false;
  112. connection
  113. .sendIQ2(testIQ(), { timeout: 15000 })
  114. .then(() => {
  115. resolved = true;
  116. }, () => {
  117. rejected = true;
  118. });
  119. jasmine.clock().tick(10000);
  120. return nextTick()
  121. .then(() => {
  122. expect(sendIQSpy).not.toHaveBeenCalled();
  123. expect(resolved).toBe(false);
  124. expect(rejected).toBe(false);
  125. jasmine.clock().tick(10000);
  126. return nextTick();
  127. })
  128. .then(() => {
  129. expect(sendIQSpy).not.toHaveBeenCalled();
  130. expect(resolved).toBe(false);
  131. expect(rejected).toBe(true);
  132. });
  133. });
  134. it('will reject the promise on explicit disconnect', () => {
  135. mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTING);
  136. let rejected = false, resolved = false;
  137. connection
  138. .sendIQ2(testIQ(), { timeout: 15000 })
  139. .then(() => {
  140. resolved = true;
  141. }, error => {
  142. rejected = error;
  143. });
  144. jasmine.clock().tick(10000);
  145. return nextTick()
  146. .then(() => {
  147. expect(sendIQSpy).not.toHaveBeenCalled();
  148. expect(resolved).toBe(false);
  149. expect(rejected).toBe(false);
  150. connection.disconnect();
  151. return nextTick();
  152. })
  153. .then(() => {
  154. expect(sendIQSpy).not.toHaveBeenCalled();
  155. expect(resolved).toBe(false);
  156. expect(rejected).toEqual(new Error('disconnect'));
  157. });
  158. });
  159. });
  160. });