Skip to content

Commit

Permalink
feat(adapters.nestjs): support primitive value type in dependencies (#56
Browse files Browse the repository at this point in the history
)
  • Loading branch information
omermorad committed Jun 9, 2023
1 parent 7dce4b3 commit ce2e779
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 10 deletions.
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default async (): Promise<Config.InitialOptions> => {
},
projects: [
'<rootDir>/packages/core',
'<rootDir>/packages/reflectors/nestjs',
'<rootDir>/packages/adapters/nestjs',
'<rootDir>/packages/testbeds/jest',
],
};
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"useWorkspaces": true,
"packages": [
"packages/*",
"packages/reflectors/*",
"packages/adapters/*",
"packages/testbeds/*"
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"workspaces": [
"packages/*",
"packages/reflectors/*",
"packages/adapters/*",
"packages/testbeds/*"
],
"lint-staged": {
Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions packages/adapters/nestjs/__test__/integration.assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { forwardRef, Inject } from '@nestjs/common';

export class DependencyOne {
print(): string {
return 'dependencyOne';
}
}

export class DependencyTwo {
print(): string {
return 'dependencyTwo';
}
}

export class DependencyThree {
print(): string {
return 'dependencyThree';
}
}

export class DependencyFourToken {
print(): string {
return 'dependencyFour';
}
}

export class MainClass {
public constructor(
private readonly dependencyOne: DependencyOne,
private readonly dependencyTwo: DependencyTwo,
@Inject(forwardRef(() => DependencyThree)) private readonly dependencyThree: DependencyThree,
@Inject('CUSTOM_TOKEN') private readonly dependencyFour: DependencyFourToken,
@Inject('LITERAL_VALUE_ARR') private readonly literalValueArray: string[],
@Inject('LITERAL_VALUE_STR') private readonly literalValueString: string
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ReflectorFactory } from '../src/reflector.service';
import { TokensReflector } from '../src/token-reflector.service';
import {
DependencyFourToken,
DependencyOne,
DependencyThree,
DependencyTwo,
MainClass,
} from './integration.assets';
import { Type } from '@automock/types';
import { ClassDependencies, PrimitiveValue } 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],
])
);
});
});
});
3 changes: 3 additions & 0 deletions packages/adapters/nestjs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import DependenciesReflector from './src';

export = DependenciesReflector
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export default async (): Promise<Config.InitialOptions> => {

return {
...baseConfig,
name: 'nestjs',
displayName: 'nestjs',
name: 'adapters.nestjs',
displayName: 'adapters.nestjs',
collectCoverageFrom: ['src/**/*.ts'],
coveragePathIgnorePatterns: ['index.ts'],
};
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { Type } from '@automock/types';
import { DependenciesReflector as AutomockDependenciesReflector } from '@automock/common';
import {
ClassDependencies,
DependenciesReflector as AutomockDependenciesReflector,
PrimitiveValue,
} from '@automock/common';
import { CustomToken, TokensReflector } from './token-reflector.service';

const INJECTED_TOKENS_METADATA = 'self:paramtypes';
const PARAM_TYPES_METADATA = 'design:paramtypes';

type ClassDependencies = Map<Type | string, Type>;

export function ReflectorFactory(
reflector: typeof Reflect,
tokensReflector: TokensReflector
): AutomockDependenciesReflector {
function reflectDependencies(targetClass: Type): ClassDependencies {
const types = reflectParamTypes(targetClass);
const tokens = reflectParamTokens(targetClass);
const classDependencies: ClassDependencies = new Map<Type | string, Type>();
const classDependencies: ClassDependencies = new Map<Type | string, PrimitiveValue | Type>();

const callback = tokensReflector.attachTokenToDependency(tokens);

Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion packages/reflectors/nestjs/index.ts

This file was deleted.

0 comments on commit ce2e779

Please sign in to comment.