PeekingADoc.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const sinon = require('sinon')
  2. const MockWebApi = require('./helpers/MockWebApi')
  3. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  4. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  5. const { expect } = require('chai')
  6. const { RequestFailedError } = require('@overleaf/fetch-utils')
  7. describe('Peeking a document', function () {
  8. before(async function () {
  9. this.lines = ['one', 'two', 'three']
  10. this.version = 42
  11. await DocUpdaterApp.ensureRunning()
  12. })
  13. describe('when the document is not loaded', function () {
  14. before(async function () {
  15. this.project_id = DocUpdaterClient.randomId()
  16. this.doc_id = DocUpdaterClient.randomId()
  17. sinon.spy(MockWebApi, 'getDocument')
  18. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  19. lines: this.lines,
  20. version: this.version,
  21. })
  22. })
  23. after(function () {
  24. MockWebApi.getDocument.restore()
  25. })
  26. it('should not load the document from the web API and should return a 404 response', async function () {
  27. await expect(DocUpdaterClient.peekDoc(this.project_id, this.doc_id))
  28. .to.be.rejectedWith(RequestFailedError)
  29. .and.eventually.have.nested.property('response.status', 404)
  30. MockWebApi.getDocument.called.should.equal(false)
  31. })
  32. })
  33. describe('when the document is already loaded', function () {
  34. before(async function () {
  35. this.project_id = DocUpdaterClient.randomId()
  36. this.doc_id = DocUpdaterClient.randomId()
  37. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  38. lines: this.lines,
  39. version: this.version,
  40. })
  41. await DocUpdaterClient.preloadDoc(this.project_id, this.doc_id)
  42. sinon.spy(MockWebApi, 'getDocument')
  43. this.returnedDoc = await DocUpdaterClient.peekDoc(
  44. this.project_id,
  45. this.doc_id
  46. )
  47. })
  48. after(function () {
  49. MockWebApi.getDocument.restore()
  50. })
  51. it('should return the document lines', function () {
  52. this.returnedDoc.lines.should.deep.equal(this.lines)
  53. })
  54. it('should return the document version', function () {
  55. this.returnedDoc.version.should.equal(this.version)
  56. })
  57. it('should not load the document from the web API', function () {
  58. MockWebApi.getDocument.called.should.equal(false)
  59. })
  60. })
  61. })