Observer.spec.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import Vue from 'vue'
  2. import Emitter from '@/Emitter'
  3. import Observer from '@/Observer'
  4. import { Server, WebSocket } from 'mock-socket'
  5. describe('Observer.js', () => {
  6. let observer, mockServer
  7. let wsUrl = 'ws://localhost:8080'
  8. it('fires onopen event', (done) => {
  9. mockServer = new Server(wsUrl)
  10. mockServer.on('connection', ws => {
  11. ws.send('hi')
  12. })
  13. Vue.use(VueNativeSock, wsUrl)
  14. let vm = new Vue()
  15. observer = new Observer(wsUrl)
  16. Emitter.addListener('onopen', (data) => {
  17. expect(data.type).to.equal('open')
  18. mockServer.stop(done)
  19. }, vm)
  20. })
  21. it('fires onopen event skip scheme', (done) => {
  22. mockServer = new Server(wsUrl)
  23. mockServer.on('connection', ws => {
  24. ws.send('hi')
  25. })
  26. Vue.use(VueNativeSock, '//localhost:8080')
  27. let vm = new Vue()
  28. observer = new Observer(wsUrl)
  29. Emitter.addListener('onopen', (data) => {
  30. expect(data.type).to.equal('open')
  31. mockServer.stop(done)
  32. }, vm)
  33. })
  34. // TODO: DRY
  35. it('passes a json commit to the provided vuex store', (done) => {
  36. let expectedMsg = { mutation: 'setName', value: 'steve' }
  37. let mockStore = sinon.mock({ commit: () => {} })
  38. mockStore.expects('commit').withArgs('SOCKET_ONOPEN')
  39. mockStore.expects('commit').withArgs(expectedMsg.mutation)
  40. mockServer = new Server(wsUrl)
  41. mockServer.on('connection', ws => {
  42. ws.send(JSON.stringify(expectedMsg))
  43. })
  44. Vue.use(VueNativeSock, wsUrl)
  45. let vm = new Vue()
  46. observer = new Observer(wsUrl, {
  47. store: mockStore.object,
  48. format: 'json',
  49. websocket: new WebSocket(wsUrl)
  50. })
  51. setTimeout(() => {
  52. mockStore.verify()
  53. mockServer.stop(done)
  54. }, 100)
  55. })
  56. // TODO: DRY
  57. it('passes a json action to the provided vuex store', (done) => {
  58. let expectedMsg = { action: 'setName', value: 'steve' }
  59. let mockStore = sinon.mock({
  60. commit: () => {},
  61. dispatch: () => {}
  62. })
  63. mockStore.expects('dispatch').withArgs(expectedMsg.action, expectedMsg)
  64. mockServer = new Server(wsUrl)
  65. mockServer.on('connection', ws => {
  66. ws.send(JSON.stringify(expectedMsg))
  67. })
  68. Vue.use(VueNativeSock, wsUrl)
  69. let vm = new Vue()
  70. observer = new Observer(wsUrl, {
  71. store: mockStore.object,
  72. format: 'json',
  73. websocket: new WebSocket(wsUrl)
  74. })
  75. setTimeout(() => {
  76. mockStore.verify()
  77. mockServer.stop(done)
  78. }, 100)
  79. })
  80. // TODO: DRY
  81. it('passes a namespaced json commit to the provided vuex store', (done) => {
  82. let expectedMsg = { namespace: 'users', mutation: 'setName', value: 'steve' }
  83. let mockStore = sinon.mock({ commit: () => {} })
  84. mockStore.expects('commit').withArgs('SOCKET_ONOPEN')
  85. mockStore.expects('commit').withArgs(expectedMsg.namespace + '/' + expectedMsg.mutation)
  86. mockServer = new Server(wsUrl)
  87. mockServer.on('connection', ws => {
  88. ws.send(JSON.stringify(expectedMsg))
  89. })
  90. Vue.use(VueNativeSock, wsUrl)
  91. let vm = new Vue()
  92. observer = new Observer(wsUrl, {
  93. store: mockStore.object,
  94. format: 'json',
  95. websocket: new WebSocket(wsUrl)
  96. })
  97. setTimeout(() => {
  98. mockStore.verify()
  99. mockServer.stop(done)
  100. }, 100)
  101. })
  102. // TODO: DRY
  103. it('passes a namespaced json action to the provided vuex store', (done) => {
  104. let expectedMsg = { namespace: 'users', action: 'setName', value: 'steve' }
  105. let mockStore = sinon.mock({
  106. commit: () => {},
  107. dispatch: () => {}
  108. })
  109. mockStore.expects('dispatch').withArgs(expectedMsg.namespace + '/' + expectedMsg.action, expectedMsg)
  110. mockServer = new Server(wsUrl)
  111. mockServer.on('connection', ws => {
  112. ws.send(JSON.stringify(expectedMsg))
  113. })
  114. Vue.use(VueNativeSock, wsUrl)
  115. let vm = new Vue()
  116. observer = new Observer(wsUrl, {
  117. store: mockStore.object,
  118. format: 'json',
  119. websocket: new WebSocket(wsUrl)
  120. })
  121. setTimeout(() => {
  122. mockStore.verify()
  123. mockServer.stop(done)
  124. }, 100)
  125. })
  126. // TODO: DRY
  127. it('passes a custom commit name to the provided vuex store', (done) => {
  128. let expectedMsg = 'hello world'
  129. let mutations = {
  130. SOCKET_ONOPEN: '✅ Socket connected',
  131. SOCKET_ONMESSAGE: 'Websocket message received'
  132. }
  133. let mockStore = sinon.mock({ commit: () => {} })
  134. mockStore.expects('commit').withArgs(mutations.SOCKET_ONOPEN)
  135. mockStore.expects('commit').withArgs(mutations.SOCKET_ONMESSAGE)
  136. mockServer = new Server(wsUrl)
  137. mockServer.on('connection', ws => {
  138. ws.send(expectedMsg)
  139. })
  140. Vue.use(VueNativeSock, wsUrl)
  141. let vm = new Vue()
  142. observer = new Observer(wsUrl, {
  143. store: mockStore.object,
  144. mutations,
  145. websocket: new WebSocket(wsUrl)
  146. })
  147. setTimeout(() => {
  148. mockStore.verify()
  149. mockServer.stop(done)
  150. }, 100)
  151. })
  152. describe('reconnection feature', () => {
  153. let observer, mockServer, vm, mockStore
  154. let wsUrl = 'ws://localhost:8080'
  155. beforeEach(() => {
  156. mockServer = new Server(wsUrl)
  157. mockServer.on('connection', ws => ws.send('hi'))
  158. Vue.use(VueNativeSock, wsUrl)
  159. vm = new Vue()
  160. mockStore = sinon.mock({ commit: () => {} })
  161. observer = new Observer(wsUrl, {
  162. store: mockStore.object,
  163. reconnection: true,
  164. reconnectionAttempts: 2,
  165. WebSocket: new WebSocket(wsUrl),
  166. })
  167. })
  168. it('calls #reconnect() method', (done) => {
  169. sinon.spy(observer, 'reconnect');
  170. mockServer.close()
  171. expect(observer.reconnect).to.called
  172. mockServer.stop(done)
  173. })
  174. it('fires SOCKET_RECONNECT event', (done) => {
  175. sinon.spy(observer, 'passToStore');
  176. const clock = sinon.useFakeTimers()
  177. mockServer.close()
  178. clock.tick(1500);
  179. expect(observer.passToStore).to.have.been.calledWith('SOCKET_RECONNECT')
  180. mockServer.stop(done)
  181. })
  182. it('fires SOCKET_RECONNECT_ERROR event, after all attemps', (done) => {
  183. sinon.spy(observer, 'passToStore');
  184. observer.reconnectionCount = 2
  185. observer.reconnectionAttempts = 1
  186. mockServer.close()
  187. expect(observer.passToStore).to.have.been.calledWith('SOCKET_RECONNECT_ERROR')
  188. mockServer.stop(done)
  189. })
  190. })
  191. })