Skip to content

Commit

Permalink
test(common): added test for fieldname in validation pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias committed Sep 18, 2023
1 parent 7ddc06e commit 9a72802
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/common/pipes/parse-uuid.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ParseUUIDPipe implements PipeTransform<string> {
throw this.exceptionFactory(
`Validation failed (uuid${
this.version ? ` v ${this.version}` : ''
} is expected in ${metadata.data})`,
} is expected in "${metadata.data}")`,
);
}
return value;
Expand Down
5 changes: 5 additions & 0 deletions packages/common/test/pipes/parse-array.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ describe('ParseArrayPipe', () => {
beforeEach(() => {
target = new ParseArrayPipe();
});
it('should mention the field name in the error', async () => {
return expect(
target.transform(true, { data: 'foo' } as ArgumentMetadata),
).to.be.rejectedWith(/.*Validation failed.*"foo".*/);
});
it('should throw an exception (boolean)', async () => {
return expect(
target.transform(true, {} as ArgumentMetadata),
Expand Down
11 changes: 9 additions & 2 deletions packages/common/test/pipes/parse-bool.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { ArgumentMetadata } from '../../interfaces';
import { ParseBoolPipe } from '../../pipes/parse-bool.pipe';
import { BadRequestException } from '../../exceptions';

describe('ParseBoolPipe', () => {
let target: ParseBoolPipe;
Expand All @@ -27,8 +28,14 @@ describe('ParseBoolPipe', () => {
});
describe('when validation fails', () => {
it('should throw an error', async () => {
return expect(target.transform('123abc', {} as ArgumentMetadata)).to.be
.rejected;
return expect(
target.transform('123abc', {} as ArgumentMetadata),
).to.be.rejectedWith(BadRequestException);
});
it('should mention the field name in the error', async () => {
return expect(
target.transform('123abc', { data: 'foo' } as ArgumentMetadata),
).to.be.rejectedWith(/.*Validation failed.*"foo".*/);
});
});
});
Expand Down
23 changes: 19 additions & 4 deletions packages/common/test/pipes/parse-float.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as sinon from 'sinon';
import { expect } from 'chai';
import { ArgumentMetadata } from '../../interfaces';
import { ParseFloatPipe } from '../../pipes/parse-float.pipe';
import { HttpException } from '../../exceptions';
import { BadRequestException, HttpException } from '../../exceptions';

class CustomTestError extends HttpException {
constructor() {
Expand All @@ -13,9 +13,7 @@ class CustomTestError extends HttpException {
describe('ParseFloatPipe', () => {
let target: ParseFloatPipe;
beforeEach(() => {
target = new ParseFloatPipe({
exceptionFactory: (error: any) => new CustomTestError(),
});
target = new ParseFloatPipe();
});
describe('transform', () => {
describe('when validation passes', () => {
Expand All @@ -35,6 +33,23 @@ describe('ParseFloatPipe', () => {
it('should throw an error', async () => {
return expect(
target.transform('123.123abc', {} as ArgumentMetadata),
).to.be.rejectedWith(BadRequestException);
});
it('should mention the field name in the error', async () => {
return expect(
target.transform('123.123abc', { data: 'foo' } as ArgumentMetadata),
).to.be.rejectedWith(/.*Validation failed.*"foo".*/);
});
});
describe('with an exceptionFactory', () => {
beforeEach(() => {
target = new ParseFloatPipe({
exceptionFactory: (error: any) => new CustomTestError(),
});
});
it('uses it when the validation fails', async () => {
return expect(
target.transform('123abc', {} as ArgumentMetadata),
).to.be.rejectedWith(CustomTestError);
});
});
Expand Down
25 changes: 20 additions & 5 deletions packages/common/test/pipes/parse-int.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { ArgumentMetadata } from '../../interfaces';
import { ParseIntPipe } from '../../pipes/parse-int.pipe';
import { HttpException } from '../../exceptions';
import { BadRequestException, HttpException } from '../../exceptions';

class CustomTestError extends HttpException {
constructor() {
Expand All @@ -12,9 +12,7 @@ class CustomTestError extends HttpException {
describe('ParseIntPipe', () => {
let target: ParseIntPipe;
beforeEach(() => {
target = new ParseIntPipe({
exceptionFactory: (error: any) => new CustomTestError(),
});
target = new ParseIntPipe();
});
describe('transform', () => {
describe('when validation passes', () => {
Expand All @@ -37,14 +35,31 @@ describe('ParseIntPipe', () => {
});
});
describe('when validation fails', () => {
it('should mention the field name in the error', async () => {
return expect(
target.transform('123abc', { data: 'foo' } as ArgumentMetadata),
).to.be.rejectedWith(/.*Validation failed.*"foo".*/);
});
it('should throw an error', async () => {
return expect(
target.transform('123abc', {} as ArgumentMetadata),
).to.be.rejectedWith(CustomTestError);
).to.be.rejectedWith(BadRequestException);
});
it('should throw an error when number has wrong number encoding', async () => {
return expect(
target.transform('0xFF', {} as ArgumentMetadata),
).to.be.rejectedWith(BadRequestException);
});
});
describe('with an exceptionFactory', () => {
beforeEach(() => {
target = new ParseIntPipe({
exceptionFactory: (error: any) => new CustomTestError(),
});
});
it('uses it when the validation fails', async () => {
return expect(
target.transform('123abc', {} as ArgumentMetadata),
).to.be.rejectedWith(CustomTestError);
});
});
Expand Down
7 changes: 7 additions & 0 deletions packages/common/test/pipes/parse-uuid.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ describe('ParseUUIDPipe', () => {
).to.be.rejectedWith(TestException);
});

it('should mention the field name in the error', async () => {
target = new ParseUUIDPipe();
await expect(
target.transform('123a', { data: 'foo' } as ArgumentMetadata),
).to.be.rejectedWith(/.*Validation failed.*"foo".*/);
});

it('should throw an error - not a string', async () => {
target = new ParseUUIDPipe({ exceptionFactory });
await expect(
Expand Down

0 comments on commit 9a72802

Please sign in to comment.