angularjs - how to test inner controller which loads data for select control in angular-formly -
i have ui-select field
{ key: 'data_id', type: 'ui-select', templateoptions: { required: true, label: 'select label', options: [], valueprop: 'id', labelprop: 'name' }, controller: function($scope, dataservice) { dataservice.getselectdata().then(function(response) { $scope.to.options = response.data; }); } }
how can access inner controller in unit tests , check data loading select field works ?
update: example of test such:
var initializepagecontroller = function() { return $controller('pagectrl', { '$state': $state, '$stateparams': $stateparams }); }; var initializeselectcontroller = function(selectelement) { return $controller(selectelement.controller, { '$scope': $scope }); };
then test case looks like:
it('should able list of data....', function() { $scope.to = {}; var vm = initializepagecontroller(); $httpbackend.expectget(/\/api\/v1\/data...../).respond([ {id: 1, name: 'data 1'}, {id: 2, name: 'data 2'} ]); initializeselectcontroller(vm.fields[1]); $httpbackend.flush(); expect($scope.to.options.length).to.equal(2); });
you few ways. 1 option test controller contains configuration. so, if have field configuration set $scope.fields
so:
$scope.fields = [ { /* field config have above */ } ];
then in test like:
$controller($scope.fields[0].controller, { mockscope, mockdataservice });
then assertions.
Comments
Post a Comment