<script src="http://jasmine.github.io/1.3/lib/jasmine.js"></script>
<script src="http://jasmine.github.io/1.3/lib/jasmine-html.js"></script>
<link rel="stylesheet" href="http://jasmine.github.io/1.3/lib/jasmine.css">
<script src="http://code.angularjs.org/1.2.9/angular.js"></script>
<script src="http://code.angularjs.org/1.2.9/angular-mocks.js"></script>
Example of using jasmine and angular using the $timeout. It will allow you to upgrade your jasmine to version 2.0 without problems unlike running with the runs(){} and waits(){} that will have to be rewritten as they are not longer supported.
JavaScript
//--- CODE --------------------------
// Create module
var myApp = angular.module('myApp', []);
myApp.directive('delayedModel', function($timeout) {
return {
scope: {
model: '=delayedModel'
},
link: function(scope, element, attrs) {
element.val(scope.model);
scope.$watch('model', function(newVal, oldVal) {
if (newVal !== oldVal) {
element.val(scope.model);
}
});
var timeout;
element.on('paste', function() {
$timeout(function() {
scope.model = element[0].value;
element.val(scope.model);
scope.$apply();
}, attrs.delay || 500);
});
}
};
});
// ---SPECS-------------------------
describe('Test directive with a timeout', function() {
var $scope, $compile, html, element, $timeout;
beforeEach(module('myApp'));
beforeEach( inject( function ( $rootScope , _$compile_, _$timeout_) {
this.$scope = $rootScope.$new();
this.$compile = _$compile_;
this.$timeout = _$timeout_;
}));
afterEach ( function() {
this.$timeout.verifyNoPendingTasks();
});
describe('using the $timeout functionality', function() {
var html;
beforeEach(function (){
//setup to have a delay of 10 miliseconds.
this.html = '<input delayed-model="query" delay="10" name="Search"></input>';
this.element = this.$compile(this.html)(this.$scope);
//as we have our own scope in the directive we should use our element's scope
this.element.scope().$apply();
});
describe('when we check the delayed search', function () {
//starts off as nothing
it('scope and element value should start off unset and empty', function() {
expect(this.$scope.query).toBeUndefined();
expect(this.element.val()).toEqual('');
expect(this.element.scope().query).toBeUndefined();
});
it('it will trigger only change the value on the scope after the timeout has elapsed',...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.