Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(ngMock, ngMockE2E): add option to match latest definition for $h… #16560

Closed
wants to merge 2 commits into from

Conversation

Narretz
Copy link
Contributor

@Narretz Narretz commented May 9, 2018

…ttpBackend request

Closes #16251
Closes #11637

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Feature

What is the current behavior? (You can also link to an open issue here)
See #11637

What is the new behavior (if this is a feature change)?
Config option to change the behavior

Does this PR introduce a breaking change?
No

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Fix/Feature: Docs have been added/updated
  • [ ?] Fix/Feature: Tests have been added; existing tests pass

Other information:
First draft to discuss the API.
As noted in #16251, it's not possible (afaics) to put the option on the httpBackendProvider, because they cannot be decorated. It's also not useful to put the method on the ng.httpBackendProvider, because that's not where it's needed. The option to switch it directly from httpBackend could also be used to migrate tests step by step or use it only for new tests. Another option would be a new "$httpBackendMocksProvider" or similar (but I don't think that's very useful).

@Narretz Narretz force-pushed the feat-ngMock-match-latest branch from f3fa83b to 35aaef3 Compare May 9, 2018 17:38
@petebacondarwin petebacondarwin self-assigned this May 14, 2018
Copy link
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM except for the lack of docs 📖

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but needs docs and perhaps another test?

while ((definition = definitions[++i])) {
var i = matchLatestDefinition ? definitions.length : -1, definition;

while ((definition = definitions[matchLatestDefinition ? --i : ++i])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is dangerous stuff due to the subtle difference between prefix and postfix operators. But given that we were doing this already I guess it'll be fine.

@@ -1507,6 +1509,15 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
return chain;
};

$httpBackend.matchLatestDefinition = function(value) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this blank line needed / in keeping with the rest of the code?

$httpBackend.matchLatestDefinition = function(value) {

if (isDefined(value)) {
matchLatestDefinition = value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we normally return this when it is a setter to allow chaining?

hb.flush();
expect(callback).toHaveBeenCalledOnce();
}
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we already have tests for the order when this is false?

hb('GET', '/url1', null, callback);
expect(callback).not.toHaveBeenCalled();
hb.flush();
expect(callback).toHaveBeenCalledOnce();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check that it continues to work backwards if the last one doesn't match?

@@ -1507,6 +1509,15 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
return chain;
};

$httpBackend.matchLatestDefinition = function(value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we needed docs here.

@Narretz Narretz force-pushed the feat-ngMock-match-latest branch from 235d954 to ed92fb2 Compare May 18, 2018 19:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ngMock - why does $httpBackend match the first and not the last mock definition?
4 participants