Skip to content

Commit 9f58507

Browse files
committed
test($stateParams): verify observation behavior
1 parent d254057 commit 9f58507

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

test/stateSpec.js

+83
Original file line numberDiff line numberDiff line change
@@ -1062,3 +1062,86 @@ describe('state queue', function() {
10621062
});
10631063
});
10641064
});
1065+
1066+
describe("state params", function() {
1067+
1068+
describe("observation", function() {
1069+
it("should broadcast updates when values change", inject(function($stateParams, $rootScope) {
1070+
var called = false;
1071+
1072+
$stateParams.$observe("a", function(newVal) {
1073+
called = (newVal === "Hello");
1074+
});
1075+
1076+
$stateParams.a = "Hello";
1077+
$rootScope.$digest();
1078+
expect(called).toBe(true);
1079+
}));
1080+
1081+
it("should broadcast once on change", inject(function($stateParams, $rootScope) {
1082+
var called = 0;
1083+
1084+
$stateParams.$observe("a", function(newVal) {
1085+
called++;
1086+
});
1087+
1088+
$stateParams.a = "Hello";
1089+
$rootScope.$digest();
1090+
expect(called).toBe(1);
1091+
1092+
$rootScope.$digest();
1093+
expect(called).toBe(1);
1094+
1095+
$stateParams.a = "Goodbye";
1096+
$rootScope.$digest();
1097+
expect(called).toBe(2);
1098+
}));
1099+
1100+
it("should be attachable to multiple fields", inject(function($stateParams, $rootScope) {
1101+
var called = 0;
1102+
1103+
$stateParams.$observe("a b", function(newVal) {
1104+
called += (newVal === "Hello") ? 1 : 0;
1105+
});
1106+
1107+
$stateParams.a = "Hello";
1108+
$rootScope.$digest();
1109+
1110+
expect(called).toBe(1);
1111+
1112+
$stateParams.b = "Hello";
1113+
$rootScope.$digest();
1114+
1115+
expect(called).toBe(2);
1116+
}));
1117+
1118+
it("should be detachable", inject(function($stateParams, $rootScope) {
1119+
var called = 0, off = $stateParams.$observe("a", function(newVal) {
1120+
called++;
1121+
});
1122+
1123+
$stateParams.a = "Hello";
1124+
$rootScope.$digest();
1125+
off();
1126+
1127+
$stateParams.a = "Goodbye";
1128+
$rootScope.$digest();
1129+
1130+
expect(called).toBe(1);
1131+
1132+
$stateParams.$observe("a", function(newVal) {
1133+
called++;
1134+
});
1135+
1136+
$stateParams.a = "Hello";
1137+
$rootScope.$digest();
1138+
expect(called).toBe(2);
1139+
1140+
$stateParams.$off();
1141+
1142+
$stateParams.a = "Hello";
1143+
$rootScope.$digest();
1144+
expect(called).toBe(2);
1145+
}));
1146+
});
1147+
});

0 commit comments

Comments
 (0)