Alex Rickabaugh ca1e538752 feat(ivy): setClassMetadata() for assigning decorator metadata (#26860)
This commit introduces the setClassMetadata() private function, which
adds metadata to a type in a way that can be accessed via Angular's
ReflectionCapabilities. Currently, it writes to static fields as if
the metadata being added was downleveled from decorators by tsickle.

The plan is for ngtsc to emit code which calls this function, passing
metadata on to the runtime for testing purposes. Calls to this function
would then be tree-shaken away for production bundles.

Testing strategy: proper operation of this function will be an integral
part of TestBed metadata overriding. Angular core tests will fail if this
is broken.

PR Close #26860
2018-10-31 19:52:36 -04:00

67 lines
2.2 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {setClassMetadata} from '../../src/render3/metadata';
import {Type} from '../../src/type';
interface Decorator {
type: any;
args?: any[];
}
interface HasMetadata extends Type<any> {
decorators?: Decorator[];
ctorParameters: {type: any, decorators?: Decorator[]}[];
propDecorators: {[field: string]: Decorator[]};
}
interface CtorParameter {
type: any;
decorators?: Decorator[];
}
function metadataOf(value: Type<any>): HasMetadata {
return value as HasMetadata;
}
describe('render3 setClassMetadata()', () => {
it('should set decorator metadata on a type', () => {
const Foo = metadataOf(class Foo{});
setClassMetadata(Foo, [{type: 'test', args: ['arg']}], null, null);
expect(Foo.decorators).toEqual([{type: 'test', args: ['arg']}]);
});
it('should merge decorator metadata on a type', () => {
const Foo = metadataOf(class Foo{});
Foo.decorators = [{type: 'first'}];
setClassMetadata(Foo, [{type: 'test', args: ['arg']}], null, null);
expect(Foo.decorators).toEqual([{type: 'first'}, {type: 'test', args: ['arg']}]);
});
it('should set ctor parameter metadata on a type', () => {
const Foo = metadataOf(class Foo{});
Foo.ctorParameters = [{type: 'initial'}];
setClassMetadata(Foo, null, [{type: 'test'}], null);
expect(Foo.ctorParameters).toEqual([{type: 'test'}]);
});
it('should set parameter decorator metadata on a type', () => {
const Foo = metadataOf(class Foo{});
setClassMetadata(Foo, null, null, {field: [{type: 'test', args: ['arg']}]});
expect(Foo.propDecorators).toEqual({field: [{type: 'test', args: ['arg']}]});
});
it('should merge parameter decorator metadata on a type', () => {
const Foo = metadataOf(class Foo{});
Foo.propDecorators = {initial: [{type: 'first'}]};
setClassMetadata(Foo, null, null, {field: [{type: 'test', args: ['arg']}]});
expect(Foo.propDecorators)
.toEqual({field: [{type: 'test', args: ['arg']}], initial: [{type: 'first'}]});
});
});