2016-07-28 07:54:49 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2017-12-17 18:10:54 -05:00
|
|
|
import {MetadataOverrider} from '@angular/platform-browser-dynamic/testing/src/metadata_overrider';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
2016-07-28 07:54:49 -04:00
|
|
|
|
|
|
|
interface SomeMetadataType {
|
|
|
|
plainProp?: string;
|
|
|
|
getterProp?: string;
|
|
|
|
arrayProp?: any[];
|
|
|
|
}
|
|
|
|
|
2016-08-12 20:39:33 -04:00
|
|
|
interface OtherMetadataType extends SomeMetadataType {
|
|
|
|
otherPlainProp?: string;
|
|
|
|
}
|
|
|
|
|
2016-07-28 07:54:49 -04:00
|
|
|
class SomeMetadata implements SomeMetadataType {
|
|
|
|
plainProp: string;
|
|
|
|
private _getterProp: string;
|
|
|
|
arrayProp: any[];
|
|
|
|
|
|
|
|
constructor(options: SomeMetadataType) {
|
2020-04-13 19:40:21 -04:00
|
|
|
this.plainProp = options.plainProp!;
|
|
|
|
this._getterProp = options.getterProp!;
|
|
|
|
this.arrayProp = options.arrayProp!;
|
2019-02-08 17:01:51 -05:00
|
|
|
Object.defineProperty(this, 'getterProp', {
|
|
|
|
enumerable: true, // getters are non-enumerable by default in es2015
|
|
|
|
get: () => this._getterProp,
|
|
|
|
});
|
2016-07-28 07:54:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 20:39:33 -04:00
|
|
|
class OtherMetadata extends SomeMetadata implements OtherMetadataType {
|
|
|
|
otherPlainProp: string;
|
|
|
|
|
|
|
|
constructor(options: OtherMetadataType) {
|
|
|
|
super({
|
|
|
|
plainProp: options.plainProp,
|
|
|
|
getterProp: options.getterProp,
|
|
|
|
arrayProp: options.arrayProp
|
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
this.otherPlainProp = options.otherPlainProp!;
|
2016-08-12 20:39:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2016-07-28 07:54:49 -04:00
|
|
|
describe('metadata overrider', () => {
|
|
|
|
let overrider: MetadataOverrider;
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
overrider = new MetadataOverrider();
|
|
|
|
});
|
2016-07-28 07:54:49 -04:00
|
|
|
|
|
|
|
it('should return a new instance with the same values', () => {
|
|
|
|
const oldInstance = new SomeMetadata({plainProp: 'somePlainProp', getterProp: 'someInput'});
|
|
|
|
const newInstance = overrider.overrideMetadata(SomeMetadata, oldInstance, {});
|
|
|
|
expect(newInstance).not.toBe(oldInstance);
|
|
|
|
expect(newInstance).toBeAnInstanceOf(SomeMetadata);
|
|
|
|
expect(newInstance).toEqual(oldInstance);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set individual properties and keep others', () => {
|
|
|
|
const oldInstance =
|
|
|
|
new SomeMetadata({plainProp: 'somePlainProp', getterProp: 'someGetterProp'});
|
|
|
|
const newInstance =
|
|
|
|
overrider.overrideMetadata(SomeMetadata, oldInstance, {set: {plainProp: 'newPlainProp'}});
|
|
|
|
expect(newInstance)
|
|
|
|
.toEqual(new SomeMetadata({plainProp: 'newPlainProp', getterProp: 'someGetterProp'}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('add properties', () => {
|
|
|
|
it('should replace non array values', () => {
|
|
|
|
const oldInstance =
|
|
|
|
new SomeMetadata({plainProp: 'somePlainProp', getterProp: 'someGetterProp'});
|
|
|
|
const newInstance = overrider.overrideMetadata(
|
|
|
|
SomeMetadata, oldInstance, {add: {plainProp: 'newPlainProp'}});
|
|
|
|
expect(newInstance)
|
|
|
|
.toEqual(new SomeMetadata({plainProp: 'newPlainProp', getterProp: 'someGetterProp'}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add to array values', () => {
|
|
|
|
const oldInstance = new SomeMetadata({arrayProp: ['a']});
|
|
|
|
const newInstance =
|
|
|
|
overrider.overrideMetadata(SomeMetadata, oldInstance, {add: {arrayProp: ['b']}});
|
|
|
|
expect(newInstance).toEqual(new SomeMetadata({arrayProp: ['a', 'b']}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('remove', () => {
|
|
|
|
it('should set values to undefined if their value matches', () => {
|
|
|
|
const oldInstance =
|
|
|
|
new SomeMetadata({plainProp: 'somePlainProp', getterProp: 'someGetterProp'});
|
|
|
|
const newInstance = overrider.overrideMetadata(
|
|
|
|
SomeMetadata, oldInstance, {remove: {plainProp: 'somePlainProp'}});
|
|
|
|
expect(newInstance)
|
|
|
|
.toEqual(new SomeMetadata({plainProp: undefined, getterProp: 'someGetterProp'}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should leave values if their value does not match', () => {
|
|
|
|
const oldInstance =
|
|
|
|
new SomeMetadata({plainProp: 'somePlainProp', getterProp: 'someGetterProp'});
|
|
|
|
const newInstance = overrider.overrideMetadata(
|
|
|
|
SomeMetadata, oldInstance, {remove: {plainProp: 'newPlainProp'}});
|
|
|
|
expect(newInstance)
|
|
|
|
.toEqual(new SomeMetadata({plainProp: 'somePlainProp', getterProp: 'someGetterProp'}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove a value from an array', () => {
|
|
|
|
const oldInstance =
|
|
|
|
new SomeMetadata({arrayProp: ['a', 'b', 'c'], getterProp: 'someGetterProp'});
|
|
|
|
const newInstance = overrider.overrideMetadata(
|
|
|
|
SomeMetadata, oldInstance, {remove: {arrayProp: ['a', 'c']}});
|
|
|
|
expect(newInstance)
|
|
|
|
.toEqual(new SomeMetadata({arrayProp: ['b'], getterProp: 'someGetterProp'}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support types as values', () => {
|
|
|
|
class Class1 {}
|
|
|
|
class Class2 {}
|
|
|
|
class Class3 {}
|
|
|
|
|
|
|
|
const instance1 = new SomeMetadata({arrayProp: [Class1, Class2, Class3]});
|
|
|
|
const instance2 =
|
|
|
|
overrider.overrideMetadata(SomeMetadata, instance1, {remove: {arrayProp: [Class1]}});
|
|
|
|
expect(instance2).toEqual(new SomeMetadata({arrayProp: [Class2, Class3]}));
|
|
|
|
const instance3 =
|
|
|
|
overrider.overrideMetadata(SomeMetadata, instance2, {remove: {arrayProp: [Class3]}});
|
|
|
|
expect(instance3).toEqual(new SomeMetadata({arrayProp: [Class2]}));
|
|
|
|
});
|
|
|
|
});
|
2016-08-12 20:39:33 -04:00
|
|
|
|
|
|
|
describe('subclasses', () => {
|
|
|
|
it('should set individual properties and keep others', () => {
|
|
|
|
const oldInstance = new OtherMetadata({
|
|
|
|
plainProp: 'somePlainProp',
|
|
|
|
getterProp: 'someGetterProp',
|
|
|
|
otherPlainProp: 'newOtherProp'
|
|
|
|
});
|
|
|
|
const newInstance = overrider.overrideMetadata(
|
|
|
|
OtherMetadata, oldInstance, {set: {plainProp: 'newPlainProp'}});
|
|
|
|
expect(newInstance).toEqual(new OtherMetadata({
|
|
|
|
plainProp: 'newPlainProp',
|
|
|
|
getterProp: 'someGetterProp',
|
|
|
|
otherPlainProp: 'newOtherProp'
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
2016-07-28 07:54:49 -04:00
|
|
|
});
|
|
|
|
}
|