2018-10-30 13:16:52 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2018-10-30 13:16:52 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2019-01-09 16:49:16 -05:00
|
|
|
import {Type} from '../../src/interface/type';
|
2018-10-30 13:16:52 -04:00
|
|
|
import {setClassMetadata} from '../../src/render3/metadata';
|
|
|
|
|
|
|
|
interface Decorator {
|
|
|
|
type: any;
|
|
|
|
args?: any[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface HasMetadata extends Type<any> {
|
|
|
|
decorators?: Decorator[];
|
2018-12-09 09:31:55 -05:00
|
|
|
ctorParameters: () => CtorParameter[];
|
2018-10-30 13:16:52 -04:00
|
|
|
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', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const Foo = metadataOf(class Foo {});
|
2018-10-30 13:16:52 -04:00
|
|
|
setClassMetadata(Foo, [{type: 'test', args: ['arg']}], null, null);
|
|
|
|
expect(Foo.decorators).toEqual([{type: 'test', args: ['arg']}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should merge decorator metadata on a type', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const Foo = metadataOf(class Foo {});
|
2018-10-30 13:16:52 -04:00
|
|
|
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', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const Foo = metadataOf(class Foo {});
|
2018-12-09 09:31:55 -05:00
|
|
|
Foo.ctorParameters = () => [{type: 'initial'}];
|
|
|
|
setClassMetadata(Foo, null, () => [{type: 'test'}], null);
|
|
|
|
expect(Foo.ctorParameters()).toEqual([{type: 'test'}]);
|
2018-10-30 13:16:52 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should set parameter decorator metadata on a type', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const Foo = metadataOf(class Foo {});
|
2018-10-30 13:16:52 -04:00
|
|
|
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', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const Foo = metadataOf(class Foo {});
|
2018-10-30 13:16:52 -04:00
|
|
|
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'}]});
|
|
|
|
});
|
|
|
|
});
|