TemplatesControllerTests.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const SandboxedModule = require('sandboxed-module')
  2. const { expect } = require('chai')
  3. const sinon = require('sinon')
  4. const ProjectHelper = require('../../../../app/src/Features/Project/ProjectHelper')
  5. const modulePath = '../../../../app/src/Features/Templates/TemplatesController'
  6. describe('TemplatesController', function () {
  7. beforeEach(function () {
  8. this.user_id = 'user-id'
  9. this.TemplatesController = SandboxedModule.require(modulePath, {
  10. requires: {
  11. '../Project/ProjectHelper': ProjectHelper,
  12. '../Authentication/AuthenticationController':
  13. (this.AuthenticationController = {
  14. getLoggedInUserId: sinon.stub().returns(this.user_id),
  15. }),
  16. './TemplatesManager': (this.TemplatesManager = {
  17. promises: { createProjectFromV1Template: sinon.stub() },
  18. }),
  19. },
  20. })
  21. this.next = sinon.stub()
  22. this.req = {
  23. body: {
  24. brandVariationId: 'brand-variation-id',
  25. compiler: 'compiler',
  26. mainFile: 'main-file',
  27. templateId: 'template-id',
  28. templateName: 'template-name',
  29. templateVersionId: 'template-version-id',
  30. },
  31. session: {
  32. templateData: 'template-data',
  33. user: {
  34. _id: this.user_id,
  35. },
  36. },
  37. }
  38. return (this.res = { redirect: sinon.stub() })
  39. })
  40. describe('createProjectFromV1Template', function () {
  41. describe('on success', function () {
  42. beforeEach(function () {
  43. this.project = { _id: 'project-id' }
  44. this.TemplatesManager.promises.createProjectFromV1Template.resolves(
  45. this.project
  46. )
  47. return this.TemplatesController.createProjectFromV1Template(
  48. this.req,
  49. this.res,
  50. this.next
  51. )
  52. })
  53. it('should call TemplatesManager', function () {
  54. return this.TemplatesManager.promises.createProjectFromV1Template.should.have.been.calledWithMatch(
  55. 'brand-variation-id',
  56. 'compiler',
  57. 'main-file',
  58. 'template-id',
  59. 'template-name',
  60. 'template-version-id',
  61. 'user-id'
  62. )
  63. })
  64. it('should redirect to project', function () {
  65. return this.res.redirect.should.have.been.calledWith(
  66. '/project/project-id'
  67. )
  68. })
  69. it('should delete session', function () {
  70. return expect(this.req.session.templateData).to.be.undefined
  71. })
  72. })
  73. describe('on error', function () {
  74. beforeEach(function () {
  75. this.TemplatesManager.promises.createProjectFromV1Template.rejects(
  76. 'error'
  77. )
  78. return this.TemplatesController.createProjectFromV1Template(
  79. this.req,
  80. this.res,
  81. this.next
  82. )
  83. })
  84. it('should call next with error', function () {
  85. return this.next.should.have.been.calledWithMatch(
  86. sinon.match.instanceOf(Error)
  87. )
  88. })
  89. it('should not redirect', function () {
  90. return this.res.redirect.called.should.equal(false)
  91. })
  92. })
  93. })
  94. })