-describe('crmAutosave', function() {
+'use strict';
- function using(name, values, func) {
- for (var i = 0, count = values.length; i < count; i++) {
- if (Object.prototype.toString.call(values[i]) !== '[object Array]') {
- values[i] = [values[i]];
- }
- func.apply(this, values[i]);
- jasmine.currentEnv_.currentSpec.description += ' (with "' + name + '" using ' + values[i].join(', ') + ')';
- }
- }
+describe('crmAutosave', function() {
beforeEach(function() {
module('crmUtil');
$interval,
$timeout,
fakeCtrl,
+ CrmAutosaveCtrl,
model,
element;
- beforeEach(inject(function(_$compile_, _$rootScope_, _$interval_, _$timeout_, _$q_) {
+ beforeEach(inject(function(_$compile_, _$rootScope_, _$interval_, _$timeout_, _$q_, _CrmAutosaveCtrl_) {
// The injector unwraps the underscores (_) from around the parameter names when matching
$compile = _$compile_;
$rootScope = _$rootScope_;
$interval = _$interval_;
$timeout = _$timeout_;
+ CrmAutosaveCtrl = _CrmAutosaveCtrl_;
$rootScope.fakeCtrl = fakeCtrl = {
doSave: function() {
};
angular.forEach(fakeSaves, function(saveFunc, saveFuncExpr) {
it('calls ' + saveFuncExpr + ' twice over the course of three changes', function() {
- element = $compile('<form name="myForm" crm-autosave="' + saveFuncExpr + '" crm-autosave-model="model" crm-autosave-interval="{poll: 25, save: 50}"><input class="fieldA" ng-model="model.fieldA"><input class="fieldB" ng-model="model.fieldB"></form>')($rootScope);
+ var myAutosave = $rootScope.myAutosave = new CrmAutosaveCtrl({
+ save: fakeCtrl[saveFunc],
+ model: function(){ return model; },
+ interval: {poll: 25, save: 50},
+ form: function(){ return $rootScope.myForm; }
+ });
+ myAutosave.start();
+ $rootScope.$on('$destroy', myAutosave.stop);
+ element = $compile('<form name="myForm"><input class="fieldA" ng-model="model.fieldA"><input class="fieldB" ng-model="model.fieldB"></form>')($rootScope);
$rootScope.$digest();
// check that we load pristine data and don't do any saving
});
it('does not save an invalid form', function() {
- element = $compile('<form name="myForm" crm-autosave="fakeCtrl.doSave()" crm-autosave-model="model" crm-autosave-interval="{poll: 25, save: 50}"><input class="fieldA" ng-model="model.fieldA"><input class="fieldB" required ng-model="model.fieldB"></form>')($rootScope);
+ var myAutosave = $rootScope.myAutosave = new CrmAutosaveCtrl({
+ save: fakeCtrl.doSave,
+ model: function(){ return model; },
+ interval: {poll: 25, save: 50},
+ form: function(){ return $rootScope.myForm; }
+ });
+ myAutosave.start();
+ $rootScope.$on('$destroy', myAutosave.stop);
+ element = $compile('<form name="myForm"><input class="fieldA" ng-model="model.fieldA"><input class="fieldB" required ng-model="model.fieldB"></form>')($rootScope);
$rootScope.$digest();
// check that we load pristine data and don't do any saving
});
it('defers saving new changes when a save is already pending', function() {
- element = $compile('<form name="myForm" crm-autosave="fakeCtrl.doSaveSlowly()" crm-autosave-model="model" crm-autosave-interval="{poll: 25, save: 50}"><input class="fieldA" ng-model="model.fieldA"><input class="fieldB" ng-model="model.fieldB"></form>')($rootScope);
+ var myAutosave = $rootScope.myAutosave = new CrmAutosaveCtrl({
+ save: fakeCtrl.doSaveSlowly,
+ model: function(){ return model; },
+ interval: {poll: 25, save: 50},
+ form: function(){ return $rootScope.myForm; }
+ });
+ myAutosave.start();
+ $rootScope.$on('$destroy', myAutosave.stop);
+ element = $compile('<form name="myForm"><input class="fieldA" ng-model="model.fieldA"><input class="fieldB" ng-model="model.fieldB"></form>')($rootScope);
$rootScope.$digest();
// check that we load pristine data and don't do any saving