MockDocUpdaterServer.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. import sinon from 'sinon'
  13. import express from 'express'
  14. let MockDocUpdaterServer
  15. export default MockDocUpdaterServer = {
  16. docs: {},
  17. createMockDoc(projectId, docId, data) {
  18. return (MockDocUpdaterServer.docs[`${projectId}:${docId}`] = data)
  19. },
  20. getDocument(projectId, docId, fromVersion, callback) {
  21. if (callback == null) {
  22. callback = function () {}
  23. }
  24. return callback(null, MockDocUpdaterServer.docs[`${projectId}:${docId}`])
  25. },
  26. deleteProject: sinon.stub().callsArg(1),
  27. getDocumentRequest(req, res, next) {
  28. const { project_id: projectId, doc_id: docId } = req.params
  29. let { fromVersion } = req.query
  30. fromVersion = parseInt(fromVersion, 10)
  31. return MockDocUpdaterServer.getDocument(
  32. projectId,
  33. docId,
  34. fromVersion,
  35. (error, data) => {
  36. if (error != null) {
  37. return next(error)
  38. }
  39. if (!data) {
  40. return res.sendStatus(404)
  41. }
  42. return res.json(data)
  43. }
  44. )
  45. },
  46. deleteProjectRequest(req, res, next) {
  47. const { project_id: projectId } = req.params
  48. return MockDocUpdaterServer.deleteProject(projectId, error => {
  49. if (error != null) {
  50. return next(error)
  51. }
  52. return res.sendStatus(204)
  53. })
  54. },
  55. running: false,
  56. run(callback) {
  57. if (callback == null) {
  58. callback = function () {}
  59. }
  60. if (MockDocUpdaterServer.running) {
  61. return callback()
  62. }
  63. const app = express()
  64. app.get(
  65. '/project/:project_id/doc/:doc_id',
  66. MockDocUpdaterServer.getDocumentRequest
  67. )
  68. app.delete(
  69. '/project/:project_id',
  70. MockDocUpdaterServer.deleteProjectRequest
  71. )
  72. return app
  73. .listen(3003, error => {
  74. MockDocUpdaterServer.running = true
  75. return callback(error)
  76. })
  77. .on('error', error => {
  78. console.error('error starting MockDocUpdaterServer:', error.message)
  79. return process.exit(1)
  80. })
  81. },
  82. }
  83. sinon.spy(MockDocUpdaterServer, 'getDocument')