-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix($resource): Check if timeoutDeferred is null inside $cancelRequest #16037
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add some tests to make sure we don't accidentally break this in the future?
Hi @gkalpak, This is my first time contributing to this project, so kindly bear with me. |
That's where I got stuck too since I couldn't find any existing tests for cancelRequest. I thought I would spend some time digging into the code and figure it out but I couldn't 🙁 |
@chirag64, the it('should not break when calling old `$cancelRequest` after the response arrives', function() {
$httpBackend.whenGET('/CreditCard').respond({});
var CreditCard = $resource('/CreditCard', {}, {
get: {
method: 'GET',
cancellable: true
}
});
var creditCard = CreditCard.get();
var cancelRequest = creaditCard.$cancelRequest;
$httpBackend.flush();
expect(cancelRequest).not.toBe(noop);
expect(cancelRequest).not.toThrow();
}); (Please, ensure that the test fails before your change in @namansancheti, thx for stepping up. Let's leave this to @chirag64, since he is already working on it (and willing to take it further). Don't get disappointed though 😃 We have lots of issues that are good candidates for first-time contributors here. (Just make sure you don't pick one someone's already working on.) Always feel free to ask for further guidance/info/help. Welcome and have fun! |
4c0b173
to
1fc383b
Compare
@gkalpak Thanks for the help with the test, I've pushed in a few changes. Let me know if I missed something out 🙂 |
test/ngResource/resourceSpec.js
Outdated
var cancelRequest = creditCard.$cancelRequest; | ||
|
||
creditCard.$promise.then(function(list) { | ||
cancelRequest(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the piece of code mentioned in the Issue #16016 in the codepen to reproduce this issue. I suppose this is not needed since we'll be calling our function using the expect function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you call cancelRequest
after flushing (as suggested in https://github.com/angular/angular.js/pull/16037/files#r127979323), then this shouldn't be needed (unless I am missing something).
test/ngResource/resourceSpec.js
Outdated
|
||
$httpBackend.flush(); | ||
|
||
expect(creditCard.$cancelRequest).not.toThrow(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wouldn't fail before either. Please change to:
expect(cancelRequest).not.toBe(noop);
expect(cancelRequest).not.toThrow();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I removed lines 2118 - 2121 and added not.toBe(noop) just above 2124, just like you mentioned in your comments. But now the tests are passing before and after the change I made in resource.js.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you also changed creditCard.$cancelRequest
to cancelRequest
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's exactly what I missed, thanks 👍🏼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx! LGTM
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix
What is the current behavior? (You can also link to an open issue here)
#16016
What is the new behavior (if this is a feature change)?
Does this PR introduce a breaking change?
No
Please check if the PR fulfills these requirements
Other information: