|
| 1 | +/* tslint:disable:max-classes-per-file */ |
| 2 | + |
| 3 | +import * as assert from 'assert'; |
| 4 | +import { AssertType, ValidateClass } from '../index'; |
| 5 | + |
| 6 | +/* https://github.com/woutervh-/typescript-is/issues/75 */ |
| 7 | + |
| 8 | +describe('@ValidateClass, @AssertType', () => { |
| 9 | + describe('@ValidateClass(), @AssertType() skipping first parameter', () => { |
| 10 | + @ValidateClass() |
| 11 | + class TestClass { |
| 12 | + testMethod(first: string, @AssertType() second: number) { |
| 13 | + // No op. |
| 14 | + } |
| 15 | + } |
| 16 | + const instance = new TestClass(); |
| 17 | + |
| 18 | + it('should pass validation for as long as the second parameter is a number', () => { |
| 19 | + instance.testMethod('foo', 0); |
| 20 | + instance.testMethod('foo', 1); |
| 21 | + instance.testMethod('foo', Number.NaN); |
| 22 | + instance.testMethod(false as any, 0); |
| 23 | + instance.testMethod(false as any, 1); |
| 24 | + instance.testMethod(false as any, Number.NaN); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should throw an error for non-numbers passed to the second parameter', () => { |
| 28 | + const expectedMessageRegExp = /validation failed at second: expected a number, found: .*$/; |
| 29 | + assert.throws(() => instance.testMethod('foo', '' as any), expectedMessageRegExp); |
| 30 | + assert.throws(() => instance.testMethod('foo', '0' as any), expectedMessageRegExp); |
| 31 | + assert.throws(() => instance.testMethod('foo', '1' as any), expectedMessageRegExp); |
| 32 | + assert.throws(() => instance.testMethod('foo', true as any), expectedMessageRegExp); |
| 33 | + assert.throws(() => instance.testMethod('foo', false as any), expectedMessageRegExp); |
| 34 | + assert.throws(() => instance.testMethod('foo', {} as any), expectedMessageRegExp); |
| 35 | + assert.throws(() => instance.testMethod('foo', [] as any), expectedMessageRegExp); |
| 36 | + assert.throws(() => instance.testMethod('foo', null as any), expectedMessageRegExp); |
| 37 | + }); |
| 38 | + }); |
| 39 | +}); |
0 commit comments