I'm use jest with vue-test-utils and I want test method for upload files. This method in 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
}
}
and I want to test it with resolve and reject
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)
})
But the test always returns null
