PeekingADoc.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const sinon = require('sinon')
  2. const MockWebApi = require('./helpers/MockWebApi')
  3. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  4. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  5. describe('Peeking a document', function () {
  6. before(function (done) {
  7. this.lines = ['one', 'two', 'three']
  8. this.version = 42
  9. return DocUpdaterApp.ensureRunning(done)
  10. })
  11. describe('when the document is not loaded', function () {
  12. before(function (done) {
  13. this.project_id = DocUpdaterClient.randomId()
  14. this.doc_id = DocUpdaterClient.randomId()
  15. sinon.spy(MockWebApi, 'getDocument')
  16. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  17. lines: this.lines,
  18. version: this.version,
  19. })
  20. return DocUpdaterClient.peekDoc(
  21. this.project_id,
  22. this.doc_id,
  23. (error, res, returnedDoc) => {
  24. this.error = error
  25. this.res = res
  26. this.returnedDoc = returnedDoc
  27. return done()
  28. }
  29. )
  30. })
  31. after(function () {
  32. return MockWebApi.getDocument.restore()
  33. })
  34. it('should return a 404 response', function () {
  35. this.res.statusCode.should.equal(404)
  36. })
  37. it('should not load the document from the web API', function () {
  38. return MockWebApi.getDocument.called.should.equal(false)
  39. })
  40. })
  41. describe('when the document is already loaded', function () {
  42. before(function (done) {
  43. this.project_id = DocUpdaterClient.randomId()
  44. this.doc_id = DocUpdaterClient.randomId()
  45. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  46. lines: this.lines,
  47. version: this.version,
  48. })
  49. return DocUpdaterClient.preloadDoc(
  50. this.project_id,
  51. this.doc_id,
  52. error => {
  53. if (error != null) {
  54. throw error
  55. }
  56. sinon.spy(MockWebApi, 'getDocument')
  57. return DocUpdaterClient.getDoc(
  58. this.project_id,
  59. this.doc_id,
  60. (error, res, returnedDoc) => {
  61. if (error) return done(error)
  62. this.res = res
  63. this.returnedDoc = returnedDoc
  64. return done()
  65. }
  66. )
  67. }
  68. )
  69. })
  70. after(function () {
  71. return MockWebApi.getDocument.restore()
  72. })
  73. it('should return a 200 response', function () {
  74. this.res.statusCode.should.equal(200)
  75. })
  76. it('should return the document lines', function () {
  77. return this.returnedDoc.lines.should.deep.equal(this.lines)
  78. })
  79. it('should return the document version', function () {
  80. return this.returnedDoc.version.should.equal(this.version)
  81. })
  82. it('should not load the document from the web API', function () {
  83. return MockWebApi.getDocument.called.should.equal(false)
  84. })
  85. })
  86. })