TemplatesController.test.mjs 3.4 KB

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