|
| 1 | +import * as sinon from 'sinon'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { ArgumentMetadata } from './../../interfaces'; |
| 4 | +import { IsString } from 'class-validator'; |
| 5 | +import { ValidationPipe } from './../../pipes/validation.pipe'; |
| 6 | + |
| 7 | +class TestModel { |
| 8 | + constructor() {} |
| 9 | + @IsString() public prop1: string; |
| 10 | + |
| 11 | + @IsString() public prop2: string; |
| 12 | +} |
| 13 | + |
| 14 | +describe('ValidationPipe', () => { |
| 15 | + let target: ValidationPipe; |
| 16 | + const metadata: ArgumentMetadata = { |
| 17 | + type: 'body', |
| 18 | + metatype: TestModel, |
| 19 | + data: '', |
| 20 | + }; |
| 21 | + beforeEach(() => { |
| 22 | + target = new ValidationPipe(); |
| 23 | + }); |
| 24 | + describe('transform', () => { |
| 25 | + describe('when metadata is empty or undefined', () => { |
| 26 | + it('should return the value unchanged', async () => { |
| 27 | + const testObj = { prop1: 'value1', prop2: 'value2' }; |
| 28 | + expect(await target.transform(testObj, {} as any)).to.equal(testObj); |
| 29 | + expect(await target.transform(testObj, {} as any)).to.not.be.instanceOf( |
| 30 | + TestModel, |
| 31 | + ); |
| 32 | + }); |
| 33 | + }); |
| 34 | + describe('when metadata contains a class', () => { |
| 35 | + it('should return an instance of the class', async () => { |
| 36 | + const testObj = { prop1: 'value1', prop2: 'value2' }; |
| 37 | + const result = await target.transform(testObj, metadata); |
| 38 | + expect(result).to.be.instanceOf(TestModel); |
| 39 | + }); |
| 40 | + }); |
| 41 | + describe('when validation vails', () => { |
| 42 | + it('should throw an error', async () => { |
| 43 | + const testObj = { prop1: 'value1' }; |
| 44 | + return expect(target.transform(testObj, metadata)).to.be.rejected; |
| 45 | + }); |
| 46 | + }); |
| 47 | + }); |
| 48 | +}); |
0 commit comments