I have a method which extracts the fileName from a http response header:
export const getFilenameFromResponse = response => {
const filenameRegex = /filename[^=\n]*=["](.*?)["]/;
const matches = filenameRegex.exec(
response.headers.get('Content-Disposition')
);
return matches != null && matches[1] ? matches[1] : '';
};
Now I was going to write a unit test with Jest. Unfortunately, In cannot do something like
const headers = myHeaders = new Headers([
['Content-Disposition', 'form-data; fileName="testfile.txt"']
]);
const response = new Response ({headers: newHeaders});
result = getFilenameFromResponse(response)
expect(result).ToEqual('testfile.txt';
because the test fails because the result ist an empty string. I guess, it is due to wrong init of the response object.
Is there a way to mock response.headers.get()?