Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix metadata map getting recreated #8

Merged
merged 2 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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