Skip to content

Commit

Permalink
fix(ngTableSelectFilterDs): not binding to scope an array returned as…
Browse files Browse the repository at this point in the history
…ynchronously
  • Loading branch information
ccrowhurstram committed Sep 1, 2015
1 parent a096f4f commit 4c06368
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/scripts/ngTableSelectFilterDs.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@
ngTableSelectFilterDsController.$inject = ['$scope', '$parse', '$attrs', '$q'];
function ngTableSelectFilterDsController($scope, $parse, $attrs, $q){

var $column = {};
init();

function init(){
var $column = $parse($attrs.ngTableSelectFilterDs)($scope);
$column = $parse($attrs.ngTableSelectFilterDs)($scope);
$scope.$watch(function(){
return $column.data;
}, bindDataSource);
}

function bindDataSource(){
getSelectListData($column).then(function(data){
if (data && !hasEmptyOption(data)){
data.unshift({ id: '', title: ''});
Expand Down
124 changes: 122 additions & 2 deletions test/selectFilterDsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ describe('ngTableSelectFilterDs directive', function(){
expect($scope.$selectData).toEqual([]);
});

it('should keep the array on scope in sync with data array on $column', function(){
// given
$scope.$column = {
data: null
};
$compile(elem)($scope);
$scope.$digest();

// when
var newArray = [{id: 1, title: 'A'}];
$scope.$column.data = newArray;
$scope.$digest();

// then
expect($scope.$selectData).toBe(newArray);
});

it('should add empty option to array', function(){
// note: modifying the array supplied is not great as this can cause unexpected side effects
// however, it does mean that a consumer can update the array and have this reflected in the select list
Expand Down Expand Up @@ -84,6 +101,23 @@ describe('ngTableSelectFilterDs directive', function(){
// then
expect(data).toEqual([{id: '', title: ''}, {id: 1, title: 'A'}]);
});

it('should add empty option to a new arriving array', function(){
// given
$scope.$column = {
data: [{id: 1, title: 'A'}]
};
$compile(elem)($scope);
$scope.$digest();

// when
$scope.$column.data = [{id: 1, title: 'B'}];
$scope.$digest();

// then
expect($scope.$selectData).toEqual([{ id: '', title: ''}, {id: 1, title: 'B'}]);
});

});

describe('function datasource', function(){
Expand Down Expand Up @@ -117,6 +151,24 @@ describe('ngTableSelectFilterDs directive', function(){
expect($scope.$selectData).toEqual([]);
});

it('should keep the array on scope in sync with data array on $column', function(){
// given
data = [{id: 1, title: 'A'}];
$compile(elem)($scope);
$scope.$digest();

// when
var newArray = [{id: 1, title: 'A'}];
$scope.$column.data = function(){
return newArray;
};
$scope.$digest();

// then
expect($scope.$selectData).toBe(newArray);
});


it('should add empty option to array', function(){
// given
data = [{id: 1, title: 'A'}];
Expand Down Expand Up @@ -149,14 +201,34 @@ describe('ngTableSelectFilterDs directive', function(){
// then
expect(data).toEqual([{id: '', title: ''}, {id: 1, title: 'A'}]);
});

it('should add empty option to a new arriving array', function(){
// given
data = [{id: 1, title: 'A'}];
$compile(elem)($scope);
$scope.$digest();

// when
$scope.$column.data = function(){
return [{id: 1, title: 'B'}];
};
$scope.$digest();

// then
expect($scope.$selectData).toEqual([{ id: '', title: ''}, {id: 1, title: 'B'}]);
});
});

describe('asyn function datasource', function(){
var data;
beforeEach(inject(function($q){
var $timeout;
beforeEach(inject(function(_$timeout_){
$timeout = _$timeout_;
$scope.$column = {
data: function(){
return $q.when(data);
return $timeout(function(){
return data;
}, 10);
}
};
}));
Expand All @@ -167,6 +239,7 @@ describe('ngTableSelectFilterDs directive', function(){
// when
$compile(elem)($scope);
$scope.$digest();
$timeout.flush();
// then
expect($scope.$selectData).toBe(data);
});
Expand All @@ -177,16 +250,40 @@ describe('ngTableSelectFilterDs directive', function(){
// when
$compile(elem)($scope);
$scope.$digest();
$timeout.flush();
// then
expect($scope.$selectData).toEqual([]);
});

it('should keep the array on scope in sync with data array on $column', function(){
// given
data = [{id: 1, title: 'A'}];
$compile(elem)($scope);
$scope.$digest();
$timeout.flush();

// when
var newArray = [{id: 1, title: 'A'}];
$scope.$column.data = function(){
return $timeout(function(){
return newArray;
}, 10);
};
$scope.$digest();
$timeout.flush();

// then
expect($scope.$selectData).toBe(newArray);
});


it('should add empty option to array', function(){
// given
data = [{id: 1, title: 'A'}];
// when
$compile(elem)($scope);
$scope.$digest();
$timeout.flush();
// then
expect(data).toEqual([{ id: '', title: ''}, {id: 1, title: 'A'}]);
});
Expand All @@ -200,6 +297,7 @@ describe('ngTableSelectFilterDs directive', function(){
// when
$compile(elem)($scope);
$scope.$digest();
$timeout.flush();
// then
expect(data).toEqual([{ id: '', title: ''}]);
});
Expand All @@ -210,8 +308,30 @@ describe('ngTableSelectFilterDs directive', function(){
// when
$compile(elem)($scope);
$scope.$digest();
$timeout.flush();
// then
expect(data).toEqual([{id: '', title: ''}, {id: 1, title: 'A'}]);
});

it('should add empty option to a new arriving array', function(){
// given
data = [{id: 1, title: 'A'}];
$compile(elem)($scope);
$scope.$digest();
$timeout.flush();

// when
$scope.$column.data = function(){
return $timeout(function(){
return [{id: 1, title: 'B'}];
}, 10);
};
$scope.$digest();
$timeout.flush();

// then
expect($scope.$selectData).toEqual([{ id: '', title: ''}, {id: 1, title: 'B'}]);
});

});
});

0 comments on commit 4c06368

Please sign in to comment.