I cannot convert a Blob file to base64 and save it in an object in JavaScript

Viewed 112

I have the following method in my Vue component:

  methods: {
    async saveFile() {
      const { valid } = await this.$refs.file.validate();
      if (valid) {
        this.$emit("uploadFile", this.file);
      }
    },
  },

I am trying to test this method by trying to convert a blob to base64. But the problem is that wrapper.vm.file gives a null instead of the base64 file. And if the file isn't validated in the method saveFile() then, the $emit event won't happen.

I am not experienced with JavaScript, so other suggestions for the test would be great too!

var reader = new FileReader();
reader.readAsDataURL(file);
        
reader.onloadend = async function () {
    var base64data = reader.result;
    wrapper.vm.file = base64data
}
reader.onloadend()
await wrapper.vm.saveFile()
await Vue.nextTick()
expect(wrapper.emitted()).toHaveProperty('uploadFile')
1 Answers
Related