SystemMessageManagerTests.coffee 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. SandboxedModule = require('sandboxed-module')
  2. assert = require('assert')
  3. require('chai').should()
  4. sinon = require('sinon')
  5. modulePath = require('path').join __dirname, '../../../../app/js/Features/SystemMessages/SystemMessageManager.js'
  6. describe 'SystemMessageManager', ->
  7. beforeEach ->
  8. @SystemMessage = {}
  9. @SystemMessageManager = SandboxedModule.require modulePath, requires:
  10. "../../models/SystemMessage": SystemMessage: @SystemMessage
  11. @callback = sinon.stub()
  12. describe "getMessage", ->
  13. beforeEach ->
  14. @messages = ["messages-stub"]
  15. @SystemMessage.find = sinon.stub().callsArgWith(1, null, @messages)
  16. describe "when the messages are not cached", ->
  17. beforeEach ->
  18. @SystemMessageManager.getMessages @callback
  19. it "should look the messages up in the database", ->
  20. @SystemMessage.find
  21. .calledWith({})
  22. .should.equal true
  23. it "should return the messages", ->
  24. @callback.calledWith(null, @messages).should.equal true
  25. it "should cache the messages", ->
  26. @SystemMessageManager._cachedMessages.should.equal @messages
  27. describe "when the messages are cached", ->
  28. beforeEach ->
  29. @SystemMessageManager._cachedMessages = @messages
  30. @SystemMessageManager.getMessages @callback
  31. it "should not look the messages up in the database", ->
  32. @SystemMessage.find.called.should.equal false
  33. it "should return the messages", ->
  34. @callback.calledWith(null, @messages).should.equal true
  35. describe "clearMessages", ->
  36. beforeEach ->
  37. @SystemMessage.remove = sinon.stub().callsArg(1)
  38. @SystemMessageManager.clearMessages @callback
  39. it "should remove the messages from the database", ->
  40. @SystemMessage.remove
  41. .calledWith({})
  42. .should.equal true
  43. it "should return the callback", ->
  44. @callback.called.should.equal true