Deferred.js 13.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
define([
	"doh/main",
	"dojo/Deferred",
	"dojo/promise/Promise",
	"dojo/errors/CancelError"
], function(doh, Deferred, Promise, CancelError){
	var tests = {
		"deferred receives result after resolving": function(t){
			var obj = {};
			var received;
			this.deferred.then(function(result){ received = result; });
			this.deferred.resolve(obj);
			t.t(received === obj);
		},

		"promise receives result after resolving": function(t){
			var obj = {};
			var received;
			this.deferred.promise.then(function(result){ received = obj; });
			this.deferred.resolve(obj);
			t.t(received === obj);
		},

		"resolve() returns promise": function(t){
			var obj = {};
			var returnedPromise = this.deferred.resolve(obj);
			t.t(returnedPromise instanceof Promise);
			t.t(returnedPromise === this.deferred.promise);
		},

		"isResolved() returns true after resolving": function(t){
			t.f(this.deferred.isResolved());
			this.deferred.resolve();
			t.t(this.deferred.isResolved());
		},

		"isFulfilled() returns true after resolving": function(t){
			t.f(this.deferred.isFulfilled());
			this.deferred.resolve();
			t.t(this.deferred.isFulfilled());
		},

		"resolve() is ignored after having been fulfilled": function(t){
			this.deferred.resolve();
			this.deferred.resolve();
		},

		"resolve() throws error after having been fulfilled and strict": function(t){
			this.deferred.resolve();
			t.e(Error, this.deferred, "resolve", [{}, true]);
		},

		"resolve() results are cached": function(t){
			var obj = {};
			var received;
			this.deferred.resolve(obj);
			this.deferred.then(function(result){ received = result; });
			t.t(received === obj);
		},

		"resolve() is already bound to the deferred": function(t){
			var obj = {};
			var received;
			this.deferred.then(function(result){ received = result; });
			var resolve = this.deferred.resolve;
			resolve(obj);
			t.t(received === obj);
		},

		"deferred receives result after rejecting": function(t){
			var obj = {};
			var received;
			this.deferred.then(null, function(result){ received = result; });
			this.deferred.reject(obj);
			t.t(received === obj);
		},

		"promise receives result after rejecting": function(t){
			var obj = {};
			var received;
			this.deferred.promise.then(null, function(result){ received = result; });
			this.deferred.reject(obj);
			t.t(received === obj);
		},

		"reject() returns promise": function(t){
			var obj = {};
			var returnedPromise = this.deferred.reject(obj);
			t.t(returnedPromise instanceof Promise);
			t.t(returnedPromise === this.deferred.promise);
		},

		"isRejected() returns true after rejecting": function(t){
			t.f(this.deferred.isRejected());
			this.deferred.reject();
			t.t(this.deferred.isRejected());
		},

		"isFulfilled() returns true after rejecting": function(t){
			t.f(this.deferred.isFulfilled());
			this.deferred.reject();
			t.t(this.deferred.isFulfilled());
		},

		"reject() is ignored after having been fulfilled": function(t){
			this.deferred.reject();
			this.deferred.reject();
		},

		"reject() throws error after having been fulfilled and strict": function(t){
			this.deferred.reject();
			t.e(Error, this.deferred, "reject", [{}, true]);
		},

		"reject() results are cached": function(t){
			var obj = {};
			var received;
			this.deferred.reject(obj);
			this.deferred.then(null, function(result){ received = result; });
			t.t(received === obj);
		},

		"reject() is already bound to the deferred": function(t){
			var obj = {};
			var received;
			this.deferred.then(null, function(result){ received = result; });
			var reject = this.deferred.reject;
			reject(obj);
			t.t(received === obj);
		},

		"deferred receives result after progress": function(t){
			var obj = {};
			var received;
			this.deferred.then(null, null, function(result){ received = result; });
			this.deferred.progress(obj);
			t.t(received === obj);
		},

		"promise receives result after progres": function(t){
			var obj = {};
			var received;
			this.deferred.promise.then(null, null, function(result){ received = result; });
			this.deferred.progress(obj);
			t.t(received === obj);
		},

		"progress() returns promise": function(t){
			var obj = {};
			var returnedPromise = this.deferred.progress(obj);
			t.t(returnedPromise instanceof Promise);
			t.t(returnedPromise === this.deferred.promise);
		},

		"isResolved() returns false after progress": function(t){
			t.f(this.deferred.isResolved());
			this.deferred.progress();
			t.f(this.deferred.isResolved());
		},

		"isRejected() returns false after progress": function(t){
			t.f(this.deferred.isRejected());
			this.deferred.progress();
			t.f(this.deferred.isRejected());
		},

		"isFulfilled() returns false after progress": function(t){
			t.f(this.deferred.isFulfilled());
			this.deferred.progress();
			t.f(this.deferred.isFulfilled());
		},

		"progress() is ignored after having been fulfilled": function(t){
			this.deferred.resolve();
			this.deferred.resolve();
		},

		"progress() throws error after having been fulfilled and strict": function(t){
			this.deferred.resolve();
			t.e(Error, this.deferred, "progress", [{}, true]);
		},

		"progress() results are not cached": function(t){
			var obj1 = {}, obj2 = {};
			var received = [];
			this.deferred.progress(obj1);
			this.deferred.then(null, null, function(result){ received.push(result); });
			this.deferred.progress(obj2);
			t.t(received[0] === obj2);
			t.is(1, received.length);
		},

		"progress() with chaining": function(t){
			var obj = {};
			var inner = new Deferred();
			var received;
			this.deferred.then(function(){ return inner; }).then(null, null, function(result){ received = result; });
			this.deferred.resolve();
			inner.progress(obj);
			t.t(received === obj);
		},

		"after progress(), the progback return value is emitted on the returned promise": function(t){
			var received;
			var promise = this.deferred.then(null, null, function(n){ return n * n; });
			promise.then(null, null, function(n){ received = n; });
			this.deferred.progress(2);
			t.is(4, received);
		},

		"progress() is already bound to the deferred": function(t){
			var obj = {};
			var received;
			this.deferred.then(null, null, function(result){ received = result; });
			var progress = this.deferred.progress;
			progress(obj);
			t.t(received === obj);
		},

		"cancel() invokes a canceler": function(t){
			var invoked;
			this.canceler = function(){ invoked = true; };
			this.deferred.cancel();
			t.t(invoked);
		},

		"isCanceled() returns true after canceling": function(t){
			t.f(this.deferred.isCanceled());
			this.deferred.cancel();
			t.t(this.deferred.isCanceled());
		},

		"isResolved() returns false after canceling": function(t){
			t.f(this.deferred.isResolved());
			this.deferred.cancel();
			t.f(this.deferred.isResolved());
		},

		"isRejected() returns true after canceling": function(t){
			t.f(this.deferred.isRejected());
			this.deferred.cancel();
			t.t(this.deferred.isRejected());
		},

		"isFulfilled() returns true after canceling": function(t){
			t.f(this.deferred.isFulfilled());
			this.deferred.cancel();
			t.t(this.deferred.isFulfilled());
		},

		"cancel() is ignored after having been fulfilled": function(t){
			var canceled = false;
			this.canceler = function(){ canceled = true; };
			this.deferred.resolve();
			this.deferred.cancel();
			t.f(canceled);
		},

		"cancel() throws error after having been fulfilled and strict": function(t){
			this.deferred.resolve();
			t.e(Error, this.deferred, "cancel", [null, true]);
		},

		"cancel() without reason results in CancelError": function(t){
			var reason = this.deferred.cancel();
			var received;
			this.deferred.then(null, function(result){ received = result; });
			t.t(received, reason);
		},

		"cancel() returns default reason": function(t){
			var reason = this.deferred.cancel();
			t.t(reason instanceof CancelError);
		},

		"reason is passed to canceler": function(t){
			var obj = {};
			var received;
			this.canceler = function(reason){ received = reason; };
			this.deferred.cancel(obj);
			t.t(received === obj);
		},

		"cancels with reason returned from canceler": function(t){
			var obj = {};
			var received;
			this.canceler = function(){ return obj; };
			var reason = this.deferred.cancel();
			this.deferred.then(null, function(reason){ received = reason; });
			t.t(received === obj);
		},

		"cancel() returns reason from canceler": function(t){
			var obj = {};
			this.canceler = function(){ return obj; };
			var reason = this.deferred.cancel();
			t.t(reason === obj);
		},

		"cancel() returns reason from canceler, if canceler rejects with reason": function(t){
			var obj = {};
			var deferred = this.deferred;
			this.canceler = function(){ deferred.reject(obj); return obj; };
			var reason = this.deferred.cancel();
			t.t(reason === obj);
		},

		"with canceler not returning anything, returns default CancelError": function(t){
			this.canceler = function(){};
			var reason = this.deferred.cancel();
			var received;
			this.deferred.then(null, function(result){ received = result; });
			t.t(received === reason);
		},

		"with canceler not returning anything, still returns passed reason": function(t){
			var obj = {};
			var received;
			this.canceler = function(){};
			var reason = this.deferred.cancel(obj);
			t.t(reason === obj);
			this.deferred.then(null, function(result){ received = result; });
			t.t(received === reason);
		},

		"cancel() doesn't reject promise if canceler resolves deferred": function(t){
			var deferred = this.deferred;
			var obj = {};
			var received;
			this.canceler = function(){ deferred.resolve(obj); };
			this.deferred.cancel();
			this.deferred.then(function(result){ received = result; });
			t.t(received === obj);
		},

		"cancel() doesn't reject promise if canceler resolves a chain of promises": function(t){
			var deferred = this.deferred;
			var obj = {};
			var received;
			this.canceler = function(){ deferred.resolve(obj); };
			var last = this.deferred.then().then().then();
			last.cancel();
			last.then(function(result){ received = result; });
			t.t(received === obj);
			t.t(this.deferred.isCanceled());
			t.t(last.isCanceled());
		},

		"cancel() returns undefined if canceler resolves deferred": function(t){
			var deferred = this.deferred;
			var obj = {};
			this.canceler = function(){ deferred.resolve(obj); };
			var result = this.deferred.cancel();
			t.t(typeof result === "undefined");
		},

		"cancel() doesn't change rejection value if canceler rejects deferred": function(t){
			var deferred = this.deferred;
			var obj = {};
			var received;
			this.canceler = function(){ deferred.reject(obj); };
			this.deferred.cancel();
			this.deferred.then(null, function(result){ received = result; });
			t.t(received === obj);
		},

		"cancel() doesn't change rejection value if canceler rejects a chain of promises": function(t){
			var deferred = this.deferred;
			var obj = {};
			var received;
			this.canceler = function(){ deferred.reject(obj); };
			var last = this.deferred.then().then().then();
			last.cancel();
			last.then(null, function(result){ received = result; });
			t.t(received === obj);
			t.t(this.deferred.isCanceled());
			t.t(last.isCanceled());
		},

		"cancel() returns undefined if canceler rejects deferred": function(t){
			var deferred = this.deferred;
			var obj = {};
			this.canceler = function(){ deferred.reject(obj); };
			var result = this.deferred.cancel();
			t.t(typeof result === "undefined");
		},

		"cancel() a promise chain": function(t){
			var obj = {};
			var received;
			this.canceler = function(reason){ received = reason; };
			this.deferred.then().then().then().cancel(obj);
			t.t(received === obj);
		},

		"cancel() a returned promise": function(t){
			var obj = {};
			var received;
			var inner = new Deferred(function(reason){ received = reason; });
			var chain = this.deferred.then(function(){
				return inner;
			});
			this.deferred.resolve();
			chain.cancel(obj, true);
			t.t(received === obj);
		},

		"cancel() is already bound to the deferred": function(t){
			var received;
			this.deferred.then(null, function(result){ received = result; });
			var cancel = this.deferred.cancel;
			cancel();
			t.t(received instanceof CancelError);
		},

		"chained then()": function(t){
			function square(n){ return n * n; }

			var result;
			this.deferred.then(square).then(square).then(function(n){
				result = n;
			});
			this.deferred.resolve(2);
			t.is(result, 16);
		},

		"asynchronously chained then()": function(t){
			function asyncSquare(n){
				var inner = new Deferred();
				setTimeout(function(){ inner.resolve(n * n); }, 0);
				return inner.promise;
			}

			var td = new doh.Deferred();
			this.deferred.then(asyncSquare).then(asyncSquare).then(function(n){
				t.is(n, 16);
				td.callback(true);
			});
			this.deferred.resolve(2);
			return td;
		},

		"then() is already bound to the deferred": function(t){
			var obj = {};
			var then = this.deferred.then;
			var received;
			then(function(result){ received = result; });
			this.deferred.resolve(obj);
			t.t(received === obj);
		},

		"then() with progback: returned promise is not fulfilled when progress is emitted": function(t){
			var progressed = false;
			var promise = this.deferred.then(null, null, function(){ progressed = true; });
			this.deferred.progress();
			t.t(progressed, "Progress was received.");
			t.f(promise.isFulfilled(), "Promise is not fulfilled.");
		}
	};

	var wrapped = [];
	for(var name in tests){
		wrapped.push({
			name: name,
			setUp: setUp,
			runTest: tests[name]
		});
	}

	function setUp(){
		var self = this;
		this.canceler = function(reason){};
		this.deferred = new Deferred(function(reason){ return self.canceler(reason); });
	}

	doh.register("tests.Deferred", wrapped);
});