Skip to content

Commit

Permalink
fix(metadata): add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Feb 5, 2018
1 parent c29dd19 commit de969e4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/metadata/src/decorator-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,14 @@ export class DecoratorFactory<
Reflector.getMetadata(this.key, target),
);
meta = this.mergeWithInherited(meta, target, member, descriptorOrIndex);
/* istanbul ignore if */
if (debug.enabled) {
debug('%s: %j', targetName, meta);
}
Reflector.defineMetadata(this.key, meta, target);
} else {
meta = this.mergeWithOwn(meta, target, member, descriptorOrIndex);
/* istanbul ignore if */
if (debug.enabled) {
debug('%s: %j', targetName, meta);
}
Expand Down
32 changes: 32 additions & 0 deletions packages/metadata/test/unit/decorator-factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,38 @@ describe('ClassDecoratorFactory', () => {
});
});

describe('ClassDecoratorFactory for primitive types', () => {
/**
* Define `@classDecorator(spec)`
* @param spec
*/
function classDecorator(spec: number): ClassDecorator {
return ClassDecoratorFactory.createDecorator('test', spec);
}

const xSpec = 1;
@classDecorator(xSpec)
class BaseController {}

@classDecorator(2)
class SubController extends BaseController {}

it('applies metadata to a class', () => {
const meta = Reflector.getOwnMetadata('test', BaseController);
expect(meta).to.equal(xSpec);
});

it('merges with base class metadata', () => {
const meta = Reflector.getOwnMetadata('test', SubController);
expect(meta).to.equal(2);
});

it('does not mutate base class metadata', () => {
const meta = Reflector.getOwnMetadata('test', BaseController);
expect(meta).to.equal(1);
});
});

describe('ClassDecoratorFactory with create', () => {
interface MySpec {
x?: number;
Expand Down

0 comments on commit de969e4

Please sign in to comment.