Skip to content

Commit db5ee64

Browse files
nateabelechristopherthielen
authored andcommitted
test($stateParams): verify observation behavior
1 parent 79c4649 commit db5ee64

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

0 commit comments

Comments
 (0)