ProjectApiController.test.mjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { vi } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath = '../../../../app/src/Features/Project/ProjectApiController'
  4. describe('Project api controller', function () {
  5. beforeEach(async function (ctx) {
  6. ctx.ProjectDetailsHandler = { getDetails: sinon.stub() }
  7. vi.doMock(
  8. '../../../../app/src/Features/Project/ProjectDetailsHandler',
  9. () => ({
  10. default: ctx.ProjectDetailsHandler,
  11. })
  12. )
  13. ctx.controller = (await import(modulePath)).default
  14. ctx.project_id = '321l3j1kjkjl'
  15. ctx.req = {
  16. params: {
  17. project_id: ctx.project_id,
  18. },
  19. session: {
  20. destroy: sinon.stub(),
  21. },
  22. }
  23. ctx.res = {}
  24. ctx.next = sinon.stub()
  25. return (ctx.projDetails = { name: 'something' })
  26. })
  27. describe('getProjectDetails', function () {
  28. it('should ask the project details handler for proj details', async function (ctx) {
  29. await new Promise(resolve => {
  30. ctx.ProjectDetailsHandler.getDetails.callsArgWith(
  31. 1,
  32. null,
  33. ctx.projDetails
  34. )
  35. ctx.res.json = data => {
  36. ctx.ProjectDetailsHandler.getDetails
  37. .calledWith(ctx.project_id)
  38. .should.equal(true)
  39. data.should.deep.equal(ctx.projDetails)
  40. return resolve()
  41. }
  42. return ctx.controller.getProjectDetails(ctx.req, ctx.res)
  43. })
  44. })
  45. it('should send a 500 if there is an error', function (ctx) {
  46. ctx.ProjectDetailsHandler.getDetails.callsArgWith(1, 'error')
  47. ctx.controller.getProjectDetails(ctx.req, ctx.res, ctx.next)
  48. return ctx.next.calledWith('error').should.equal(true)
  49. })
  50. })
  51. })