@@ -307,4 +307,49 @@ QUnit.module('basic', () => {
307
307
jsonLogic . apply ( { "or" : [ { "push" : [ true ] } , { "push" : [ true ] } ] } ) ;
308
308
assert . deepEqual ( i , [ true ] ) ;
309
309
} ) ;
310
+
311
+ QUnit . test ( "Expanding functionality with add_operator - controlledExecution" , function ( assert ) {
312
+ // assert that controlled execution doesn't do pre-evaluation
313
+ var customOp = function ( values , data , jsonLogic ) {
314
+ return jsonLogic . apply ( values [ 0 ] , data ) ;
315
+ }
316
+
317
+ jsonLogic . add_operation ( 'customOp' , customOp , { controlledExecution : true } ) ;
318
+
319
+ assert . deepEqual ( jsonLogic . apply ( { customOp : [ { "var" : "" } , { "var" : "test" } ] } , { test : 123 } ) , { test : 123 } ) ;
320
+ assert . deepEqual ( jsonLogic . apply ( { customOp : [ { "var" : "test" } , { "var" : "" } ] } , { test : 123 } ) , 123 ) ;
321
+
322
+ // assert that controlled execution custom operators can be removed as normal
323
+ jsonLogic . rm_operation ( 'customOp' ) ;
324
+
325
+ assert . throws ( ( ) => jsonLogic . apply ( { customOp : [ ] } ) , Error , "Unrecognized operation customOp" ) ;
326
+
327
+ // assert that controlled-execution custom operators have access to jsonLogic object
328
+ // and can run on external data
329
+ const externalData = {
330
+ specialReference : 'external reference'
331
+ } ;
332
+ customOp = function ( values , data , jsonLogic ) {
333
+ return jsonLogic . apply ( values [ 0 ] , externalData ) ;
334
+ }
335
+
336
+ jsonLogic . add_operation ( 'customOp' , customOp , { controlledExecution : true } ) ;
337
+
338
+ assert . deepEqual ( jsonLogic . apply ( { customOp : [ { var : "specialReference" } ] } , { specialReference : 'pre-evaluation value' } ) , 'external reference' ) ;
339
+
340
+ // assert that operators are added with normal functionality when options is omitted
341
+ jsonLogic . add_operation ( 'customOp' , customOp ) ;
342
+
343
+ assert . throws ( ( ) => jsonLogic . apply ( { customOp : [ { var : "specialReference" } ] } , { specialReference : 'pre-evaluation value' } ) , TypeError , "Cannot read property 'apply' of undefined" ) ;
344
+
345
+ // assert that adding a custom operator without controlled-execution still
346
+ // results in pre-evaluation
347
+ customOp = function ( value ) {
348
+ return value ;
349
+ }
350
+
351
+ jsonLogic . add_operation ( 'customOp' , customOp ) ;
352
+
353
+ assert . deepEqual ( jsonLogic . apply ( { customOp : [ { var : "specialReference" } ] } , { specialReference : 'pre-evaluation value' } ) , 'pre-evaluation value' ) ;
354
+ } ) ;
310
355
} ) ;
0 commit comments