Skip to content

Update upstream #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angularjs",
"license": "MIT",
"branchVersion": "^1.6.0",
"branchVersion": "^1.7.0",
"branchPattern": "1.7.*",
"distTag": "latest",
"repository": {
Expand Down
47 changes: 45 additions & 2 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@ angular.mock.$httpBackendDecorator =
function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
var definitions = [],
expectations = [],
matchLatestDefinition = false,
responses = [],
responsesPush = angular.bind(responses, responses.push),
copy = angular.copy,
Expand Down Expand Up @@ -1430,8 +1431,9 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
wasExpected = true;
}

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

while ((definition = definitions[matchLatestDefinition ? --i : ++i])) {
if (definition.match(method, url, data, headers || {})) {
if (definition.response) {
// if $browser specified, we do auto flush all requests
Expand Down Expand Up @@ -1507,6 +1509,47 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
return chain;
};

/**
* @ngdoc method
* @name $httpBackend#matchLatestDefinition
* @description
* This method can be used to change which mocked responses `$httpBackend` returns, when defining
* them with {@link ngMock.$httpBackend#when $httpBackend.when()} (and shortcut methods).
* By default, `$httpBackend` returns the first definition that matches. When setting
* `$http.matchLatestDefinition(true)`, it will use the last response that matches, i.e. the
* one that was added last.
*
* ```js
* hb.when('GET', '/url1').respond(200, 'content', {});
* hb.when('GET', '/url1').respond(201, 'another', {});
* hb('GET', '/url1'); // receives "content"
*
* $http.matchLatestDefinition(true)
* hb('GET', '/url1'); // receives "another"
*
* hb.when('GET', '/url1').respond(201, 'onemore', {});
* hb('GET', '/url1'); // receives "onemore"
* ```
*
* This is useful if a you have a default response that is overriden inside specific tests.
*
* Note that different from config methods on providers, `matchLatestDefinition()` can be changed
* even when the application is already running.
*
* @param {Boolean=} value value to set, either `true` or `false`. Default is `false`.
* If omitted, it will return the current value.
* @return {$httpBackend|Boolean} self when used as a setter, and the current value when used
* as a getter
*/
$httpBackend.matchLatestDefinitionEnabled = function(value) {
if (isDefined(value)) {
matchLatestDefinition = value;
return this;
} else {
return matchLatestDefinition;
}
};

/**
* @ngdoc method
* @name $httpBackend#whenGET
Expand Down
74 changes: 73 additions & 1 deletion test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ describe('ngMock', function() {
});


it('should respond with first matched definition', function() {
it('should respond with first matched definition by default', function() {
hb.when('GET', '/url1').respond(200, 'content', {});
hb.when('GET', '/url1').respond(201, 'another', {});

Expand All @@ -1106,6 +1106,78 @@ describe('ngMock', function() {
});


describe('matchLatestDefinitionEnabled()', function() {

it('should be set to false by default', function() {
expect(hb.matchLatestDefinitionEnabled()).toBe(false);
});


it('should allow to change the value', function() {
hb.matchLatestDefinitionEnabled(true);
expect(hb.matchLatestDefinitionEnabled()).toBe(true);
});


it('should return the httpBackend when used as a setter', function() {
expect(hb.matchLatestDefinitionEnabled(true)).toBe(hb);
});


it('should respond with the first matched definition when false',
function() {
hb.matchLatestDefinitionEnabled(false);

hb.when('GET', '/url1').respond(200, 'content', {});
hb.when('GET', '/url1').respond(201, 'another', {});

callback.and.callFake(function(status, response) {
expect(status).toBe(200);
expect(response).toBe('content');
});

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


it('should respond with latest matched definition when true',
function() {
hb.matchLatestDefinitionEnabled(true);

hb.when('GET', '/url1').respond(200, 'match1', {});
hb.when('GET', '/url1').respond(200, 'match2', {});
hb.when('GET', '/url2').respond(204, 'nomatch', {});

callback.and.callFake(function(status, response) {
expect(status).toBe(200);
expect(response).toBe('match2');
});

hb('GET', '/url1', null, callback);

// Check if a newly added match is used
hb.when('GET', '/url1').respond(201, 'match3', {});

var callback2 = jasmine.createSpy();

callback2.and.callFake(function(status, response) {
expect(status).toBe(201);
expect(response).toBe('match3');
});

hb('GET', '/url1', null, callback2);
expect(callback).not.toHaveBeenCalled();
hb.flush();
expect(callback).toHaveBeenCalledOnce();
}
);
});


it('should respond with a copy of the mock data', function() {
var mockObject = {a: 'b'};

Expand Down