Skip to content

Commit

Permalink
refactor(core): add optional decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
notaphplover committed Nov 17, 2024
1 parent 18ad2de commit a8966d8
Show file tree
Hide file tree
Showing 3 changed files with 450 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { beforeAll, describe, expect, it } from '@jest/globals';

import 'reflect-metadata';

import { getReflectMetadata } from '@inversifyjs/reflect-metadata-utils';

import { classMetadataReflectKey } from '../../reflectMetadata/data/classMetadataReflectKey';
import { MaybeClassElementMetadataKind } from '../models/MaybeClassElementMetadataKind';
import { MaybeClassMetadata } from '../models/MaybeClassMetadata';
import { optional } from './optional';

describe(optional.name, () => {
describe('when called', () => {
let result: unknown;

beforeAll(() => {
class Foo {
@optional()
public readonly bar!: string;

@optional()
public readonly baz!: string;

constructor(
@optional()
public firstParam: number,
@optional()
public secondParam: number,
) {}
}

result = getReflectMetadata(Foo, classMetadataReflectKey);
});

it('should return expected metadata', () => {
const expected: MaybeClassMetadata = {
constructorArguments: [
{
kind: MaybeClassElementMetadataKind.unknown,
name: undefined,
optional: true,
tags: new Map(),
targetName: undefined,
},
{
kind: MaybeClassElementMetadataKind.unknown,
name: undefined,
optional: true,
tags: new Map(),
targetName: undefined,
},
],
lifecycle: {
postConstructMethodName: undefined,
preDestroyMethodName: undefined,
},
properties: new Map([
[
'bar',
{
kind: MaybeClassElementMetadataKind.unknown,
name: undefined,
optional: true,
tags: new Map(),
targetName: undefined,
},
],
[
'baz',
{
kind: MaybeClassElementMetadataKind.unknown,
name: undefined,
optional: true,
tags: new Map(),
targetName: undefined,
},
],
]),
};

expect(result).toStrictEqual(expected);
});
});
});
Loading

0 comments on commit a8966d8

Please sign in to comment.