1+ 'use strict' ;
2+
3+ var JSONScript = require ( '../lib/jsonscript' ) ;
4+ var assert = require ( 'assert' ) ;
5+ var testutil = require ( './testutil' ) ;
6+ var shouldBeError = testutil . shouldBeError ;
7+ var routers = require ( './routers' ) ;
8+
9+
10+ describe ( '$if instruction - conditional evaluation' , function ( ) {
11+ var js ;
12+
13+ before ( function ( ) {
14+ js = JSONScript ( ) ;
15+ js . addExecutor ( 'router1' , routers . router1 ) ;
16+ js . addExecutor ( 'router2' , routers . router2 ) ;
17+ js . addExecutor ( 'async' , function ( value ) { return Promise . resolve ( value ) ; } ) ;
18+ } ) ;
19+
20+ var data = {
21+ cond1 : true ,
22+ cond2 : false ,
23+ notBoolean : 1 ,
24+ then : 'foo' ,
25+ else : 'bar' ,
26+ thenRouter : 'router1' ,
27+ elseRouter : 'router2'
28+ } ;
29+
30+ it ( 'should return the result of evaluation of script in $then or in $else' , function ( ) {
31+ return Promise . all ( [
32+ test ( { $if : true , $then : 'foo' , $else : 'bar' } , 'foo' ) ,
33+ test ( { $if : false , $then : 'foo' , $else : 'bar' } , 'bar' ) ,
34+ test ( { $if : false , $then : 'foo' } , null ) ,
35+ test ( { $if : { $data : '/cond1' } , $then : 'foo' , $else : 'bar' } , 'foo' ) ,
36+ test ( { $if : { $data : '/cond2' } , $then : 'foo' , $else : 'bar' } , 'bar' ) ,
37+ test ( { $if : { $data : '/cond2' } , $then : 'foo' } , null ) ,
38+ test ( {
39+ $if : true ,
40+ $then : { $data : '/then' } ,
41+ $else : { $data : '/else' }
42+ } , 'foo' ) ,
43+ test ( {
44+ $if : false ,
45+ $then : { $data : '/then' } ,
46+ $else : { $data : '/else' }
47+ } , 'bar' ) ,
48+ test ( {
49+ $if : false ,
50+ $then : { $data : '/then' }
51+ } , null ) ,
52+ test ( {
53+ $if : { $data : '/cond1' } ,
54+ $then : { $data : '/then' } ,
55+ $else : { $data : '/else' }
56+ } , 'foo' ) ,
57+ test ( {
58+ $if : { $data : '/cond2' } ,
59+ $then : { $data : '/then' } ,
60+ $else : { $data : '/else' }
61+ } , 'bar' ) ,
62+ test ( {
63+ $if : { $data : '/cond2' } ,
64+ $then : { $data : '/then' }
65+ } , null )
66+ ] ) ;
67+ } ) ;
68+
69+ it ( 'should allow for $if/$then/$else to be asynchronous' , function ( ) {
70+ return Promise . all ( [
71+ test ( {
72+ $if : { $exec : 'async' , $args : true } ,
73+ $then : 'foo' ,
74+ $else : 'bar'
75+ } , 'foo' ) ,
76+ test ( {
77+ $if : { $exec : 'async' , $args : false } ,
78+ $then : 'foo' ,
79+ $else : 'bar'
80+ } , 'bar' ) ,
81+ test ( {
82+ $if : { $exec : 'async' , $args : false } ,
83+ $then : 'foo'
84+ } , null ) ,
85+ test ( {
86+ $if : true ,
87+ $then : { $exec : 'async' , $args : 'foo' } ,
88+ $else : { $exec : 'async' , $args : 'bar' }
89+ } , 'foo' ) ,
90+ test ( {
91+ $if : false ,
92+ $then : { $exec : 'async' , $args : 'foo' } ,
93+ $else : { $exec : 'async' , $args : 'bar' }
94+ } , 'bar' ) ,
95+ test ( {
96+ $if : false ,
97+ $then : { $exec : 'async' , $args : 'foo' }
98+ } , null ) ,
99+ test ( {
100+ $if : { $exec : 'async' , $args : true } ,
101+ $then : { $exec : 'async' , $args : 'foo' } ,
102+ $else : { $exec : 'async' , $args : 'bar' }
103+ } , 'foo' ) ,
104+ test ( {
105+ $if : { $exec : 'async' , $args : false } ,
106+ $then : { $exec : 'async' , $args : 'foo' } ,
107+ $else : { $exec : 'async' , $args : 'bar' }
108+ } , 'bar' ) ,
109+ test ( {
110+ $if : { $exec : 'async' , $args : false } ,
111+ $then : { $exec : 'async' , $args : 'foo' }
112+ } , null )
113+ ] ) ;
114+ } ) ;
115+
116+ it ( 'should allow for $if/$then/$else to be any script' , function ( ) {
117+ return Promise . all ( [
118+ test ( {
119+ $if : { $data : '/cond1' } ,
120+ $then : { $exec : 'router1' , $args : { path : '/resource' } } ,
121+ $else : { $exec : 'router2' , $args : { path : '/resource' } }
122+ } , 'you requested /resource from router1' ) ,
123+ test ( {
124+ $exec : {
125+ $if : { $data : '/cond1' } ,
126+ $then : { $data : '/thenRouter' } ,
127+ $else : { $data : '/elseRouter' }
128+ } ,
129+ $args : { path : '/resource' }
130+ } , 'you requested /resource from router1' )
131+ ] ) ;
132+ } ) ;
133+
134+ it ( 'should throw error if $if is not boolean' , function ( ) {
135+ return Promise . all ( [
136+ shouldBeError ( js . evaluate ( { $if : 1 , $then : 'foo' } ) , 'validation failed' , [ 'should be boolean' ] ) ,
137+ shouldBeError ( js . evaluate ( { $if : { $data : '/notBoolean' } , $then : 'foo' } , data ) ,
138+ 'validation failed' , [ 'should be boolean' ] ) ,
139+ shouldBeError ( js . evaluate ( { $if : { $exec : 'async' , $args : 1 } , $then : 'foo' } , data ) ,
140+ 'validation failed' , [ 'should be boolean' ] ) ,
141+ shouldBeError ( js . evaluate ( { $if : { $data : { $exec : 'async' , $args : '/notBoolean' } } , $then : 'foo' } , data ) ,
142+ 'validation failed' , [ 'should be boolean' ] )
143+ ] ) ;
144+ } ) ;
145+
146+
147+ function test ( script , expectedResult ) {
148+ return js . evaluate ( script , data ) . then ( function ( res ) {
149+ assert . strictEqual ( res , expectedResult ) ;
150+ if ( expectedResult ) assert . deepEqual ( res , expectedResult ) ;
151+ } ) ;
152+ } ;
153+ } ) ;
0 commit comments