1
+ import { validateRequiredField , validateOptionalField } from '../../../src/models/transactions/common'
2
+ import { ValidationError } from '../../../src/errors'
3
+
4
+ describe ( 'validateRequiredField' , ( ) => {
5
+ const txMock = {
6
+ TransactionType : 'Payment' ,
7
+ amount : 42 ,
8
+ account : 'rXYZ' ,
9
+ }
10
+
11
+ it ( 'throws an error with expected and actual types' , ( ) => {
12
+ expect ( ( ) =>
13
+ validateRequiredField ( txMock , 'amount' , ( val ) => typeof val === 'string' , 'string' )
14
+ ) . toThrow ( new ValidationError ( 'Payment: invalid field amount: expected string, received number' ) )
15
+ } )
16
+
17
+ it ( 'does not throw if value is valid' , ( ) => {
18
+ expect ( ( ) =>
19
+ validateRequiredField ( txMock , 'account' , ( val ) => typeof val === 'string' , 'string' )
20
+ ) . not . toThrow ( )
21
+ } )
22
+
23
+ it ( 'throws without expectedType if not passed' , ( ) => {
24
+ expect ( ( ) =>
25
+ validateRequiredField ( txMock , 'amount' , ( val ) => typeof val === 'string' )
26
+ ) . toThrow ( new ValidationError ( 'Payment: invalid field amount' ) )
27
+ } )
28
+
29
+ it ( 'throws when field is missing' , ( ) => {
30
+ expect ( ( ) =>
31
+ validateRequiredField ( txMock , 'nonExistentField' , ( val ) => typeof val === 'string' )
32
+ ) . toThrow ( new ValidationError ( 'Payment: missing field nonExistentField' ) )
33
+ } )
34
+ } )
35
+
36
+ describe ( 'validateOptionalField' , ( ) => {
37
+ const txMock = {
38
+ TransactionType : 'Payment' ,
39
+ memo : 123 ,
40
+ }
41
+
42
+ const txNoMemo = {
43
+ TransactionType : 'Payment' ,
44
+ }
45
+
46
+ it ( 'skips validation if value is undefined' , ( ) => {
47
+ expect ( ( ) =>
48
+ validateOptionalField ( txNoMemo , 'memo' , ( val ) => typeof val === 'string' , 'string' )
49
+ ) . not . toThrow ( )
50
+ } )
51
+
52
+ it ( 'delegates to validation if value is defined' , ( ) => {
53
+ expect ( ( ) =>
54
+ validateOptionalField ( txMock , 'memo' , ( val ) => typeof val === 'string' , 'string' )
55
+ ) . toThrow ( new ValidationError ( 'Payment: invalid field memo: expected string, received number' ) )
56
+ } )
57
+ } )
0 commit comments