TemplatesWebControllerTests.coffee 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "../../../../app/js/Features/Templates/TemplatesWebController"
  7. expect = require("chai").expect
  8. describe "TemplatesWebController", ->
  9. beforeEach ->
  10. @settings =
  11. apis:
  12. templates_api:
  13. url:"templates.sharelatex.env"
  14. @TemplatesWebController = SandboxedModule.require modulePath, requires:
  15. "settings-sharelatex":@settings
  16. "logger-sharelatex":
  17. log:->
  18. err:->
  19. @stubbedApiData =
  20. template:{_id:"12312321", name:"bob"}
  21. tag: {name:"tag name"}
  22. @TemplatesWebController._getDataFromTemplatesApi = sinon.stub().callsArgWith(1, null, @stubbedApiData)
  23. @user_id = "12332lk3jlkj"
  24. @tag_name = "tag-name-here"
  25. @template_name = "template-name-here"
  26. @template_id = "template_id_here"
  27. @req =
  28. params:
  29. user_id: @user_id
  30. @res = {}
  31. describe "renderTemplatesIndexPage", ->
  32. it "should get the data from the templates api", (done)->
  33. @res.render = (viewName, data)=>
  34. @TemplatesWebController._getDataFromTemplatesApi.calledWith("/user/#{@user_id}").should.equal true
  35. data.should.equal @stubbedApiData
  36. done()
  37. @TemplatesWebController.renderTemplatesIndexPage @req, @res
  38. describe "renerTemplateInTag", ->
  39. it "should get the data from the templates api", (done)->
  40. @res.render = (viewName, data)=>
  41. @TemplatesWebController._getDataFromTemplatesApi.calledWith("/user/#{@user_id}/tag/#{@tag_name}/template/#{@template_name}").should.equal true
  42. data.should.equal @stubbedApiData
  43. done()
  44. @req.params =
  45. user_id:@user_id
  46. template_name:@template_name
  47. tag_name:@tag_name
  48. @TemplatesWebController.renerTemplateInTag @req, @res
  49. describe "tagOrCanonicalPage", ->
  50. beforeEach ->
  51. @TemplatesWebController._renderCanonicalPage = sinon.stub()
  52. @TemplatesWebController._renderAllTemplatesPage = sinon.stub()
  53. @TemplatesWebController._renderTagPage = sinon.stub()
  54. it "should call _renderCanonicalPage if there is a template id", ()->
  55. @req.params =
  56. template_id:@template_id
  57. @TemplatesWebController.tagOrCanonicalPage @req, @res
  58. @TemplatesWebController._renderCanonicalPage.called.should.equal true
  59. @TemplatesWebController._renderAllTemplatesPage.called.should.equal false
  60. @TemplatesWebController._renderTagPage.called.should.equal false
  61. it "should call _renderAllTemplatesPage the tag name is all", ()->
  62. @req.params =
  63. tag_name:"all"
  64. @TemplatesWebController.tagOrCanonicalPage @req, @res
  65. @TemplatesWebController._renderCanonicalPage.called.should.equal false
  66. @TemplatesWebController._renderAllTemplatesPage.called.should.equal true
  67. @TemplatesWebController._renderTagPage.called.should.equal false
  68. it "should call _renderTagPage the tag name is set", ()->
  69. @req.params =
  70. tag_name:"some-tag"
  71. @TemplatesWebController.tagOrCanonicalPage @req, @res
  72. @TemplatesWebController._renderCanonicalPage.called.should.equal false
  73. @TemplatesWebController._renderAllTemplatesPage.called.should.equal false
  74. @TemplatesWebController._renderTagPage.called.should.equal true
  75. describe "_renderCanonicalPage", ->
  76. it "should get the data from the templates api", (done)->
  77. @res.render = (viewName, data)=>
  78. @TemplatesWebController._getDataFromTemplatesApi.calledWith("/user/#{@user_id}/template/#{@template_id}").should.equal true
  79. data.tag = null
  80. data.should.equal @stubbedApiData
  81. done()
  82. @req.params =
  83. user_id:@user_id
  84. template_id:@template_id
  85. @TemplatesWebController._renderCanonicalPage @req, @res
  86. describe "_renderAllTemplatesPage", ->
  87. it "should get the data from the templates api", (done)->
  88. @res.render = (viewName, data)=>
  89. @TemplatesWebController._getDataFromTemplatesApi.calledWith("/user/#{@user_id}/all").should.equal true
  90. data.should.equal @stubbedApiData
  91. done()
  92. @req.params =
  93. user_id:@user_id
  94. @TemplatesWebController._renderAllTemplatesPage @req, @res
  95. describe "_renderTagPage", ->
  96. it "should get the data from the templates api", (done)->
  97. @res.render = (viewName, data)=>
  98. @TemplatesWebController._getDataFromTemplatesApi.calledWith("/user/#{@user_id}/tag/#{@tag_name}").should.equal true
  99. data.should.equal @stubbedApiData
  100. done()
  101. @req.params =
  102. user_id:@user_id
  103. tag_name:@tag_name
  104. @TemplatesWebController._renderTagPage @req, @res