RoomManagerTests.coffee 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Rooms/RoomManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. events = require "events"
  8. mongojs = require "mongojs"
  9. ObjectId = mongojs.ObjectId
  10. describe "RoomManager", ->
  11. beforeEach ->
  12. @RoomManager = SandboxedModule.require modulePath, requires:
  13. "../../mongojs":
  14. db: @db = { rooms: {} }
  15. ObjectId: ObjectId
  16. @callback = sinon.stub()
  17. describe "findOrCreateRoom", ->
  18. describe "when the room exists", ->
  19. beforeEach ->
  20. @project_id = ObjectId().toString()
  21. @room =
  22. _id: ObjectId()
  23. project_id: ObjectId(@project_id)
  24. @db.rooms.findOne = sinon.stub().callsArgWith(1, null, @room)
  25. @RoomManager.findOrCreateRoom(project_id: @project_id, @callback)
  26. it "should look up the room based on the query", ->
  27. @db.rooms.findOne
  28. .calledWith(project_id: ObjectId(@project_id))
  29. .should.equal true
  30. it "should return the room in the callback", ->
  31. @callback
  32. .calledWith(null, @room)
  33. .should.equal true
  34. describe "when the room does not exist", ->
  35. beforeEach ->
  36. @project_id = ObjectId().toString()
  37. @room =
  38. _id: ObjectId()
  39. project_id: ObjectId(@project_id)
  40. @db.rooms.findOne = sinon.stub().callsArgWith(1, null, null)
  41. @db.rooms.save = sinon.stub().callsArgWith(1, null, @room)
  42. @RoomManager.findOrCreateRoom(project_id: @project_id, @callback)
  43. it "should create the room", ->
  44. @db.rooms.save
  45. .calledWith(project_id: ObjectId(@project_id))
  46. .should.equal true
  47. it "should return the room in the callback", ->
  48. @callback
  49. .calledWith(null, @room)
  50. .should.equal true