0

I have this logic for temporarily reading and displaying a profile image:

setTempProfileImage(file) {
      if (file) {
        let reader = new FileReader()
        reader.onload = e => {
          this.generalInfo.profileImage = e.target.result
        }
        reader.readAsDataURL(file)
      }
    }

where file is a BLOB object (which for testing is an 'empty' blob object).

But I'm struggling to write a unit test that checks the onload function call and reader.readDataAsURL(file).

I've got this unit test so far:

let file = new Blob(["testing"], { type: "application/pdf" });
wrapper.vm.setTempProfileImage(file)
wrapper.vm.$nextTick()
console.log(wrapper.vm.profileImage)
expect(wrapper.vm.generalInfo.profileImage).toBe(file)

but it simply reads back as 'undefined'

1 Answers1

0

Estus Flask's link answered my question: https://stackoverflow.com/a/61575553/3731501

along with this:

https://zaengle.com/blog/mocking-file-upload-in-vue-with-jest

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 09 '22 at 06:22