UppySocket.test.js 5.1 KB

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