fix(ivy): warn instead of throwing for unknown properties (#32463)
Logs a warning instead of throwing when running into a binding to an unknown property in JIT mode. Since we aren't using a schema for the runtime validation anymore, this allows us to support browsers where properties are unsupported. PR Close #32463
This commit is contained in:
@ -9,6 +9,7 @@
|
||||
import {CommonModule} from '@angular/common';
|
||||
import {Component, NO_ERRORS_SCHEMA, NgModule} from '@angular/core';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {modifiedInIvy, onlyInIvy} from '@angular/private/testing';
|
||||
|
||||
describe('NgModule', () => {
|
||||
@Component({template: 'hello'})
|
||||
@ -76,33 +77,64 @@ describe('NgModule', () => {
|
||||
});
|
||||
|
||||
describe('schemas', () => {
|
||||
it('should throw on unknown props if NO_ERRORS_SCHEMA is absent', () => {
|
||||
@Component({
|
||||
selector: 'my-comp',
|
||||
template: `
|
||||
<ng-container *ngIf="condition">
|
||||
<div [unknown-prop]="true"></div>
|
||||
</ng-container>
|
||||
`,
|
||||
})
|
||||
class MyComp {
|
||||
condition = true;
|
||||
}
|
||||
onlyInIvy('Unknown property warning logged instead of throwing an error')
|
||||
.it('should throw on unknown props if NO_ERRORS_SCHEMA is absent', () => {
|
||||
@Component({
|
||||
selector: 'my-comp',
|
||||
template: `
|
||||
<ng-container *ngIf="condition">
|
||||
<div [unknown-prop]="true"></div>
|
||||
</ng-container>
|
||||
`,
|
||||
})
|
||||
class MyComp {
|
||||
condition = true;
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
imports: [CommonModule],
|
||||
declarations: [MyComp],
|
||||
})
|
||||
class MyModule {
|
||||
}
|
||||
@NgModule({
|
||||
imports: [CommonModule],
|
||||
declarations: [MyComp],
|
||||
})
|
||||
class MyModule {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({imports: [MyModule]});
|
||||
TestBed.configureTestingModule({imports: [MyModule]});
|
||||
|
||||
expect(() => {
|
||||
const fixture = TestBed.createComponent(MyComp);
|
||||
fixture.detectChanges();
|
||||
}).toThrowError(/Can't bind to 'unknown-prop' since it isn't a known property of 'div'/);
|
||||
});
|
||||
const spy = spyOn(console, 'warn');
|
||||
const fixture = TestBed.createComponent(MyComp);
|
||||
fixture.detectChanges();
|
||||
expect(spy.calls.mostRecent().args[0])
|
||||
.toMatch(/Can't bind to 'unknown-prop' since it isn't a known property of 'div'/);
|
||||
});
|
||||
|
||||
modifiedInIvy('Unknown properties throw an error instead of logging a warning')
|
||||
.it('should throw on unknown props if NO_ERRORS_SCHEMA is absent', () => {
|
||||
@Component({
|
||||
selector: 'my-comp',
|
||||
template: `
|
||||
<ng-container *ngIf="condition">
|
||||
<div [unknown-prop]="true"></div>
|
||||
</ng-container>
|
||||
`,
|
||||
})
|
||||
class MyComp {
|
||||
condition = true;
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
imports: [CommonModule],
|
||||
declarations: [MyComp],
|
||||
})
|
||||
class MyModule {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({imports: [MyModule]});
|
||||
|
||||
expect(() => {
|
||||
const fixture = TestBed.createComponent(MyComp);
|
||||
fixture.detectChanges();
|
||||
}).toThrowError(/Can't bind to 'unknown-prop' since it isn't a known property of 'div'/);
|
||||
});
|
||||
|
||||
it('should not throw on unknown props if NO_ERRORS_SCHEMA is present', () => {
|
||||
@Component({
|
||||
|
Reference in New Issue
Block a user