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.

MockClasses.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Strophe } from 'strophe.js';
  2. import Listenable from '../util/Listenable';
  3. /* eslint-disable no-empty-function */
  4. /**
  5. * Mock {@link ChatRoom}.
  6. */
  7. export class MockChatRoom {
  8. /**
  9. * {@link ChatRoom.addPresenceListener}.
  10. */
  11. addPresenceListener() {
  12. }
  13. /**
  14. * {@link ChatRoom.sendCodecInfoPresence}.
  15. */
  16. sendCodecInfoPresence() {
  17. }
  18. }
  19. /**
  20. * Mock Strophe connection.
  21. */
  22. export class MockStropheConnection extends Listenable {
  23. /**
  24. * A constructor...
  25. */
  26. constructor() {
  27. super();
  28. this.sentIQs = [];
  29. this._proto = {
  30. socket: undefined
  31. };
  32. }
  33. /**
  34. * XMPP service URL.
  35. *
  36. * @returns {string}
  37. */
  38. get service() {
  39. return 'wss://localhost/xmpp-websocket';
  40. }
  41. /**
  42. * {@see Strophe.Connection.connect}
  43. */
  44. connect(jid, pass, callback) {
  45. this._connectCb = callback;
  46. }
  47. /**
  48. * {@see Strophe.Connection.disconnect}
  49. */
  50. disconnect() {
  51. this.simulateConnectionState(Strophe.Status.DISCONNECTING);
  52. this.simulateConnectionState(Strophe.Status.DISCONNECTED);
  53. }
  54. /**
  55. * Simulates transition to the new connection status.
  56. *
  57. * @param {Strophe.Status} newState - The new connection status to set.
  58. * @returns {void}
  59. */
  60. simulateConnectionState(newState) {
  61. if (newState === Strophe.Status.CONNECTED) {
  62. this._proto.socket = {
  63. readyState: WebSocket.OPEN
  64. };
  65. } else {
  66. this._proto.socket = undefined;
  67. }
  68. this._connectCb(newState);
  69. }
  70. /**
  71. * {@see Strophe.Connection.sendIQ}.
  72. */
  73. sendIQ(iq, resultCb) {
  74. this.sentIQs.push(iq);
  75. resultCb && resultCb();
  76. }
  77. }
  78. /* eslint-enable no-empty-function */