1- const propertiesValidator = require ( '../properties' ) ;
1+ const { validateProperties , validateEvaluationOptions } = require ( '../properties' ) ;
22
3- describe ( 'properties validator ' , ( ) => {
3+ describe ( 'validateProperties ' , ( ) => {
44 test ( 'should return error on invalid properties' , done => {
5- const expected = 'Input must be a plain object.' ;
6-
7- const result = propertiesValidator ( 'test' ) ;
8-
5+ const expected = 'properties must be a plain object.' ;
6+ const result = validateProperties ( 'test' ) ;
97 expect ( result ) . toHaveProperty ( 'valid' , false ) ;
108 expect ( result ) . toHaveProperty ( 'error' , expected ) ;
119 expect ( result ) . not . toHaveProperty ( 'value' ) ;
1210 done ( ) ;
1311 } ) ;
1412
1513 test ( 'should return error on invalid properties 2' , done => {
16- const expected = 'Input must be a plain object.' ;
17-
18- const result = propertiesValidator ( '[]' ) ;
19-
14+ const expected = 'properties must be a plain object.' ;
15+ const result = validateProperties ( '[]' ) ;
2016 expect ( result ) . toHaveProperty ( 'valid' , false ) ;
2117 expect ( result ) . toHaveProperty ( 'error' , expected ) ;
2218 expect ( result ) . not . toHaveProperty ( 'value' ) ;
2319 done ( ) ;
2420 } ) ;
2521
2622 test ( 'should return error on invalid properties 3' , done => {
27- const expected = 'Input must be a plain object.' ;
28-
29- const result = propertiesValidator ( 'true' ) ;
30-
23+ const expected = 'properties must be a plain object.' ;
24+ const result = validateProperties ( 'true' ) ;
3125 expect ( result ) . toHaveProperty ( 'valid' , false ) ;
3226 expect ( result ) . toHaveProperty ( 'error' , expected ) ;
3327 expect ( result ) . not . toHaveProperty ( 'value' ) ;
3428 done ( ) ;
3529 } ) ;
3630
3731 test ( 'should be valid when properties is an object' , done => {
38- const result = propertiesValidator ( '{"my-prop":true}' ) ;
32+ const result = validateProperties ( '{"my-prop":true}' ) ;
3933 expect ( result ) . toHaveProperty ( 'valid' , true ) ;
4034 expect ( result ) . toHaveProperty ( 'value' , {
4135 'my-prop' : true ,
@@ -44,21 +38,116 @@ describe('properties validator', () => {
4438 done ( ) ;
4539 } ) ;
4640
41+ test ( 'should be valid when properties is an object' , done => {
42+ const expected = 'properties must only contain boolean, string, or number values.' ;
43+ const result = validateProperties ( '{"my-prop":[]}' ) ;
44+ expect ( result ) . toHaveProperty ( 'valid' , false ) ;
45+ expect ( result ) . toHaveProperty ( 'error' , expected ) ;
46+ expect ( result ) . not . toHaveProperty ( 'value' ) ;
47+ done ( ) ;
48+ } ) ;
49+
4750 test ( 'should be valid when properties is empty object' , done => {
48- const result = propertiesValidator ( '{}' ) ;
49-
51+ const result = validateProperties ( '{}' ) ;
5052 expect ( result ) . toHaveProperty ( 'valid' , true ) ;
5153 expect ( result ) . toHaveProperty ( 'value' , { } ) ;
5254 expect ( result ) . not . toHaveProperty ( 'error' ) ;
5355 done ( ) ;
5456 } ) ;
5557
5658 test ( 'should be valid when properties is null' , done => {
57- const result = propertiesValidator ( ) ;
58-
59+ const result = validateProperties ( ) ;
5960 expect ( result ) . toHaveProperty ( 'valid' , true ) ;
6061 expect ( result ) . toHaveProperty ( 'value' , null ) ;
6162 expect ( result ) . not . toHaveProperty ( 'error' ) ;
6263 done ( ) ;
6364 } ) ;
6465} ) ;
66+
67+ describe ( 'validateEvaluationOptions' , ( ) => {
68+ test ( 'should return error on invalid options' , done => {
69+ const expected = 'options must be a plain object.' ;
70+ const result = validateEvaluationOptions ( 'test' ) ;
71+ expect ( result ) . toHaveProperty ( 'valid' , false ) ;
72+ expect ( result ) . toHaveProperty ( 'error' , expected ) ;
73+ expect ( result ) . not . toHaveProperty ( 'value' ) ;
74+ done ( ) ;
75+ } ) ;
76+
77+ test ( 'should return error on invalid options 2' , done => {
78+ const expected = 'options must be a plain object.' ;
79+ const result = validateEvaluationOptions ( '[]' ) ;
80+ expect ( result ) . toHaveProperty ( 'valid' , false ) ;
81+ expect ( result ) . toHaveProperty ( 'error' , expected ) ;
82+ expect ( result ) . not . toHaveProperty ( 'value' ) ;
83+ done ( ) ;
84+ } ) ;
85+
86+ test ( 'should return error on invalid options 3' , done => {
87+ const expected = 'options must be a plain object.' ;
88+ const result = validateEvaluationOptions ( 'true' ) ;
89+ expect ( result ) . toHaveProperty ( 'valid' , false ) ;
90+ expect ( result ) . toHaveProperty ( 'error' , expected ) ;
91+ expect ( result ) . not . toHaveProperty ( 'value' ) ;
92+ done ( ) ;
93+ } ) ;
94+
95+ test ( 'should be valid when options is an object' , done => {
96+ const result = validateEvaluationOptions ( '{"foo":1}' ) ;
97+ expect ( result ) . toHaveProperty ( 'valid' , true ) ;
98+ expect ( result ) . toHaveProperty ( 'value' , { foo : 1 } ) ;
99+ expect ( result ) . not . toHaveProperty ( 'error' ) ;
100+ done ( ) ;
101+ } ) ;
102+
103+ test ( 'should be valid when options is empty object' , done => {
104+ const result = validateEvaluationOptions ( '{}' ) ;
105+ expect ( result ) . toHaveProperty ( 'valid' , true ) ;
106+ expect ( result ) . toHaveProperty ( 'value' , { } ) ;
107+ expect ( result ) . not . toHaveProperty ( 'error' ) ;
108+ done ( ) ;
109+ } ) ;
110+
111+ test ( 'should be valid when options is null' , done => {
112+ const result = validateEvaluationOptions ( ) ;
113+ expect ( result ) . toHaveProperty ( 'valid' , true ) ;
114+ expect ( result ) . toHaveProperty ( 'value' , null ) ;
115+ expect ( result ) . not . toHaveProperty ( 'error' ) ;
116+ done ( ) ;
117+ } ) ;
118+
119+ test ( 'should return error if options.properties is not valid' , done => {
120+ const expected = 'properties must be a plain object.' ;
121+ const result = validateEvaluationOptions ( '{"properties": "not-an-object"}' ) ;
122+ expect ( result ) . toHaveProperty ( 'valid' , false ) ;
123+ expect ( result ) . toHaveProperty ( 'error' , expected ) ;
124+ expect ( result ) . not . toHaveProperty ( 'value' ) ;
125+ done ( ) ;
126+ } ) ;
127+
128+ test ( 'should return error if options.properties has invalid value type' , done => {
129+ const expected = 'properties must only contain boolean, string, or number values.' ;
130+ const result = validateEvaluationOptions ( '{"properties": {"foo": []}}' ) ;
131+ expect ( result ) . toHaveProperty ( 'valid' , false ) ;
132+ expect ( result ) . toHaveProperty ( 'error' , expected ) ;
133+ expect ( result ) . not . toHaveProperty ( 'value' ) ;
134+ done ( ) ;
135+ } ) ;
136+
137+ test ( 'should be valid if options.properties is valid' , done => {
138+ const result = validateEvaluationOptions ( '{"properties": {"foo": 1, "bar": "baz", "baz": true}}' ) ;
139+ expect ( result ) . toHaveProperty ( 'valid' , true ) ;
140+ expect ( result ) . toHaveProperty ( 'value' , { foo : 1 , bar : 'baz' , baz : true } ) ;
141+ expect ( result ) . not . toHaveProperty ( 'error' ) ;
142+ done ( ) ;
143+ } ) ;
144+
145+ test ( 'should be valid if options.properties is valid' , done => {
146+ const expected = 'properties must only contain boolean, string, or number values.' ;
147+ const result = validateEvaluationOptions ( '{"properties": {"foo": [], "bar": "baz", "baz": true}}' ) ;
148+ expect ( result ) . toHaveProperty ( 'valid' , false ) ;
149+ expect ( result ) . toHaveProperty ( 'error' , expected ) ;
150+ expect ( result ) . not . toHaveProperty ( 'value' ) ;
151+ done ( ) ;
152+ } ) ;
153+ } ) ;
0 commit comments