-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
create-param-decorator.spec.ts
177 lines (163 loc) 路 5.11 KB
/
create-param-decorator.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { expect } from 'chai';
import { ROUTE_ARGS_METADATA } from '../../constants';
import { createParamDecorator } from '../../decorators/http/create-route-param-metadata.decorator';
import { ParseIntPipe } from '../../index';
describe('createParamDecorator', () => {
let result;
beforeEach(() => {
const fn = (data, req) => true;
result = createParamDecorator(fn);
});
it('should return a function as a first element', () => {
expect(result).to.be.a('function');
});
describe('returned decorator', () => {
const factoryFn = (data, req) => true;
const Decorator = createParamDecorator(factoryFn);
describe('when 0 pipes have been passed', () => {
const data = { data: 'test' };
class Test {
public test(@Decorator(data) param) {}
}
it('should enhance param with "data"', () => {
const metadata = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
const key = Object.keys(metadata)[0];
expect(metadata[key]).to.be.eql({
data,
factory: factoryFn,
index: 0,
pipes: [],
});
});
});
describe('when > 0 pipes have been passed', () => {
const data = 'test';
const pipe = new ParseIntPipe();
class Test {
public test(
@Decorator(data, pipe)
param,
) {}
public testNoData(@Decorator(pipe) param) {}
public testNoDataClass(@Decorator(ParseIntPipe) param) {}
}
it('should enhance param with "data" and ParseIntPipe', () => {
const metadata = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
const key = Object.keys(metadata)[0];
expect(metadata[key]).to.be.eql({
data: 'test',
factory: factoryFn,
index: 0,
pipes: [pipe],
});
});
it('should enhance param with ParseIntPipe', () => {
const metadata = Reflect.getMetadata(
ROUTE_ARGS_METADATA,
Test,
'testNoData',
);
const key = Object.keys(metadata)[0];
expect(metadata[key]).to.be.eql({
data: undefined,
factory: factoryFn,
index: 0,
pipes: [pipe],
});
});
it('should enhance param with ParseIntPipe metatype', () => {
const metadata = Reflect.getMetadata(
ROUTE_ARGS_METADATA,
Test,
'testNoDataClass',
);
const key = Object.keys(metadata)[0];
expect(metadata[key]).to.be.eql({
data: undefined,
factory: factoryFn,
index: 0,
pipes: [ParseIntPipe],
});
});
});
describe('when class type passed as data', () => {
class Data {}
class Test {
public test(@Decorator(Data) prop) {}
}
it('should return class type as data parameter', () => {
const metadata = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
const key = Object.keys(metadata)[0];
expect(metadata[key].data).to.equal(Data);
});
});
});
describe('returned generic typed decorator', () => {
const factoryFn = (data, req) => true;
interface User {
name: string;
}
const stringOnlyDecorator = createParamDecorator<string>(factoryFn);
const stringOrNumberDecorator = createParamDecorator<string | number>(
factoryFn,
);
const customTypeDecorator = createParamDecorator<User>(factoryFn);
describe('when string is passed to stringOnlyDecorator', () => {
const data = 'test';
class Test {
public test(
@stringOnlyDecorator(data)
param,
) {}
}
it('should enhance param with "data" as string', () => {
const metadata = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
const key = Object.keys(metadata)[0];
expect(metadata[key]).to.be.eql({
data: 'test',
factory: factoryFn,
index: 0,
pipes: [],
});
});
});
describe('when number is passed to stringOrNumberDecorator', () => {
const data = 10;
class Test {
public test(
@stringOrNumberDecorator(data)
param,
) {}
}
it('should enhance param with "data" as number', () => {
const metadata = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
const key = Object.keys(metadata)[0];
expect(metadata[key]).to.be.eql({
data: 10,
factory: factoryFn,
index: 0,
pipes: [],
});
});
});
describe('when a custom Type is passed to customTypeDecorator', () => {
const data = { name: 'john' };
class Test {
public test(
@customTypeDecorator(data)
param,
) {}
}
it('should enhance param with "data" as custom Type', () => {
const metadata = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
const key = Object.keys(metadata)[0];
expect(metadata[key]).to.be.eql({
data: { name: 'john' },
factory: factoryFn,
index: 0,
pipes: [],
});
});
});
});
});