There is a view with a button with the angular ng-disabled directive associated with some model properties. The complication is that the button is located inside the <li> tag symbol.
What I want to do is set up the properties of the model, compile the view, and then check if the button is turned on or off correctly according to the values of the model properties; but when I try to compile, the list is empty: there is only a <ul> </ul> tag.
Is there a way to accomplish what I'm trying to do?
Here is my code:
----------------- view -------------------
<div ng-show="group.AreItemsExpanded">
<div>
<ul>
<li ng-repeat="item in group.Items">
<div>
{{item.Name}}
</div>
<div>
<button id="btn" type="button" ng-click="clickItem(item)"
ng-disabled="!permissions.IsAllowed || !item.IsClickable">
Click!
</button>
</div>
</li>
</ul>
</div>
</div>
----------------- test ------------------
describe('testing view', function(){
beforeEach(module('groups.templates'));
var $scope;
var item;
var $templateCache;
var $compile;
beforeEach(inject(function($rootScope, _$templateCache_, _$compile_){
$templateCache = _$templateCache_;
$compile = _$compile_;
item = {
Name: 'testitem0',
IsClickable: false
};
$scope = $rootScope.$new();
$scope.group = {
Items: [item],
AreItemsExpanded: true
};
$scope.permissions = {IsAllowed: false};
}));
describe('testing button', function(){
it('should be enabled', function(){
$scope.group.Items[0].IsClickable = true;
$scope.permissions.IsAllowed = true;
var html = $templateCache.get('view.html');
var view = $compile(angular.element(html))($scope);
expect(view.find('button')[0].disabled).toEqual(false);
});
it('should be disabled', function(){
$scope.group.Items[0].IsClickable = false;
$scope.permissions.IsAllowed = true;
var html = $templateCache.get('view.html');
var view = $compile(angular.element(html))($scope);
expect(view.find('button')[0].disabled).toEqual(true);
});
});
});
------------- karma.conf.js ---------------------
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'node_modules/angular-resource/angular-resource.js',
'node_modules/angular-route/angular-route.js',
'node_modules/angular-bootstrap/ui-bootstrap.js',
'*.js',
'*.html'
],
exclude: [
],
preprocessors: {
"*.html": ["ng-html2js"]
},
ngHtml2JsPreprocessor: {
moduleName: "groups.templates"
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};