I have a vue component that has a function:
...
reader.onload = () => {
let base64String = btoa(reader.result);
};
...
The base64 string is from a file input using a FileReader. I need to pass the base 64 string to the API but on a test with jest, how can I mock reader.onLoad() to return just a simples base64 string?
Thanks.
On a high level, you can try to move the base64String extraction to its own function, then mock that function:
reader.getBase64String = () => {
return btoa(reader.result)
}
reader.onload = () => {
let base64String = reader.getBase64String()
// rest of implementation
};
Then, in your test (this is pseudo-code, might not compile):
MyTestReader extends Reader {
this.getBase64String = () => {
return "SSBsaWtlIHRvIExPTAo="
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With