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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 extends Listenable {
  8. /**
  9. * {@link ChatRoom.addPresenceListener}.
  10. */
  11. addPresenceListener(): void {
  12. // no operation; intentionally left blank
  13. }
  14. }
  15. /**
  16. * Mock Strophe connection.
  17. */
  18. export interface IProto {
  19. socket?: WebSocket;
  20. }
  21. export class MockStropheConnection extends Listenable {
  22. private _connectCb?: (status: Strophe.Status) => void;
  23. private _proto: IProto;
  24. public sentIQs: any[];
  25. /**
  26. * A constructor...
  27. */
  28. constructor() {
  29. super();
  30. this.sentIQs = [];
  31. this._proto = {
  32. socket: undefined
  33. };
  34. }
  35. /**
  36. * XMPP service URL.
  37. *
  38. * @returns {string}
  39. */
  40. get service(): string {
  41. return 'wss://localhost/xmpp-websocket';
  42. }
  43. /**
  44. * {@see Strophe.Connection.connect}
  45. */
  46. connect(jid: string, pass: string, callback: (status: Strophe.Status) => void): void {
  47. this._connectCb = callback;
  48. }
  49. /**
  50. * {@see Strophe.Connection.disconnect}
  51. */
  52. disconnect(): void {
  53. this.simulateConnectionState(Strophe.Status.DISCONNECTING);
  54. this.simulateConnectionState(Strophe.Status.DISCONNECTED);
  55. }
  56. /**
  57. * Simulates transition to the new connection status.
  58. *
  59. * @param {Strophe.Status} newState - The new connection status to set.
  60. * @returns {void}
  61. */
  62. simulateConnectionState(newState: Strophe.Status): void {
  63. if (newState === Strophe.Status.CONNECTED) {
  64. this._proto.socket = { readyState: WebSocket.OPEN } as WebSocket;
  65. } else {
  66. this._proto.socket = undefined;
  67. }
  68. this._connectCb?.(newState);
  69. }
  70. /**
  71. * {@see Strophe.Connection.sendIQ}.
  72. */
  73. sendIQ(iq: any, resultCb?: () => void): void {
  74. this.sentIQs.push(iq);
  75. resultCb && resultCb();
  76. }
  77. /**
  78. * {@see Strophe.Connection.registerSASLMechanisms}.
  79. */
  80. registerSASLMechanisms(): void {
  81. // Intentionally left blank for mock functionality
  82. }
  83. }
  84. /* eslint-enable no-empty-function */