jakerella / jquery-mockjax

The jQuery Mockjax Plugin provides a simple and extremely flexible interface for mocking or simulating ajax requests and responses
Other
2.11k stars 375 forks source link

cleared mocks are not disposed properly #273

Open piuccio opened 8 years ago

piuccio commented 8 years ago

Hi

I'm having an issue with requests completed after cleaning mocks

$.mockjax({
   url: '/hello',
   responseTime: 10000,
   responseText: "whatever"
});
$.get('/hello', function () {
   console.log('I don't expect to get here');
});
$.mockjax.clear();

The request is already in progress when I clear mockjax, but all clear does is prevent other request from starting. The ones already in progress cannot be stopped.

Do you have any suggestions?

Best would be implement #184 and handle this case in the clear or dispose. A quicker approach would be to keep track of pending requests. I can't do it from my code because .response is called after responseTime and there's no event onBeforeRequest or beforeSend.

$.mockjax.mockedAjaxCalls() gets close to my need, but it only contains a list of requests, there's no reference to mockRequest = _ajax.call(). I could do with a $.mockjax.mockedAjaxRequests() but this method is not available.

jakerella commented 8 years ago

Hmm... This is tricky, the issue is that the setTimeout call is only used when responseTime is provided, otherwise we just call the underlying (original) $.ajax() method and it takes however long it takes. As such, implementing a beforeResponse event would not be consistent. Similarly, we can check to see if the mock is still valid after the setTimeout call expires, but it will not be consistent as we cannot do that for calls directly to $.ajax().

I'm open to ideas, but implementing #184 won't do this, even if you implemented a clear on that returned object because it could not be consistently applied. I'm open to options, but I don't see a clear winner yet.