var app = angular.module('plunker', []);
app.directive('sampleOne', function (){
// this is an attribute with no required controllers,
// and no isolated scope, so we're going to use all the
// defaults, and just providing a linking function.
return function(scope, elem, attrs) {
elem.bind('click', function(){
elem.text(scope.$eval(attrs.sampleOne));
});
};
});
app.directive('sampleTwo', function (){
return {
restrict: 'E',
template: '<div>{{value}}</div>',
scope: {
value: '='
}
};
});
describe('Testing sampleOne directive', function() {
var scope,
elem,
directive,
compiled,
html;
beforeEach(function (){
//load the module
module('plunker');
//set our view html.
html = '<div sample-one="foo"></div>';
inject(function($compile, $rootScope) {
//create a scope (you could just use $rootScope, I suppose)
scope = $rootScope.$new();
//get the jqLite or jQuery element
elem = angular.element(html);
//compile the element into a function to
// process the view.
compiled = $compile(elem);
//run the compiled view.
compiled(scope);
//call digest on the scope!
scope.$digest();
});
});
it('Should set the text of the element to whatever was passed.', function() {
//set a value (the same one we had in the html)
scope.foo = 'bar';
//check to see if it's blank first.
expect(elem.text()).toBe('');
//click the element.
elem[0].click();
//test to see if it was updated.
expect(elem.text()).toBe('bar');
});
});
describe('sampleTwo directive', function (){
var scope, html, elem, compiled;
beforeEach(function (){
module('plunker');
html = '<sample-two value="foo"></sample-two>';
inject(function($compile, $rootScope) {
//create the scope.
scope = $rootScope.$new();
//set the test value.
scope.foo = 'bar';
//get the element.
elem = angular.element(html);
//compile the view.
compiled = $compile(elem);
//run the view against the scope.
compiled(scope);
//call digest to update the view!
scope.$digest();
});
});
it('should set text to "bar"', function (){
expect(elem.text()).toBe('bar');
});
it('should set text to whatever is in foo', function (){
//do something to change the value.
scope.foo = 'blah';
//apply it to the view.
scope.$digest();
//test it.
expect(elem.text()).toBe('blah');
})
})
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS test</title>
<link data-require="jasmine" data-semver="1.3.1" rel="stylesheet" href="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css" />
<script data-require="json2" data-semver="0.0.2012100-8" src="//cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.js"></script>
<script data-require="jasmine" data-semver="1.3.1" src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script data-require="jasmine" data-semver="1.3.1" src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<script data-require="angular.js" data-semver="1.1.5" src="http://code.angularjs.org/1.1.5/angular.min.js"></script>
<script data-require="angular-mocks" data-semver="1.1.5" src="http://code.angularjs.org/1.1.5/angular-mocks.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
<script src="appSpec.js"></script>
<script src="jasmineBootstrap.js"></script>
<!-- bootstraps Jasmine -->
</head>
<body>
<div id="HTMLReporter" class="jasmine_reporter"></div>
</body>
</html>
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 250;
/**
Create the `HTMLReporter`, which Jasmine calls to provide results of each spec and each suite. The Reporter is responsible for presenting results to the user.
*/
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
/**
Delegate filtering of specs to the reporter. Allows for clicking on single suites or specs in the results to only run a subset of the suite.
*/
jasmineEnv.specFilter = function(spec) {
return htmlReporter.specFilter(spec);
};
/**
Run all of the tests when the page finishes loading - and make sure to run any previous `onload` handler
### Test Results
Scroll down to see the results of all of these specs.
*/
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
//document.querySelector('.version').innerHTML = jasmineEnv.versionString();
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
/* restore "body" styling that were changes by "jasmine.css"... */
body { background-color: white; padding: 0; margin: 8px; }
/* ... but remain the "jasmine.css" styling for the Jasmine reporting */
.jasmine_reporter { background-color: #eeeeee; padding: 0; margin: 0; }