DocumentTests.coffee 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. define [
  2. "libs/chai"
  3. "editor/Document"
  4. "editor/ShareJsDoc"
  5. "libs/sinon"
  6. ], (
  7. chai
  8. Document
  9. ShareJsDoc
  10. ) ->
  11. should = chai.should()
  12. describe "Document", ->
  13. beforeEach ->
  14. @ide =
  15. socket: @socket =
  16. socket:
  17. sessionid: @session_id = "mock-session-id"
  18. connected: true
  19. emit: (name, args..., callback = () ->) ->
  20. if @handlers[name]?
  21. @handlers[name].call(@, args..., callback)
  22. handlers: {}
  23. sinon.spy @ide.socket, "emit"
  24. _.extend(@ide, Backbone.Events)
  25. _.extend(@socket, Backbone.Events)
  26. @socket.removeListener = @socket.off
  27. sinon.spy ShareJsDoc::, "flushPendingOps"
  28. sinon.spy ShareJsDoc::, "updateConnectionState"
  29. @doc_id = "mock-doc-id"
  30. @docLines = ["hello", "world"]
  31. @version = 5
  32. @remote_session_id = "remote-session-id"
  33. @socket.handlers["joinDoc"] = (doc_id, args...) =>
  34. doc_id.should.equal @doc_id
  35. callback = args.pop()
  36. callback null, @docLines, @version, []
  37. @socket.handlers["leaveDoc"] = (doc_id, args...) =>
  38. doc_id.should.equal @doc_id
  39. callback = args.pop()
  40. callback null
  41. @doc = new Document(@ide, @doc_id)
  42. sinon.spy @doc, "trigger"
  43. afterEach ->
  44. ShareJsDoc::flushPendingOps.restore()
  45. ShareJsDoc::updateConnectionState.restore()
  46. # This is a little pattern I'm trying out to make the tests below much
  47. # more readable when they have a lot of repetition
  48. CONDITIONS =
  49. "connected": () ->
  50. @doc.connected = true
  51. "not connected": () ->
  52. @doc.connected = false
  53. "reconnected": () ->
  54. @ide.trigger "afterJoinProject"
  55. "disconnected": () ->
  56. @ide.socket.trigger "disconnect"
  57. "joining": () ->
  58. @callback = sinon.stub()
  59. @doc.join @callback
  60. "joined": () ->
  61. @callback = sinon.stub()
  62. @doc.join @callback
  63. "leaving": () ->
  64. @callback = sinon.stub()
  65. @doc.leave @callback
  66. "there are buffered ops": () ->
  67. sinon.stub @doc.doc, "hasBufferedOps", () -> return true
  68. "the buffered ops have been sent and acknowledged": () ->
  69. sinon.stub @doc.doc, "processUpdateFromServer"
  70. @doc.doc.hasBufferedOps.restore()
  71. @ide.socket.trigger "otUpdateApplied", { doc: @doc_id }
  72. @doc.doc.processUpdateFromServer.restore()
  73. TESTS =
  74. "emit joinDoc": () ->
  75. @socket.emit.calledWith("joinDoc", @doc_id).should.equal true
  76. "not emit joinDoc": () ->
  77. @socket.emit.calledWith("joinDoc").should.equal false
  78. "emit leaveDoc": () ->
  79. @socket.emit.calledWith("leaveDoc", @doc_id).should.equal true
  80. "not emit leaveDoc": () ->
  81. @socket.emit.calledWith("leaveDoc").should.equal false
  82. "be joined": () ->
  83. @doc.joined.should.equal true
  84. "not be joined": () ->
  85. @doc.joined.should.equal false
  86. "be wanting to be joined": () ->
  87. @doc.wantToBeJoined.should.equal true
  88. "not be wanting to be joined": () ->
  89. @doc.wantToBeJoined.should.equal false
  90. "call the callback": () ->
  91. @callback.called.should.equal true
  92. "not call the callback": () ->
  93. @callback.called.should.equal false
  94. "flush any pending ops": () ->
  95. ShareJsDoc::flushPendingOps.called.should.equal true
  96. "update the connection state to ok": () ->
  97. ShareJsDoc::updateConnectionState.calledWith("ok").should.equal true
  98. WHEN = (condition, callback) -> [
  99. "when " + condition,
  100. () ->
  101. beforeEach CONDITIONS[condition]
  102. callback()
  103. ]
  104. SHOULD = (test) -> ["should " + test, TESTS[test]]
  105. describe WHEN("connected", ->
  106. describe WHEN("joining", ->
  107. it SHOULD("emit joinDoc")...
  108. it SHOULD("be joined")...
  109. it SHOULD("be wanting to be joined")...
  110. it SHOULD("call the callback")...
  111. )...
  112. describe WHEN("leaving", ->
  113. it SHOULD("emit leaveDoc")...
  114. it SHOULD("not be joined")...
  115. it SHOULD("not be wanting to be joined")...
  116. it SHOULD("call the callback")...
  117. )...
  118. describe WHEN("joined", ->
  119. describe WHEN("there are buffered ops", ->
  120. describe WHEN("leaving", ->
  121. it SHOULD("not emit leaveDoc")...
  122. it SHOULD("be joined")...
  123. it SHOULD("not be wanting to be joined")...
  124. it SHOULD("not call the callback")...
  125. describe WHEN("the buffered ops have been sent and acknowledged", ->
  126. it SHOULD("emit leaveDoc")...
  127. it SHOULD("not be joined")...
  128. it SHOULD("not be wanting to be joined")...
  129. it SHOULD("call the callback")...
  130. )...
  131. )...
  132. )...
  133. describe WHEN("disconnected", ->
  134. describe WHEN("leaving", ->
  135. it SHOULD("not be wanting to be joined")...
  136. it SHOULD("not emit leaveDoc")...
  137. it SHOULD("call the callback")...
  138. describe WHEN("reconnected", ->
  139. it SHOULD("not emit leaveDoc")...
  140. it SHOULD("not be joined")...
  141. it SHOULD("not be wanting to be joined")...
  142. )...
  143. )...
  144. describe WHEN("there are buffered ops", ->
  145. describe WHEN("leaving", ->
  146. it SHOULD("not be wanting to be joined")...
  147. it SHOULD("not emit leaveDoc")...
  148. it SHOULD("not call the callback")...
  149. describe WHEN("reconnected", ->
  150. it SHOULD("emit joinDoc")...
  151. it SHOULD("flush any pending ops")...
  152. describe WHEN("the buffered ops have been sent and acknowledged", ->
  153. it SHOULD("emit leaveDoc")...
  154. it SHOULD("not be joined")...
  155. it SHOULD("not be wanting to be joined")...
  156. it SHOULD("call the callback")...
  157. )...
  158. )...
  159. )...
  160. )...
  161. )...
  162. )...
  163. )...
  164. describe WHEN("not connected", ->
  165. describe WHEN("joining", ->
  166. it SHOULD("be wanting to be joined")...
  167. it SHOULD("not emit joinDoc")...
  168. it SHOULD("not be joined")...
  169. it SHOULD("not call the callback")...
  170. describe WHEN("reconnected", ->
  171. it SHOULD("emit joinDoc")...
  172. it SHOULD("be joined")...
  173. it SHOULD("be wanting to be joined")...
  174. it SHOULD("flush any pending ops")...
  175. it SHOULD("update the connection state to ok")...
  176. )...
  177. )...
  178. )...
  179. describe "leaving a doc", ->
  180. describe "when not connected", ->
  181. describe "with buffered ops", ->
  182. it "should not be wanting to be joined"
  183. describe "when reconnected", ->
  184. it "should emit joinDoc"
  185. it "should flush the bufferedOps"
  186. it "should emit leaveDoc"
  187. it "should not be joined"
  188. it "should be wanting to be joined"
  189. it "should call the callback"