import uploadFile from "upload_file";
const $ = require('jquery');

describe('uploadFile', () => {
  class FakeBlobRecord {
    constructor(file, checksum, url) {
      this.file = file
      this.checksum = checksum
      this.url = url

      this.attributes = {
        filename: "file_name",
        content_type: "file_type",
        byte_size: 123,
        checksum: checksum
      }
    }

    create(callback) {
      callback(null, "checksum")
    }
  }

  class FakeFileChecksum {
    static create(file, callback) {
      callback()
    }
  }

  class FakeEvaporate {
    constructor(callback) {
      this.addCalledCount = 0
      this.callback = callback
    }

    add(inputs) {
      this.addCalledCount += 1
      this.callback()
    }
  }

  it('calls evaporate add after blob created', (done) => {
    document.body.innerHTML =
      '<div class="test">' +
      '<form>' +
      '<input class="test-input" data-direct-upload-url="http://some-awesome-url.com"/>' +
      '</form>' +
      '</div>';

    const file = jest.fn()
    const input = $(".test-input")
    const callback = () => {
      expect(evaporate.addCalledCount).toBe(1)
      done()
    }
    const evaporate = new FakeEvaporate(callback)
    const uploader = Promise.resolve(evaporate)

    uploadFile(file, uploader, input, callback, FakeBlobRecord, FakeFileChecksum)
  })
})
