refactor(forms): ngForm element selector has been deprecated in favor of ng-form (#23721)

This has been deprecated to keep selector consistent with other core Angular selectors.  As element selectors are in kebab-case.

 Now deprecated:
 ```
 <ngForm #myForm="ngForm">
 ```

 After:
 ```
 <ng-form #myForm="ngForm">
 ```

You can also choose to supress this warnings by providing a config for `FormsModule` during import:

```ts
imports: [
 FormsModule.withConfig({warnOnDeprecatedNgFormSelector: 'never'});
]

Closes: #23678

PR Close #23721
This commit is contained in:
Alan
2018-08-01 07:17:58 +02:00
committed by Igor Minar
parent 5982425436
commit 3ba5220839
8 changed files with 153 additions and 8 deletions

View File

@ -8,7 +8,7 @@
import {Component, Directive, Type, forwardRef} from '@angular/core';
import {ComponentFixture, TestBed, async, fakeAsync, tick} from '@angular/core/testing';
import {AbstractControl, AsyncValidator, COMPOSITION_BUFFER_MODE, FormControl, FormsModule, NG_ASYNC_VALIDATORS, NgForm, NgModel} from '@angular/forms';
import {AbstractControl, AsyncValidator, COMPOSITION_BUFFER_MODE, FormControl, FormsModule, NG_ASYNC_VALIDATORS, NgForm, NgFormSelectorWarning, NgModel} from '@angular/forms';
import {By} from '@angular/platform-browser/src/dom/debug/by';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {dispatchEvent} from '@angular/platform-browser/testing/src/browser_util';
@ -1630,6 +1630,61 @@ import {NgModelCustomComp, NgModelCustomWrapper} from './value_accessor_integrat
}));
});
describe('ngForm deprecation warnings', () => {
let warnSpy: jasmine.Spy;
@Component({selector: 'ng-form-deprecated', template: `<ngForm></ngForm><ngForm></ngForm>`})
class ngFormDeprecated {
}
beforeEach(() => {
(NgFormSelectorWarning as any)._ngFormWarning = false;
warnSpy = spyOn(console, 'warn');
});
describe(`when using the deprecated 'ngForm' selector`, () => {
it(`should only warn once when global provider is provided with "once"`, () => {
TestBed.configureTestingModule({
declarations: [ngFormDeprecated],
imports: [FormsModule.withConfig({warnOnDeprecatedNgFormSelector: 'once'})]
});
TestBed.createComponent(ngFormDeprecated);
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.calls.mostRecent().args[0])
.toMatch(/It looks like you're using 'ngForm'/gi);
});
it(`should only warn once by default`, () => {
initTest(ngFormDeprecated);
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.calls.mostRecent().args[0])
.toMatch(/It looks like you're using 'ngForm'/gi);
});
it(`should not warn when global provider is provided with "never"`, () => {
TestBed.configureTestingModule({
declarations: [ngFormDeprecated],
imports: [FormsModule.withConfig({warnOnDeprecatedNgFormSelector: 'never'})]
});
TestBed.createComponent(ngFormDeprecated);
expect(warnSpy).not.toHaveBeenCalled();
});
it(`should only warn for each instance when global provider is provided with "always"`,
() => {
TestBed.configureTestingModule({
declarations: [ngFormDeprecated],
imports: [FormsModule.withConfig({warnOnDeprecatedNgFormSelector: 'always'})]
});
TestBed.createComponent(ngFormDeprecated);
expect(warnSpy).toHaveBeenCalledTimes(2);
expect(warnSpy.calls.mostRecent().args[0])
.toMatch(/It looks like you're using 'ngForm'/gi);
});
});
});
});
}
@ -1845,7 +1900,7 @@ class NgModelAsyncValidation {
selector: 'ng-model-changes-form',
template: `
<form>
<input name="async" [ngModel]="name" (ngModelChange)="log()"
<input name="async" [ngModel]="name" (ngModelChange)="log()"
[ngModelOptions]="options">
</form>
`