Currently, I am using jest along with vue-test-utils to test a method that uploads files. This particular method is located in the vue file component.
async uploadFiles(file){
this.$refs.iconUpload.setAttribute('data-load', 'loading')
let formData = new FormData();
formData.append('image', file);
try{
const result = await fetch('/upload', {
method: 'POST',
body: formData,
})
const data = await result.json();
if(data.status){
this.$refs.iconUpload.setAttribute('data-load', 'success')
this.$refs.btnClear.style.display = 'none';
this.$refs.btnGoBack.style.display = 'none';
this.uploadDone = true;
}else {
this.showSpecialError(this.$refs.elseError)
}
}catch (e){
this.showSpecialError(this.$refs.elseError)
return null
}
}
In addition, I aim to perform tests on this method involving resolve and reject functionalities.
const file = {
size: 500,
type: 'image/png',
name: 'image.png
};
const event = {
target: {
files: [file],
},
};
global.fetch = jest.fn(() => Promise.resolve({
json: () => Promise.resolve({ data })
}));
test('the function of sending the file returns the required response on a successful response', async () => {
const result = await wrapper.vm.uploadFiles(event)
expect(result).toEqual(file)
})
However, the current issue I am facing is that the test consistently returns null.