Skip to content

Commit

Permalink
refactor(adapters.nestjs): tuples instead of map when collecting depe…
Browse files Browse the repository at this point in the history
…ndencies

Replace the data structure that stores the class dependencies after reflection.

Closes: #67
  • Loading branch information
omermorad committed Jul 1, 2023
1 parent 96d33a2 commit 8c5c6cb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
3 changes: 3 additions & 0 deletions packages/adapters/nestjs/__test__/integration.assets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { forwardRef, Inject } from '@nestjs/common';

type DummyType = string;

export class DependencyOne {
print(): string {
return 'dependencyOne';
Expand Down Expand Up @@ -30,6 +32,7 @@ export class MainClass {
private readonly dependencyTwo: DependencyTwo,
@Inject(forwardRef(() => DependencyThree)) private readonly dependencyThree: DependencyThree,
@Inject('CUSTOM_TOKEN') private readonly dependencyFour: DependencyFourToken,
@Inject('CUSTOM_TOKEN') private readonly dummy: DummyType,
@Inject('LITERAL_VALUE_ARR') private readonly literalValueArray: string[],
@Inject('LITERAL_VALUE_STR') private readonly literalValueString: string
) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,23 @@ import {
DependencyTwo,
MainClass,
} from './integration.assets';
import { Type } from '@automock/types';
import { ClassDependencies, PrimitiveValue } from '@automock/common';
import { ClassDependenciesMap } from '@automock/common';

describe('NestJS Automock Adapter Integration Test', () => {
describe('reflecting a class', () => {
const reflectorFactory = ReflectorFactory(Reflect, TokensReflector);
const classDependencies = reflectorFactory.reflectDependencies(MainClass);

it('should return a map of the class dependencies', () => {
expect(classDependencies).toStrictEqual<ClassDependencies>(
new Map<Type | string, PrimitiveValue | Type>([
['CUSTOM_TOKEN', DependencyFourToken],
[DependencyOne, DependencyOne],
[DependencyTwo, DependencyTwo],
[DependencyThree, DependencyThree],
['LITERAL_VALUE_ARR', Array],
['LITERAL_VALUE_STR', String],
])
);
expect(classDependencies.constructor).toStrictEqual<ClassDependenciesMap['constructor']>([
[DependencyOne, DependencyOne],
[DependencyTwo, DependencyTwo],
[DependencyThree, DependencyThree],
['CUSTOM_TOKEN', DependencyFourToken],
['CUSTOM_TOKEN', String],
['LITERAL_VALUE_ARR', Array],
['LITERAL_VALUE_STR', String],
]);
});
});
});
18 changes: 6 additions & 12 deletions packages/adapters/nestjs/src/reflector.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Type } from '@automock/types';
import {
ClassDependencies,
DependenciesReflector as AutomockDependenciesReflector,
PrimitiveValue,
} from '@automock/common';
import { ClassDependencies, ClassDependenciesMap } from '@automock/common';
import { DependenciesReflector as AutomockDependenciesReflector } from '@automock/common';
import { CustomToken, TokensReflector } from './token-reflector.service';

const INJECTED_TOKENS_METADATA = 'self:paramtypes';
Expand All @@ -13,10 +10,10 @@ export function ReflectorFactory(
reflector: typeof Reflect,
tokensReflector: TokensReflector
): AutomockDependenciesReflector {
function reflectDependencies(targetClass: Type): ClassDependencies {
function reflectDependencies(targetClass: Type): ClassDependenciesMap {
const types = reflectParamTypes(targetClass);
const tokens = reflectParamTokens(targetClass);
const classDependencies: ClassDependencies = new Map<Type | string, PrimitiveValue | Type>();
const classDependencies: ClassDependencies = [];

const callback = tokensReflector.attachTokenToDependency(tokens);

Expand All @@ -30,12 +27,9 @@ export function ReflectorFactory(
);
}
})
.forEach((tuple) => {
const [typeOrToken, type] = tuple;
classDependencies.set(typeOrToken, type);
});
.forEach((tuple) => classDependencies.push(tuple));

return classDependencies;
return { constructor: classDependencies };
}

function reflectParamTokens(targetClass: Type): CustomToken[] {
Expand Down

0 comments on commit 8c5c6cb

Please sign in to comment.