Socket.test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. const UppySocket = require('./Socket')
  2. describe('Socket', () => {
  3. let webSocketConstructorSpy
  4. let webSocketCloseSpy
  5. let webSocketSendSpy
  6. beforeEach(() => {
  7. webSocketConstructorSpy = jest.fn()
  8. webSocketCloseSpy = jest.fn()
  9. webSocketSendSpy = jest.fn()
  10. global.WebSocket = class WebSocket {
  11. constructor (target) {
  12. webSocketConstructorSpy(target)
  13. }
  14. // eslint-disable-next-line class-methods-use-this
  15. close (args) {
  16. webSocketCloseSpy(args)
  17. }
  18. // eslint-disable-next-line class-methods-use-this
  19. send (json) {
  20. webSocketSendSpy(json)
  21. }
  22. triggerOpen () {
  23. this.onopen()
  24. }
  25. triggerClose () {
  26. this.onclose()
  27. }
  28. }
  29. })
  30. afterEach(() => {
  31. global.WebSocket = undefined
  32. })
  33. it('should expose a class', () => {
  34. expect(UppySocket.name).toEqual('UppySocket')
  35. expect(
  36. new UppySocket({
  37. target: 'foo',
  38. }) instanceof UppySocket
  39. )
  40. })
  41. it('should setup a new WebSocket', () => {
  42. new UppySocket({ target: 'foo' }) // eslint-disable-line no-new
  43. expect(webSocketConstructorSpy.mock.calls[0][0]).toEqual('foo')
  44. })
  45. it('should send a message via the websocket if the connection is open', () => {
  46. const uppySocket = new UppySocket({ target: 'foo' })
  47. const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
  48. webSocketInstance.triggerOpen()
  49. uppySocket.send('bar', 'boo')
  50. expect(webSocketSendSpy.mock.calls.length).toEqual(1)
  51. expect(webSocketSendSpy.mock.calls[0]).toEqual([
  52. JSON.stringify({ action: 'bar', payload: 'boo' }),
  53. ])
  54. })
  55. it('should queue the message for the websocket if the connection is not open', () => {
  56. const uppySocket = new UppySocket({ target: 'foo' })
  57. uppySocket.send('bar', 'boo')
  58. expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([{ action: 'bar', payload: 'boo' }])
  59. expect(webSocketSendSpy.mock.calls.length).toEqual(0)
  60. })
  61. it('should queue any messages for the websocket if the connection is not open, then send them when the connection is open', () => {
  62. const uppySocket = new UppySocket({ target: 'foo' })
  63. const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
  64. uppySocket.send('bar', 'boo')
  65. uppySocket.send('moo', 'baa')
  66. expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([
  67. { action: 'bar', payload: 'boo' },
  68. { action: 'moo', payload: 'baa' },
  69. ])
  70. expect(webSocketSendSpy.mock.calls.length).toEqual(0)
  71. webSocketInstance.triggerOpen()
  72. expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([])
  73. expect(webSocketSendSpy.mock.calls.length).toEqual(2)
  74. expect(webSocketSendSpy.mock.calls[0]).toEqual([
  75. JSON.stringify({ action: 'bar', payload: 'boo' }),
  76. ])
  77. expect(webSocketSendSpy.mock.calls[1]).toEqual([
  78. JSON.stringify({ action: 'moo', payload: 'baa' }),
  79. ])
  80. })
  81. it('should start queuing any messages when the websocket connection is closed', () => {
  82. const uppySocket = new UppySocket({ target: 'foo' })
  83. const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
  84. webSocketInstance.triggerOpen()
  85. uppySocket.send('bar', 'boo')
  86. expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([])
  87. webSocketInstance.triggerClose()
  88. uppySocket.send('bar', 'boo')
  89. expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([{ action: 'bar', payload: 'boo' }])
  90. })
  91. it('should close the websocket when it is force closed', () => {
  92. const uppySocket = new UppySocket({ target: 'foo' })
  93. const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
  94. webSocketInstance.triggerOpen()
  95. uppySocket.close()
  96. expect(webSocketCloseSpy.mock.calls.length).toEqual(1)
  97. })
  98. it('should be able to subscribe to messages received on the websocket', () => {
  99. const uppySocket = new UppySocket({ target: 'foo' })
  100. const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
  101. const emitterListenerMock = jest.fn()
  102. uppySocket.on('hi', emitterListenerMock)
  103. webSocketInstance.triggerOpen()
  104. webSocketInstance.onmessage({
  105. data: JSON.stringify({ action: 'hi', payload: 'ho' }),
  106. })
  107. expect(emitterListenerMock.mock.calls).toEqual([
  108. ['ho', undefined, undefined, undefined, undefined, undefined],
  109. ])
  110. })
  111. it('should be able to emit messages and subscribe to them', () => {
  112. const uppySocket = new UppySocket({ target: 'foo' })
  113. const emitterListenerMock = jest.fn()
  114. uppySocket.on('hi', emitterListenerMock)
  115. uppySocket.emit('hi', 'ho')
  116. uppySocket.emit('hi', 'ho')
  117. uppySocket.emit('hi', 'off to work we go')
  118. expect(emitterListenerMock.mock.calls).toEqual([
  119. ['ho', undefined, undefined, undefined, undefined, undefined],
  120. ['ho', undefined, undefined, undefined, undefined, undefined],
  121. [
  122. 'off to work we go',
  123. undefined,
  124. undefined,
  125. undefined,
  126. undefined,
  127. undefined,
  128. ],
  129. ])
  130. })
  131. it('should be able to subscribe to the first event for a particular action', () => {
  132. const uppySocket = new UppySocket({ target: 'foo' })
  133. const emitterListenerMock = jest.fn()
  134. uppySocket.once('hi', emitterListenerMock)
  135. uppySocket.emit('hi', 'ho')
  136. uppySocket.emit('hi', 'ho')
  137. uppySocket.emit('hi', 'off to work we go')
  138. expect(emitterListenerMock.mock.calls.length).toEqual(1)
  139. expect(emitterListenerMock.mock.calls).toEqual([
  140. ['ho', undefined, undefined, undefined, undefined, undefined],
  141. ])
  142. })
  143. })