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

Commit ed92fb2

Browse files
committed
fixup! feat(ngMock, ngMockE2E): add option to match latest definition for $httpBackend request
1 parent 35aaef3 commit ed92fb2

File tree

2 files changed

+102
-19
lines changed

2 files changed

+102
-19
lines changed

src/ngMock/angular-mocks.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -1509,10 +1509,42 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
15091509
return chain;
15101510
};
15111511

1512-
$httpBackend.matchLatestDefinition = function(value) {
1513-
1512+
/**
1513+
* @ngdoc method
1514+
* @name $httpBackend#matchLatestDefinition
1515+
* @description
1516+
* This method can be used to change which mocked responses `$httpBackend` returns, when defining
1517+
* them with {@link ngMock.$httpBackend#when $httpBackend.when()} (and shortcut methods).
1518+
* By default, `$httpBackend` returns the first definition that matches. When setting
1519+
* `$http.matchLatestDefinition(true)`, it will use the last response that matches, i.e. the
1520+
* one that was added last.
1521+
*
1522+
* ```js
1523+
* hb.when('GET', '/url1').respond(200, 'content', {});
1524+
* hb.when('GET', '/url1').respond(201, 'another', {});
1525+
* hb('GET', '/url1'); // receives "content"
1526+
*
1527+
* $http.matchLatestDefinition(true)
1528+
* hb('GET', '/url1'); // receives "another"
1529+
*
1530+
* hb.when('GET', '/url1').respond(201, 'onemore', {});
1531+
* hb('GET', '/url1'); // receives "onemore"
1532+
* ```
1533+
*
1534+
* This is useful if a you have a default response that is overriden inside specific tests.
1535+
*
1536+
* Note that different from config methods on providers, `matchLatestDefinition()` can be changed
1537+
* even when the application is already running.
1538+
*
1539+
* @param {Boolean=} value value to set, either `true` or `false`. Default is `false`.
1540+
* If omitted, it will return the current value.
1541+
* @return {$httpBackend|Boolean} self when used as a setter, and the current value when used
1542+
* as a getter
1543+
*/
1544+
$httpBackend.matchLatestDefinitionEnabled = function(value) {
15141545
if (isDefined(value)) {
15151546
matchLatestDefinition = value;
1547+
return this;
15161548
} else {
15171549
return matchLatestDefinition;
15181550
}

test/ngMock/angular-mocksSpec.js

+68-17
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ describe('ngMock', function() {
10901090
});
10911091

10921092

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

@@ -1106,25 +1106,76 @@ describe('ngMock', function() {
11061106
});
11071107

11081108

1109-
it('should respond with latest matched definition when $httpBackend.matchLatestDefinition(true)',
1110-
function() {
1111-
hb.matchLatestDefinition(true);
1109+
describe('matchLatestDefinitionEnabled()', function() {
11121110

1113-
hb.when('GET', '/url1').respond(200, 'content', {});
1114-
hb.when('GET', '/url1').respond(200, 'middle', {});
1115-
hb.when('GET', '/url1').respond(201, 'another', {});
1111+
it('should be set to false by default', function() {
1112+
expect(hb.matchLatestDefinitionEnabled()).toBe(false);
1113+
});
11161114

1117-
callback.and.callFake(function(status, response) {
1118-
expect(status).toBe(201);
1119-
expect(response).toBe('another');
1120-
});
11211115

1122-
hb('GET', '/url1', null, callback);
1123-
expect(callback).not.toHaveBeenCalled();
1124-
hb.flush();
1125-
expect(callback).toHaveBeenCalledOnce();
1126-
}
1127-
);
1116+
it('should allow to change the value', function() {
1117+
hb.matchLatestDefinitionEnabled(true);
1118+
expect(hb.matchLatestDefinitionEnabled()).toBe(true);
1119+
});
1120+
1121+
1122+
it('should return the httpBackend when used as a setter', function() {
1123+
expect(hb.matchLatestDefinitionEnabled(true)).toBe(hb);
1124+
});
1125+
1126+
1127+
it('should respond with the first matched definition when false',
1128+
function() {
1129+
hb.matchLatestDefinitionEnabled(false);
1130+
1131+
hb.when('GET', '/url1').respond(200, 'content', {});
1132+
hb.when('GET', '/url1').respond(201, 'another', {});
1133+
1134+
callback.and.callFake(function(status, response) {
1135+
expect(status).toBe(200);
1136+
expect(response).toBe('content');
1137+
});
1138+
1139+
hb('GET', '/url1', null, callback);
1140+
expect(callback).not.toHaveBeenCalled();
1141+
hb.flush();
1142+
expect(callback).toHaveBeenCalledOnce();
1143+
}
1144+
);
1145+
1146+
1147+
it('should respond with latest matched definition when true',
1148+
function() {
1149+
hb.matchLatestDefinitionEnabled(true);
1150+
1151+
hb.when('GET', '/url1').respond(200, 'match1', {});
1152+
hb.when('GET', '/url1').respond(200, 'match2', {});
1153+
hb.when('GET', '/url2').respond(204, 'nomatch', {});
1154+
1155+
callback.and.callFake(function(status, response) {
1156+
expect(status).toBe(200);
1157+
expect(response).toBe('match2');
1158+
});
1159+
1160+
hb('GET', '/url1', null, callback);
1161+
1162+
// Check if a newly added match is used
1163+
hb.when('GET', '/url1').respond(201, 'match3', {});
1164+
1165+
var callback2 = jasmine.createSpy();
1166+
1167+
callback2.and.callFake(function(status, response) {
1168+
expect(status).toBe(201);
1169+
expect(response).toBe('match3');
1170+
});
1171+
1172+
hb('GET', '/url1', null, callback2);
1173+
expect(callback).not.toHaveBeenCalled();
1174+
hb.flush();
1175+
expect(callback).toHaveBeenCalledOnce();
1176+
}
1177+
);
1178+
});
11281179

11291180

11301181
it('should respond with a copy of the mock data', function() {

0 commit comments

Comments
 (0)