ScopeScheduler test questions
Opened this issue · 0 comments
I was looking at the ScopeScheduler test to figure out how ScopeScheduler should be used, but I'm not sure I understand the test either, so I'm still not sure I understand how to use ScopeScheduler.
I assume that if ScopeScheduler is used with .observeOn()
, the code in the subscriber's onNext function will effectively be passed to Scope.$apply()
thus ensuring the onNext logic is followed by a $digest()
so potentially-relevant watcher callbacks are run. Based on that assumption, I'd write a naive test like this:
var injector = angular.injector(['ng', 'rx']);
var scope = injector.get('$rootScope').$new();
scope.$watch('testProperty', function (val) {
start();
ok(true, 'testProperty watcher fired');
equal(val, 42, 'testProperty should match value emitted by observable');
});
Rx.Observable.return(42)
.observeOn(new Rx.ScopeScheduler(scope))
.subscribe(function(value) {
scope.testProperty = value;
});
expect(2);
I think this would prove the ScopeScheduler caused the watcher to fire and that the testProperty contains the expected value. (This test passes.)
But in the current test... the two watchers on testProperty
- $watch()
and $toObservable()
- and the throttle and the assignment in the tap and the testProperty2
and the commented-out test that fails... These all have me confused and thinking I must be missing something. What was the test trying to prove beyond my basic test above?
The current test only seems to prove that the onNext function is invoked - ok(true)
. We've no idea if a digest is actually triggered by the scheduler and no idea if the watchers see new values that might have been set inside the onNext. But again, maybe I'm completely missing some understanding of the intended usage of ScopeScheduler.
As for the commented-out line :
//ok(scope.testProperty2 === 2); -- Don't know why this doesn't work.
I think the scope.testProperty = 2
assignment inside the tap won't be noticed by Angular until after the ok(scope.testProperty2 === 2)
test is run on the ScopeScheduler. This is because an Angular $apply()
first evaluates it's expression/function arg - in this case the subscribe()
's onNext function containing the failing ok()
- and then Angular calls $rootScope.$digest()
which fires all the watchers. So the first watcher's invocation that should be assigning 2 to scope.testProperty2
doesn't fire until after the code where you test scope.testProperty2 === 2
, hence scope.testProperty2
is still 1 and that test fails.
Also I noticed the implementation of ScopeScheduler contains scheduleNow, scheduleRelative, and scheduleAbsolute functions that are passed to the base Scheduler implementation. I barely even understand what those are for, but does this test attempt to exercise them somehow? How and when would I make use of an immediate, relative or absolute ScopeScheduler?