/* global FormController: false */ 'use strict'; describe('form', function() { var doc, control, scope, $compile, changeInputValue; beforeEach(module(function($compileProvider) { $compileProvider.directive('storeModelCtrl', function() { return { require: 'ngModel', link: function(scope, elm, attr, ctrl) { control = ctrl; } }; }); })); beforeEach(inject(function($injector, $sniffer) { $compile = $injector.get('$compile'); scope = $injector.get('$rootScope'); changeInputValue = function(elm, value) { elm.val(value); browserTrigger(elm, $sniffer.hasEvent('input') ? 'input' : 'change'); }; })); afterEach(function() { dealoc(doc); }); it('should instantiate form and attach it to DOM', function() { doc = $compile('
')(scope); scope.inputPresent = true; scope.$digest(); var form = scope.myForm; control.$setValidity('required', false); expect(form.alias).toBe(control); expect(form.$error.required).toEqual([control]); // remove nested control scope.inputPresent = false; scope.$apply(); expect(form.$error.required).toBeFalsy(); expect(form.alias).toBeUndefined(); }); it('should ignore changes in manually removed controls', function() { doc = $compile( '')(scope); var form = scope.myForm; var input = doc.find('input').eq(0); var inputController = input.controller('ngModel'); changeInputValue(input, 'ab'); scope.$apply(); expect(form.$error.maxlength).toBeTruthy(); expect(form.$dirty).toBe(true); expect(form.$error.maxlength[0].$name).toBe('control'); // remove control form.$removeControl(form.control); expect(form.control).toBeUndefined(); expect(form.$error.maxlength).toBeFalsy(); inputController.$setPristine(); expect(form.$dirty).toBe(true); form.$setPristine(); changeInputValue(input, 'abc'); scope.$apply(); expect(form.$error.maxlength).toBeFalsy(); expect(form.$dirty).toBe(false); }); it('should react to validation changes in manually added controls', function() { doc = $compile( '')(scope); scope.$digest(); var form = scope.myForm; var input = doc.find('input').eq(0); // remove control and invalidate it form.$removeControl(control); expect(form.control).toBeUndefined(); changeInputValue(input, 'abc'); expect(control.$error.maxlength).toBe(true); expect(control.$dirty).toBe(true); expect(form.$error.maxlength).toBeFalsy(); expect(form.$dirty).toBe(false); // re-add the control; its current validation state is not propagated form.$addControl(control); expect(form.control).toBe(control); expect(form.$error.maxlength).toBeFalsy(); expect(form.$dirty).toBe(false); // Only when the input changes again its validation state is propagated changeInputValue(input, 'abcd'); expect(form.$error.maxlength[0]).toBe(control); expect(form.$dirty).toBe(false); }); it('should use the correct parent when renaming and removing dynamically added controls', function() { scope.controlName = 'childControl'; scope.hasChildControl = true; doc = $compile( '' + '')(scope); scope.$digest(); var form = scope.myForm; var otherForm = scope.otherForm; var childControl = form.childControl; // remove child form and add it to another form form.$removeControl(childControl); otherForm.$addControl(childControl); expect(form.childControl).toBeUndefined(); expect(otherForm.childControl).toBe(childControl); // rename the childControl scope.controlName = 'childControlMoved'; scope.$digest(); expect(form.childControlMoved).toBeUndefined(); expect(otherForm.childControl).toBeUndefined(); expect(otherForm.childControlMoved).toBe(childControl); scope.hasChildControl = false; scope.$digest(); expect(form.childControlMoved).toBeUndefined(); expect(otherForm.childControlMoved).toBeUndefined(); }); it('should remove scope reference when form with no parent form is removed from the DOM', function() { var formController; scope.ctrl = {}; doc = $compile( '')(scope); scope.$digest(); expect(scope.ctrl.myForm).toBeUndefined(); scope.$apply('formPresent = true'); expect(scope.ctrl.myForm).toBeDefined(); formController = doc.find('form').controller('form'); expect(scope.ctrl.myForm).toBe(formController); scope.$apply('formPresent = false'); expect(scope.ctrl.myForm).toBeUndefined(); }); it('should use ngForm value as form name', function() { doc = $compile( '