Skip to content

Commit c631cfc

Browse files
vsavkinkara
authored andcommitted
feat(core): add NO_ERRORS_SCHEMA that allows any properties to be set on any element (angular#10956)
Often it is useful to test a component without rendering certain directives/components in its template because these directives require some complicated setup. You can do that by using NO_ERRORS_SCHEMA. TestBed.configureTestingModule({ schemas: [NO_ERRORS_SCHEMA] }); This would disable all schema checks in your tests.
1 parent 53c99cf commit c631cfc

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

modules/@angular/compiler/src/schema/dom_element_schema_registry.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {CUSTOM_ELEMENTS_SCHEMA, Injectable, SchemaMetadata, SecurityContext} from '@angular/core';
9+
import {CUSTOM_ELEMENTS_SCHEMA, Injectable, NO_ERRORS_SCHEMA, SchemaMetadata, SecurityContext} from '@angular/core';
1010

1111
import {StringMapWrapper} from '../facade/collection';
1212
import {isPresent} from '../facade/lang';
@@ -270,6 +270,10 @@ export class DomElementSchemaRegistry extends ElementSchemaRegistry {
270270
}
271271

272272
hasProperty(tagName: string, propName: string, schemaMetas: SchemaMetadata[]): boolean {
273+
if (schemaMetas.some((schema) => schema.name === NO_ERRORS_SCHEMA.name)) {
274+
return true;
275+
}
276+
273277
if (tagName.indexOf('-') !== -1) {
274278
if (tagName === 'ng-container' || tagName === 'ng-content') {
275279
return false;
@@ -280,6 +284,7 @@ export class DomElementSchemaRegistry extends ElementSchemaRegistry {
280284
return true;
281285
}
282286
}
287+
283288
var elementProperties = this.schema[tagName.toLowerCase()];
284289
if (!isPresent(elementProperties)) {
285290
elementProperties = this.schema['unknown'];

modules/@angular/compiler/test/schema/dom_element_schema_registry_spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {DomElementSchemaRegistry} from '@angular/compiler/src/schema/dom_element_schema_registry';
10-
import {CUSTOM_ELEMENTS_SCHEMA, SecurityContext} from '@angular/core';
10+
import {CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, SecurityContext} from '@angular/core';
1111
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
1212
import {browserDetection} from '@angular/platform-browser/testing/browser_util';
1313

@@ -59,6 +59,11 @@ export function main() {
5959
expect(registry.hasProperty('custom-like', 'unknown', [CUSTOM_ELEMENTS_SCHEMA])).toBeTruthy();
6060
});
6161

62+
it('should return true for all elements if the NO_ERRORS_SCHEMA was used', () => {
63+
expect(registry.hasProperty('custom-like', 'unknown', [NO_ERRORS_SCHEMA])).toBeTruthy();
64+
expect(registry.hasProperty('a', 'unknown', [NO_ERRORS_SCHEMA])).toBeTruthy();
65+
});
66+
6267
it('should re-map property names that are specified in DOM facade',
6368
() => { expect(registry.getMappedPropName('readonly')).toEqual('readOnly'); });
6469

modules/@angular/core/src/metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {TypeDecorator, makeDecorator, makeParamDecorator, makePropDecorator} fro
2121
export {ANALYZE_FOR_ENTRY_COMPONENTS, AttributeMetadata, ContentChildMetadata, ContentChildrenMetadata, QueryMetadata, ViewChildMetadata, ViewChildrenMetadata, ViewQueryMetadata} from './metadata/di';
2222
export {ComponentMetadata, ComponentMetadataType, DirectiveMetadata, DirectiveMetadataType, HostBindingMetadata, HostListenerMetadata, InputMetadata, OutputMetadata, PipeMetadata, PipeMetadataType} from './metadata/directives';
2323
export {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit} from './metadata/lifecycle_hooks';
24-
export {CUSTOM_ELEMENTS_SCHEMA, ModuleWithProviders, NgModuleMetadata, NgModuleMetadataType, SchemaMetadata} from './metadata/ng_module';
24+
export {CUSTOM_ELEMENTS_SCHEMA, ModuleWithProviders, NO_ERRORS_SCHEMA, NgModuleMetadata, NgModuleMetadataType, SchemaMetadata} from './metadata/ng_module';
2525
export {ViewEncapsulation} from './metadata/view';
2626

2727

modules/@angular/core/src/metadata/ng_module.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ export const CUSTOM_ELEMENTS_SCHEMA: SchemaMetadata = {
3636
name: 'custom-elements'
3737
};
3838

39+
/**
40+
* Defines a schema that will allow any property on any element.
41+
*
42+
* @experimental
43+
*/
44+
export const NO_ERRORS_SCHEMA: SchemaMetadata = {
45+
name: 'no-errors-schema'
46+
};
47+
3948
/**
4049
* Interface for creating {@link NgModuleMetadata}
4150
* @experimental

tools/public_api_guard/core/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,9 @@ export declare class NgZone {
840840
static isInAngularZone(): boolean;
841841
}
842842

843+
/** @experimental */
844+
export declare const NO_ERRORS_SCHEMA: SchemaMetadata;
845+
843846
/** @stable */
844847
export declare class NoAnnotationError extends BaseException {
845848
constructor(typeOrFunc: Type<any> | Function, params: any[][]);

0 commit comments

Comments
 (0)