Skip to content

Commit 251fddd

Browse files
tests(): add clients module & reflector unit tests
1 parent a103dd2 commit 251fddd

8 files changed

Lines changed: 79 additions & 3 deletions

File tree

packages/core/test/services/reflector.service.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ describe('Reflector', () => {
1515
expect(reflector.get(key, Test)).to.eql(value);
1616
});
1717
});
18+
describe('getAll', () => {
19+
it('should reflect metadata of all targets', () => {
20+
const key = 'key';
21+
const value = 'value';
22+
Reflect.defineMetadata(key, value, Test);
23+
expect(reflector.getAll(key, [Test])).to.eql([value]);
24+
});
25+
});
1826
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const DEFAULT_CLIENT = 'DEFAULT_CLIENT';
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import { Inject } from '@nestjs/common';
2+
import { DEFAULT_CLIENT } from './clients.constants';
13
import { getClientToken } from './clients.utils';
24

3-
export const InjectClient = (name: string) => getClientToken(name);
5+
export const InjectClient = (
6+
name?: string,
7+
): PropertyDecorator | ParameterDecorator =>
8+
Inject(name ? getClientToken(name) : DEFAULT_CLIENT);
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export const getClientToken = (name: string) => `${name.toUpperCase()}_CLIENT`;
1+
import { DEFAULT_CLIENT } from './clients.constants';
2+
3+
export const getClientToken = (name?: string) =>
4+
name ? `${name.toUpperCase()}_CLIENT` : DEFAULT_CLIENT;

packages/microservices/test/container.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as sinon from 'sinon';
21
import { expect } from 'chai';
32
import { ClientsContainer } from '../container';
43

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SELF_DECLARED_DEPS_METADATA } from '@nestjs/common/constants';
2+
import { expect } from 'chai';
3+
import { getClientToken, InjectClient } from '../../module';
4+
5+
describe('@InjectClient', () => {
6+
const clientName = 'test';
7+
class Test {
8+
constructor(@InjectClient(clientName) param) {}
9+
}
10+
11+
it('should enhance class with expected constructor params metadata', () => {
12+
const metadata = Reflect.getMetadata(SELF_DECLARED_DEPS_METADATA, Test);
13+
14+
const expectedMetadata = [{ index: 0, param: getClientToken(clientName) }];
15+
expect(metadata).to.be.eql(expectedMetadata);
16+
});
17+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { DynamicModule } from '@nestjs/common';
2+
import { expect } from 'chai';
3+
import { ClientProxyFactory } from '../../client';
4+
import { ClientsModule, getClientToken } from '../../module';
5+
6+
describe('ClientsModule', () => {
7+
let dynamicModule: DynamicModule;
8+
beforeEach(() => {
9+
dynamicModule = ClientsModule.register([
10+
{
11+
name: 'test',
12+
options: {},
13+
},
14+
]);
15+
});
16+
it('should return an expected module ref', () => {
17+
expect(dynamicModule.module).to.be.eql(ClientsModule);
18+
});
19+
it('should return an expected providers array', () => {
20+
expect(dynamicModule.providers).to.be.deep.eq([
21+
{
22+
provide: getClientToken('test'),
23+
useValue: ClientProxyFactory.create({}),
24+
},
25+
]);
26+
});
27+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect } from 'chai';
2+
import { getClientToken } from '../../module';
3+
import { DEFAULT_CLIENT } from '../../module/clients.constants';
4+
5+
describe('getClientToken()', () => {
6+
describe('when name is not undefined', () => {
7+
it('should return expected token', () => {
8+
expect(getClientToken('name')).to.be.eql('NAME_CLIENT');
9+
});
10+
});
11+
describe('when name is undefined', () => {
12+
it('should return default token', () => {
13+
expect(getClientToken()).to.be.eql(DEFAULT_CLIENT);
14+
});
15+
});
16+
});

0 commit comments

Comments
 (0)