Skip to content

Commit

Permalink
Merge pull request #8 from abraham/fix-define
Browse files Browse the repository at this point in the history
Fix metadata map getting recreated
  • Loading branch information
abraham authored Jul 24, 2018
2 parents c69d0cd + b32e822 commit e0f6266
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"clean": "rimraf dist",
"prebuild": "npm run clean",
"prepare": "npm run build",
"start": "jest --watch",
"test": "jest"
},
"repository": {
Expand Down
21 changes: 13 additions & 8 deletions src/define-metadata.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { Reflection as Reflect } from './index';

const metadataKey = 'key';
const metadataValue = 'value';
const target = {};

test('with invalid target', () => {
const metadataKey = 'key';
const metadataValue = 'value';
expect(() => Reflect.defineMetadata(metadataKey, metadataValue)).toThrow(TypeError);
});

test('with target but no property key', () => {
const metadataKey = 'key';
const metadataValue = 'value';
const target = {};
expect(() => Reflect.defineMetadata(metadataKey, metadataValue, target)).not.toThrow();
});

test('with target and property key', () => {
const metadataKey = 'key';
const metadataValue = 'value';
const target = {};
const propertyKey = 'name';
expect(() => Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey)).not.toThrow();
});

test('metadata map is reused', () => {
const metadataKey2 = 'key2';
const metadataValue2 = 'value2';
Reflect.defineMetadata(metadataKey, metadataValue, target);
Reflect.defineMetadata(metadataKey2, metadataValue2, target);
expect(Reflect.getOwnMetadata(metadataKey, target)).toEqual(metadataValue);
expect(Reflect.getOwnMetadata(metadataKey2, target)).toEqual(metadataValue2);
});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function decorateProperty(decorators: MemberDecorator[], target: Target, propert
function ordinaryDefineOwnMetadata(metadataKey: MetadataKey, metadataValue: MetadataValue, target: Target, propertyKey?: PropertyKey): void {
if (propertyKey && !['string', 'symbol'].includes(typeof propertyKey)) throw new TypeError();

createMetadataMap(target, propertyKey)
(getMetadataMap(target, propertyKey) || createMetadataMap(target, propertyKey))
.set(metadataKey, metadataValue);
}

Expand Down

0 comments on commit e0f6266

Please sign in to comment.