|
1 | | -import { serialize, unserialize } from '../index'; |
| 1 | +import { checkStringLength, serialize, unserialize } from '../index'; |
| 2 | +import { PolymathError } from '../../PolymathError'; |
| 3 | +import { ErrorCode } from '../../types'; |
2 | 4 |
|
3 | 5 | describe('serialize and unserialize', () => { |
4 | 6 | const entityType = 'someEntity'; |
@@ -31,3 +33,46 @@ describe('serialize and unserialize', () => { |
31 | 33 | expect(unserialize(serialize(entityType, inversePojo1))).toEqual(pojo1); |
32 | 34 | }); |
33 | 35 | }); |
| 36 | + |
| 37 | +describe('check string length', () => { |
| 38 | + const testVariableType = 'myVar'; |
| 39 | + |
| 40 | + test('checkStringLength used on a non empty string, catches error above the default 32 characters', () => { |
| 41 | + try { |
| 42 | + checkStringLength('0123456789012345678901234567890123456789', testVariableType); |
| 43 | + } catch (error) { |
| 44 | + expect(error).toEqual( |
| 45 | + new PolymathError({ |
| 46 | + code: ErrorCode.ProcedureValidationError, |
| 47 | + message: `You must provide a valid ${testVariableType} up to 32 characters long`, |
| 48 | + }) |
| 49 | + ); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + test('checkStringLength used on an empty string, catches error due to not meeting custom requirement of 1 - 32 characters', () => { |
| 54 | + try { |
| 55 | + checkStringLength('', testVariableType, { minLength: 1, maxLength: 32 }); |
| 56 | + } catch (error) { |
| 57 | + expect(error).toEqual( |
| 58 | + new PolymathError({ |
| 59 | + code: ErrorCode.ProcedureValidationError, |
| 60 | + message: `You must provide a valid ${testVariableType} between 1 and 32 characters long`, |
| 61 | + }) |
| 62 | + ); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + test('checkStringLength used on a 6 character string, catches error due to not meeting custom requirement of 1 - 5 characters', () => { |
| 67 | + try { |
| 68 | + checkStringLength('0123456', testVariableType, { minLength: 1, maxLength: 5 }); |
| 69 | + } catch (error) { |
| 70 | + expect(error).toEqual( |
| 71 | + new PolymathError({ |
| 72 | + code: ErrorCode.ProcedureValidationError, |
| 73 | + message: `You must provide a valid ${testVariableType} between 1 and 5 characters long`, |
| 74 | + }) |
| 75 | + ); |
| 76 | + } |
| 77 | + }); |
| 78 | +}); |
0 commit comments