LocalFileWriterTests.coffee 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. assert = require("chai").assert
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. should = chai.should()
  5. expect = chai.expect
  6. modulePath = "../../../app/js/LocalFileWriter.js"
  7. SandboxedModule = require('sandboxed-module')
  8. describe "LocalFileWriter", ->
  9. beforeEach ->
  10. @writeStream =
  11. on: (type, cb)->
  12. if type == "finish"
  13. cb()
  14. @fs =
  15. createWriteStream : sinon.stub().returns(@writeStream)
  16. unlink: sinon.stub()
  17. @writer = SandboxedModule.require modulePath, requires:
  18. "fs": @fs
  19. "logger-sharelatex":
  20. log:->
  21. err:->
  22. @stubbedFsPath = "something/uploads/eio2k1j3"
  23. describe "writeStrem", ->
  24. beforeEach ->
  25. @writer._getPath = sinon.stub().returns(@stubbedFsPath)
  26. it "write the stream to ./uploads", (done)->
  27. stream =
  28. pipe: (dest)=>
  29. dest.should.equal @writeStream
  30. done()
  31. on: ->
  32. @writer.writeStream stream, null, ()=>
  33. it "should send the path in the callback", (done)->
  34. stream =
  35. pipe: (dest)=>
  36. on: (type, cb)->
  37. if type == "end"
  38. cb()
  39. @writer.writeStream stream, null, (err, fsPath)=>
  40. fsPath.should.equal @stubbedFsPath
  41. done()
  42. describe "delete file", ->
  43. it "should unlink the file", (done)->
  44. error = "my error"
  45. @fs.unlink.callsArgWith(1, error)
  46. @writer.deleteFile @stubbedFsPath, (err)=>
  47. @fs.unlink.calledWith(@stubbedFsPath).should.equal true
  48. err.should.equal error
  49. done()