Skip to content

Commit 15424e6

Browse files
committed
test(common): added test to generic decorators scenarios
1 parent 342147a commit 15424e6

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

packages/common/test/decorators/create-param-decorator.spec.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,76 @@ describe('createParamDecorator', () => {
102102
});
103103
});
104104
});
105+
106+
describe('returned generic typed decorator', () => {
107+
const factoryFn = (data, req) => true;
108+
interface User {
109+
name: string;
110+
}
111+
112+
const stringOnlyDecorator = createParamDecorator<string>(factoryFn);
113+
const stringOrNumberDecorator = createParamDecorator<string | number>(
114+
factoryFn,
115+
);
116+
const customTypeDecorator = createParamDecorator<User>(factoryFn);
117+
118+
describe('when string is passed to stringOnlyDecorator', () => {
119+
const data = 'test';
120+
class Test {
121+
public test(
122+
@stringOnlyDecorator(data)
123+
param,
124+
) {}
125+
}
126+
it('should enhance param with "data" as string', () => {
127+
const metadata = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
128+
const key = Object.keys(metadata)[0];
129+
expect(metadata[key]).to.be.eql({
130+
data: 'test',
131+
factory: factoryFn,
132+
index: 0,
133+
pipes: [],
134+
});
135+
});
136+
});
137+
138+
describe('when number is passed to stringOrNumberDecorator', () => {
139+
const data = 10;
140+
class Test {
141+
public test(
142+
@stringOrNumberDecorator(data)
143+
param,
144+
) {}
145+
}
146+
it('should enhance param with "data" as number', () => {
147+
const metadata = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
148+
const key = Object.keys(metadata)[0];
149+
expect(metadata[key]).to.be.eql({
150+
data: 10,
151+
factory: factoryFn,
152+
index: 0,
153+
pipes: [],
154+
});
155+
});
156+
});
157+
describe('when a custom Type is passed to customTypeDecorator', () => {
158+
const data = { name: 'john' };
159+
class Test {
160+
public test(
161+
@customTypeDecorator(data)
162+
param,
163+
) {}
164+
}
165+
it('should enhance param with "data" as custom Type', () => {
166+
const metadata = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
167+
const key = Object.keys(metadata)[0];
168+
expect(metadata[key]).to.be.eql({
169+
data: { name: 'john' },
170+
factory: factoryFn,
171+
index: 0,
172+
pipes: [],
173+
});
174+
});
175+
});
176+
});
105177
});

0 commit comments

Comments
 (0)