feat(core): add NO_ERRORS_SCHEMA that allows any properties to be set on any element (#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.
This commit is contained in:
Victor Savkin 2016-08-19 16:05:34 -07:00 committed by Kara
parent 53c99cfc95
commit c631cfc2fd
5 changed files with 25 additions and 3 deletions

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {CUSTOM_ELEMENTS_SCHEMA, Injectable, SchemaMetadata, SecurityContext} from '@angular/core'; import {CUSTOM_ELEMENTS_SCHEMA, Injectable, NO_ERRORS_SCHEMA, SchemaMetadata, SecurityContext} from '@angular/core';
import {StringMapWrapper} from '../facade/collection'; import {StringMapWrapper} from '../facade/collection';
import {isPresent} from '../facade/lang'; import {isPresent} from '../facade/lang';
@ -270,6 +270,10 @@ export class DomElementSchemaRegistry extends ElementSchemaRegistry {
} }
hasProperty(tagName: string, propName: string, schemaMetas: SchemaMetadata[]): boolean { hasProperty(tagName: string, propName: string, schemaMetas: SchemaMetadata[]): boolean {
if (schemaMetas.some((schema) => schema.name === NO_ERRORS_SCHEMA.name)) {
return true;
}
if (tagName.indexOf('-') !== -1) { if (tagName.indexOf('-') !== -1) {
if (tagName === 'ng-container' || tagName === 'ng-content') { if (tagName === 'ng-container' || tagName === 'ng-content') {
return false; return false;
@ -280,6 +284,7 @@ export class DomElementSchemaRegistry extends ElementSchemaRegistry {
return true; return true;
} }
} }
var elementProperties = this.schema[tagName.toLowerCase()]; var elementProperties = this.schema[tagName.toLowerCase()];
if (!isPresent(elementProperties)) { if (!isPresent(elementProperties)) {
elementProperties = this.schema['unknown']; elementProperties = this.schema['unknown'];

View File

@ -7,7 +7,7 @@
*/ */
import {DomElementSchemaRegistry} from '@angular/compiler/src/schema/dom_element_schema_registry'; import {DomElementSchemaRegistry} from '@angular/compiler/src/schema/dom_element_schema_registry';
import {CUSTOM_ELEMENTS_SCHEMA, SecurityContext} from '@angular/core'; import {CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, SecurityContext} from '@angular/core';
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal'; import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
import {browserDetection} from '@angular/platform-browser/testing/browser_util'; import {browserDetection} from '@angular/platform-browser/testing/browser_util';
@ -59,6 +59,11 @@ export function main() {
expect(registry.hasProperty('custom-like', 'unknown', [CUSTOM_ELEMENTS_SCHEMA])).toBeTruthy(); expect(registry.hasProperty('custom-like', 'unknown', [CUSTOM_ELEMENTS_SCHEMA])).toBeTruthy();
}); });
it('should return true for all elements if the NO_ERRORS_SCHEMA was used', () => {
expect(registry.hasProperty('custom-like', 'unknown', [NO_ERRORS_SCHEMA])).toBeTruthy();
expect(registry.hasProperty('a', 'unknown', [NO_ERRORS_SCHEMA])).toBeTruthy();
});
it('should re-map property names that are specified in DOM facade', it('should re-map property names that are specified in DOM facade',
() => { expect(registry.getMappedPropName('readonly')).toEqual('readOnly'); }); () => { expect(registry.getMappedPropName('readonly')).toEqual('readOnly'); });

View File

@ -21,7 +21,7 @@ import {TypeDecorator, makeDecorator, makeParamDecorator, makePropDecorator} fro
export {ANALYZE_FOR_ENTRY_COMPONENTS, AttributeMetadata, ContentChildMetadata, ContentChildrenMetadata, QueryMetadata, ViewChildMetadata, ViewChildrenMetadata, ViewQueryMetadata} from './metadata/di'; export {ANALYZE_FOR_ENTRY_COMPONENTS, AttributeMetadata, ContentChildMetadata, ContentChildrenMetadata, QueryMetadata, ViewChildMetadata, ViewChildrenMetadata, ViewQueryMetadata} from './metadata/di';
export {ComponentMetadata, ComponentMetadataType, DirectiveMetadata, DirectiveMetadataType, HostBindingMetadata, HostListenerMetadata, InputMetadata, OutputMetadata, PipeMetadata, PipeMetadataType} from './metadata/directives'; export {ComponentMetadata, ComponentMetadataType, DirectiveMetadata, DirectiveMetadataType, HostBindingMetadata, HostListenerMetadata, InputMetadata, OutputMetadata, PipeMetadata, PipeMetadataType} from './metadata/directives';
export {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit} from './metadata/lifecycle_hooks'; export {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit} from './metadata/lifecycle_hooks';
export {CUSTOM_ELEMENTS_SCHEMA, ModuleWithProviders, NgModuleMetadata, NgModuleMetadataType, SchemaMetadata} from './metadata/ng_module'; export {CUSTOM_ELEMENTS_SCHEMA, ModuleWithProviders, NO_ERRORS_SCHEMA, NgModuleMetadata, NgModuleMetadataType, SchemaMetadata} from './metadata/ng_module';
export {ViewEncapsulation} from './metadata/view'; export {ViewEncapsulation} from './metadata/view';

View File

@ -36,6 +36,15 @@ export const CUSTOM_ELEMENTS_SCHEMA: SchemaMetadata = {
name: 'custom-elements' name: 'custom-elements'
}; };
/**
* Defines a schema that will allow any property on any element.
*
* @experimental
*/
export const NO_ERRORS_SCHEMA: SchemaMetadata = {
name: 'no-errors-schema'
};
/** /**
* Interface for creating {@link NgModuleMetadata} * Interface for creating {@link NgModuleMetadata}
* @experimental * @experimental

View File

@ -840,6 +840,9 @@ export declare class NgZone {
static isInAngularZone(): boolean; static isInAngularZone(): boolean;
} }
/** @experimental */
export declare const NO_ERRORS_SCHEMA: SchemaMetadata;
/** @stable */ /** @stable */
export declare class NoAnnotationError extends BaseException { export declare class NoAnnotationError extends BaseException {
constructor(typeOrFunc: Type<any> | Function, params: any[][]); constructor(typeOrFunc: Type<any> | Function, params: any[][]);