forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-int.pipe.spec.ts
47 lines (45 loc) 路 1.43 KB
/
parse-int.pipe.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
import { expect } from 'chai';
import { ArgumentMetadata } from '../../interfaces';
import { ParseIntPipe } from '../../pipes/parse-int.pipe';
import { HttpException } from '../../exceptions';
class CustomTestError extends HttpException {
constructor() {
super('This is a TestException', 418);
}
}
describe('ParseIntPipe', () => {
let target: ParseIntPipe;
beforeEach(() => {
target = new ParseIntPipe({
exceptionFactory: (error: any) => new CustomTestError(),
});
});
describe('transform', () => {
describe('when validation passes', () => {
it('should return number', async () => {
const num = '3';
expect(await target.transform(num, {} as ArgumentMetadata)).to.equal(
parseInt(num, 10),
);
});
it('should return negative number', async () => {
const num = '-3';
expect(await target.transform(num, {} as ArgumentMetadata)).to.equal(
-3,
);
});
});
describe('when validation fails', () => {
it('should throw an error', async () => {
return expect(
target.transform('123abc', {} as ArgumentMetadata),
).to.be.rejectedWith(CustomTestError);
});
it('should throw an error when number has wrong number encoding', async () => {
return expect(
target.transform('0xFF', {} as ArgumentMetadata),
).to.be.rejectedWith(CustomTestError);
});
});
});
});