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.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /**
  15. * Mock Strophe connection.
  16. */
  17. export class MockStropheConnection extends Listenable {
  18. /**
  19. * A constructor...
  20. */
  21. constructor() {
  22. super();
  23. this.sentIQs = [];
  24. this._proto = {
  25. socket: undefined
  26. };
  27. }
  28. /**
  29. * XMPP service URL.
  30. *
  31. * @returns {string}
  32. */
  33. get service() {
  34. return 'wss://localhost/xmpp-websocket';
  35. }
  36. /**
  37. * {@see Strophe.Connection.connect}
  38. */
  39. connect(jid, pass, callback) {
  40. this._connectCb = callback;
  41. }
  42. /**
  43. * {@see Strophe.Connection.disconnect}
  44. */
  45. disconnect() {
  46. this.simulateConnectionState(Strophe.Status.DISCONNECTING);
  47. this.simulateConnectionState(Strophe.Status.DISCONNECTED);
  48. }
  49. /**
  50. * Simulates transition to the new connection status.
  51. *
  52. * @param {Strophe.Status} newState - The new connection status to set.
  53. * @returns {void}
  54. */
  55. simulateConnectionState(newState) {
  56. if (newState === Strophe.Status.CONNECTED) {
  57. this._proto.socket = {
  58. readyState: WebSocket.OPEN
  59. };
  60. } else {
  61. this._proto.socket = undefined;
  62. }
  63. this._connectCb(newState);
  64. }
  65. /**
  66. * {@see Strophe.Connection.sendIQ}.
  67. */
  68. sendIQ(iq, resultCb) {
  69. this.sentIQs.push(iq);
  70. resultCb && resultCb();
  71. }
  72. }
  73. /* eslint-enable no-empty-function */