From 0a8b3161b19c115ac3854217801e56ae766ab399 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Thu, 11 Nov 2010 16:21:51 -0800 Subject: $watch should optionally skip listener exec - if initRun param is set to false, listener doesn't execute - the oldValue should equal newValue during the initial execution - added docs - added specs --- test/ScopeSpec.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'test/ScopeSpec.js') diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index 4a207bf0..8b4ada5a 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -96,6 +96,40 @@ describe('scope/model', function(){ model.$eval(); expect(count).toEqual(1); }); + + it('should run listener upon registration by default', function() { + var model = createScope(); + var count = 0, + nameNewVal = 'crazy val 1', + nameOldVal = 'crazy val 2'; + + model.$watch('name', function(newVal, oldVal){ + count ++; + nameNewVal = newVal; + nameOldVal = oldVal + }); + + expect(count).toBe(1); + expect(nameNewVal).not.toBeDefined(); + expect(nameOldVal).not.toBeDefined(); + }); + + it('should not run listener upon registration if flag is passed in', function() { + var model = createScope(); + var count = 0, + nameNewVal = 'crazy val 1', + nameOldVal = 'crazy val 2'; + + model.$watch('name', function(newVal, oldVal){ + count ++; + nameNewVal = newVal; + nameOldVal = oldVal + }, undefined, false); + + expect(count).toBe(0); + expect(nameNewVal).toBe('crazy val 1'); + expect(nameOldVal).toBe('crazy val 2') + }); }); describe('$bind', function(){ -- cgit v1.2.3