jameslnewell / xhr-mock

Utility for mocking XMLHttpRequest.
196 stars 48 forks source link

support xhr.upload.addEventListener and removeEventListener #32

Closed beckend closed 6 years ago

beckend commented 6 years ago

does not seem to work yet

jameslnewell commented 6 years ago

Which version are you using? Are you using xhr-mock@next (v2)? Can you please provide a snippet of a minimal example that you'd expect to work.

beckend commented 6 years ago
const xhr = new XMLHttpRequest();

const handleProgress = (evt) => {
    console.log(evt);
};

xhr.upload.addEventListener('progress', handleProgress);
xhr.upload.removeEventListener('progress', handleProgress);

using 2.x preview 7

beckend commented 6 years ago

also on the note with this snippet which does not work in xhr-mock, but does in browsers.

xhr.addEventListener('loadend', handleLoadEnd);

if error occurs, this does not fire(it should).

jameslnewell commented 6 years ago

Sorry, can you please include a full example including the mock setup and full xhr details.

It should work according to these tests

beckend commented 6 years ago

the tests looks convincing, makes me wonder if i did something weird, will get back to this coming days.

jameslnewell commented 6 years ago

Hi @beckend. Have you managed to figure out whats going wrong?

beckend commented 6 years ago

@jameslnewell sorry it took a while. https://github.com/beckend/xhr-mock/blob/xhr_debug/test/debug.test.ts You can clone the branch and test, the fetchXHR is used in production and is working intended in a webrowser.

The tests should pass but do not, it's the problem i have described in this thread.

jameslnewell commented 6 years ago

Sorry I've taken so long to respond! I had some time on the train to investigate this today - thanks for your detailed repo!

xhr-mock wasn't emitting upload progress events because you were using an empty string for the body and it was evaluating to false. I've fixed the issue and released xhr-mock@2.0.0-preview.10. https://github.com/jameslnewell/xhr-mock/blob/master/src/MockXMLHttpRequest.ts#L221

re the loadend event, I think it depends on what type of error occurred. Is that your understanding too? https://xhr.spec.whatwg.org/#request-error-steps

Currently only one type of error handling is implemented and we don't have a way to tell xhr-mock which type of error handling to execute. What do you think of something like:

import mock, {RequestError, ResponseError, EndOfBodyError} from 'xhr-mock';

mock.get('/', () => Promise.reject(new ResponseError()));
beckend commented 6 years ago

looks fine to me, xhr was never easy to mock to begin with