mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 01:13:23 -04:00
break up multiwatch tests, add complex expression test
This commit is contained in:
parent
026911b86a
commit
f50ba1cd48
1 changed files with 145 additions and 103 deletions
|
@ -11,119 +11,161 @@ define(function (require) {
|
|||
$scope = $rootScope.$new();
|
||||
}));
|
||||
|
||||
it('exposes $watchMulti on all scopes', function () {
|
||||
expect($rootScope.$watchMulti).to.be.a('function');
|
||||
expect($scope).to.have.property('$watchMulti', $rootScope.$watchMulti);
|
||||
describe('basic functionality', function () {
|
||||
it('exposes $watchMulti on all scopes', function () {
|
||||
expect($rootScope.$watchMulti).to.be.a('function');
|
||||
expect($scope).to.have.property('$watchMulti', $rootScope.$watchMulti);
|
||||
|
||||
var $isoScope = $scope.$new(true);
|
||||
expect($isoScope).to.have.property('$watchMulti', $rootScope.$watchMulti);
|
||||
});
|
||||
|
||||
it('only triggers a single watch on initialization', function () {
|
||||
var stub = sinon.stub();
|
||||
|
||||
$scope.$watchMulti([
|
||||
'one',
|
||||
'two',
|
||||
'three'
|
||||
], stub);
|
||||
$rootScope.$apply();
|
||||
|
||||
expect(stub.callCount).to.be(1);
|
||||
});
|
||||
|
||||
it('only triggers a single watch when multiple values change', function () {
|
||||
var stub = sinon.spy(function (a, b) {});
|
||||
|
||||
$scope.$watchMulti([
|
||||
'one',
|
||||
'two',
|
||||
'three'
|
||||
], stub);
|
||||
|
||||
$rootScope.$apply();
|
||||
expect(stub.callCount).to.be(1);
|
||||
|
||||
$scope.one = 'a';
|
||||
$scope.two = 'b';
|
||||
$scope.three = 'c';
|
||||
$rootScope.$apply();
|
||||
|
||||
expect(stub.callCount).to.be(2);
|
||||
});
|
||||
|
||||
it('passes an array of the current values as the first arg, and an array of the previous values as the second',
|
||||
function () {
|
||||
var stub = sinon.spy(function (a, b) {});
|
||||
|
||||
$scope.one = 'a';
|
||||
$scope.two = 'b';
|
||||
$scope.three = 'c';
|
||||
$scope.$watchMulti([
|
||||
'one',
|
||||
'two',
|
||||
'three'
|
||||
], stub);
|
||||
|
||||
$rootScope.$apply();
|
||||
expect(stub.firstCall.args).to.eql([
|
||||
['a', 'b', 'c'],
|
||||
['a', 'b', 'c']
|
||||
]);
|
||||
|
||||
$scope.one = 'do';
|
||||
$scope.two = 're';
|
||||
$scope.three = 'mi';
|
||||
$rootScope.$apply();
|
||||
|
||||
expect(stub.secondCall.args).to.eql([
|
||||
['do', 're', 'mi'],
|
||||
['a', 'b', 'c']
|
||||
]);
|
||||
});
|
||||
|
||||
it('the current value is always up to date', function () {
|
||||
var count = 0;
|
||||
|
||||
$scope.vals = [1, 0];
|
||||
$scope.$watchMulti([ 'vals[0]', 'vals[1]' ], function (cur, prev) {
|
||||
expect(cur).to.eql($scope.vals);
|
||||
count++;
|
||||
var $isoScope = $scope.$new(true);
|
||||
expect($isoScope).to.have.property('$watchMulti', $rootScope.$watchMulti);
|
||||
});
|
||||
|
||||
var $child = $scope.$new();
|
||||
$child.$watch('vals[0]', function (cur) {
|
||||
$child.vals[1] = cur;
|
||||
});
|
||||
it('returns a working unwatch function', function () {
|
||||
$scope.a = 0;
|
||||
$scope.b = 0;
|
||||
var triggers = 0;
|
||||
var unwatch = $scope.$watchMulti(['a', 'b'], function () { triggers++; });
|
||||
|
||||
$rootScope.$apply();
|
||||
expect(count).to.be(2);
|
||||
// initial watch
|
||||
$scope.$apply();
|
||||
expect(triggers).to.be(1);
|
||||
|
||||
// prove that it triggers on chagne
|
||||
$scope.a++;
|
||||
$scope.$apply();
|
||||
expect(triggers).to.be(2);
|
||||
|
||||
// remove watchers
|
||||
expect($scope.$$watchers).to.not.eql([]);
|
||||
unwatch();
|
||||
expect($scope.$$watchers).to.eql([]);
|
||||
|
||||
// prove that it doesn't trigger anymore
|
||||
$scope.a++;
|
||||
$scope.$apply();
|
||||
expect(triggers).to.be(2);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a working unwatch function', function () {
|
||||
$scope.a = 0;
|
||||
$scope.b = 0;
|
||||
var triggers = 0;
|
||||
var unwatch = $scope.$watchMulti(['a', 'b'], function () { triggers++; });
|
||||
describe('simple scope watchers', function () {
|
||||
it('only triggers a single watch on initialization', function () {
|
||||
var stub = sinon.stub();
|
||||
|
||||
// initial watch
|
||||
$scope.$apply();
|
||||
expect(triggers).to.be(1);
|
||||
$scope.$watchMulti([
|
||||
'one',
|
||||
'two',
|
||||
'three'
|
||||
], stub);
|
||||
$rootScope.$apply();
|
||||
|
||||
// prove that it triggers on chagne
|
||||
$scope.a++;
|
||||
$scope.$apply();
|
||||
expect(triggers).to.be(2);
|
||||
expect(stub.callCount).to.be(1);
|
||||
});
|
||||
|
||||
// remove watchers
|
||||
expect($scope.$$watchers).to.not.eql([]);
|
||||
unwatch();
|
||||
expect($scope.$$watchers).to.eql([]);
|
||||
it('only triggers a single watch when multiple values change', function () {
|
||||
var stub = sinon.spy(function (a, b) {});
|
||||
|
||||
// prove that it doesn't trigger anymore
|
||||
$scope.a++;
|
||||
$scope.$apply();
|
||||
expect(triggers).to.be(2);
|
||||
$scope.$watchMulti([
|
||||
'one',
|
||||
'two',
|
||||
'three'
|
||||
], stub);
|
||||
|
||||
$rootScope.$apply();
|
||||
expect(stub.callCount).to.be(1);
|
||||
|
||||
$scope.one = 'a';
|
||||
$scope.two = 'b';
|
||||
$scope.three = 'c';
|
||||
$rootScope.$apply();
|
||||
|
||||
expect(stub.callCount).to.be(2);
|
||||
});
|
||||
|
||||
it('passes an array of the current and previous values, in order',
|
||||
function () {
|
||||
var stub = sinon.spy(function (a, b) {});
|
||||
|
||||
$scope.one = 'a';
|
||||
$scope.two = 'b';
|
||||
$scope.three = 'c';
|
||||
$scope.$watchMulti([
|
||||
'one',
|
||||
'two',
|
||||
'three'
|
||||
], stub);
|
||||
|
||||
$rootScope.$apply();
|
||||
expect(stub.firstCall.args).to.eql([
|
||||
['a', 'b', 'c'],
|
||||
['a', 'b', 'c']
|
||||
]);
|
||||
|
||||
$scope.one = 'do';
|
||||
$scope.two = 're';
|
||||
$scope.three = 'mi';
|
||||
$rootScope.$apply();
|
||||
|
||||
expect(stub.secondCall.args).to.eql([
|
||||
['do', 're', 'mi'],
|
||||
['a', 'b', 'c']
|
||||
]);
|
||||
});
|
||||
|
||||
it('always has an up to date value', function () {
|
||||
var count = 0;
|
||||
|
||||
$scope.vals = [1, 0];
|
||||
$scope.$watchMulti([ 'vals[0]', 'vals[1]' ], function (cur, prev) {
|
||||
expect(cur).to.eql($scope.vals);
|
||||
count++;
|
||||
});
|
||||
|
||||
var $child = $scope.$new();
|
||||
$child.$watch('vals[0]', function (cur) {
|
||||
$child.vals[1] = cur;
|
||||
});
|
||||
|
||||
$rootScope.$apply();
|
||||
expect(count).to.be(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('complex watch expressions', function () {
|
||||
var stateWatchers;
|
||||
var firstValue;
|
||||
var secondValue;
|
||||
|
||||
beforeEach(function () {
|
||||
var firstGetter = function () {
|
||||
return firstValue;
|
||||
};
|
||||
|
||||
var secondGetter = function () {
|
||||
return secondValue;
|
||||
};
|
||||
|
||||
stateWatchers = [{
|
||||
fn: $rootScope.$watch,
|
||||
get: firstGetter
|
||||
}, {
|
||||
fn: $rootScope.$watch,
|
||||
get: secondGetter
|
||||
}];
|
||||
});
|
||||
|
||||
it('should trigger the watcher on initialization', function () {
|
||||
var stub = sinon.stub();
|
||||
firstValue = 'first';
|
||||
secondValue = 'second';
|
||||
|
||||
$scope.$watchMulti(stateWatchers, stub);
|
||||
$rootScope.$apply();
|
||||
|
||||
expect(stub.callCount).to.be(1);
|
||||
|
||||
expect(stub.firstCall.args[0]).to.eql([firstValue, secondValue]);
|
||||
expect(stub.firstCall.args[1]).to.eql([firstValue, secondValue]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue