TemplatesControllerTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. '../SplitTests/SplitTestHandler': (this.SplitTestHandler = {
  20. promises: {
  21. getAssignment: sinon.stub().resolves({ variant: 'default' }),
  22. },
  23. }),
  24. },
  25. })
  26. this.next = sinon.stub()
  27. this.req = {
  28. body: {
  29. brandVariationId: 'brand-variation-id',
  30. compiler: 'compiler',
  31. mainFile: 'main-file',
  32. templateId: 'template-id',
  33. templateName: 'template-name',
  34. templateVersionId: 'template-version-id',
  35. },
  36. session: {
  37. templateData: 'template-data',
  38. user: {
  39. _id: this.user_id,
  40. },
  41. },
  42. }
  43. return (this.res = { redirect: sinon.stub() })
  44. })
  45. describe('createProjectFromV1Template', function () {
  46. describe('on success', function () {
  47. beforeEach(function () {
  48. this.project = { _id: 'project-id' }
  49. this.TemplatesManager.promises.createProjectFromV1Template.resolves(
  50. this.project
  51. )
  52. return this.TemplatesController.createProjectFromV1Template(
  53. this.req,
  54. this.res,
  55. this.next
  56. )
  57. })
  58. it('should call TemplatesManager', function () {
  59. return this.TemplatesManager.promises.createProjectFromV1Template.should.have.been.calledWithMatch(
  60. 'brand-variation-id',
  61. 'compiler',
  62. 'main-file',
  63. 'template-id',
  64. 'template-name',
  65. 'template-version-id',
  66. 'user-id'
  67. )
  68. })
  69. it('should redirect to project', function () {
  70. return this.res.redirect.should.have.been.calledWith(
  71. '/project/project-id'
  72. )
  73. })
  74. it('should delete session', function () {
  75. return expect(this.req.session.templateData).to.be.undefined
  76. })
  77. })
  78. describe('on error', function () {
  79. beforeEach(function () {
  80. this.TemplatesManager.promises.createProjectFromV1Template.rejects(
  81. 'error'
  82. )
  83. return this.TemplatesController.createProjectFromV1Template(
  84. this.req,
  85. this.res,
  86. this.next
  87. )
  88. })
  89. it('should call next with error', function () {
  90. return this.next.should.have.been.calledWithMatch(
  91. sinon.match.instanceOf(Error)
  92. )
  93. })
  94. it('should not redirect', function () {
  95. return this.res.redirect.called.should.equal(false)
  96. })
  97. })
  98. })
  99. })