2023-04-03 10:41:15 +00:00
import { parseJSObject } from "../index" ;
2023-05-16 16:59:11 +00:00
import { extractIdentifierInfoFromCode , isFunctionPresent } from "../src/index" ;
2022-08-23 11:09:42 +00:00
2022-09-28 17:28:18 +00:00
describe ( "getAllIdentifiers" , ( ) = > {
it ( "works properly" , ( ) = > {
2022-09-30 13:06:25 +00:00
const cases : Array < {
script : string ;
expectedResults : string [ ] ;
invalidIdentifiers? : Record < string , unknown > ;
} > = [
2022-09-17 17:40:28 +00:00
{
// Entity reference
2022-09-28 17:28:18 +00:00
script : "DirectTableReference" ,
expectedResults : [ "DirectTableReference" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// One level nesting
2022-09-28 17:28:18 +00:00
script : "TableDataReference.data" ,
expectedResults : [ "TableDataReference.data" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Deep nesting
2022-09-28 17:28:18 +00:00
script : "TableDataDetailsReference.data.details" ,
expectedResults : [ "TableDataDetailsReference.data.details" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Deep nesting
2022-09-28 17:28:18 +00:00
script : "TableDataDetailsMoreReference.data.details.more" ,
expectedResults : [ "TableDataDetailsMoreReference.data.details.more" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Deep optional chaining
2022-09-28 17:28:18 +00:00
script : "TableDataOptionalReference.data?.details.more" ,
expectedResults : [ "TableDataOptionalReference.data" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Deep optional chaining with logical operator
script :
2022-09-28 17:28:18 +00:00
"TableDataOptionalWithLogical.data?.details.more || FallbackTableData.data" ,
2022-09-17 17:40:28 +00:00
expectedResults : [
2022-09-28 17:28:18 +00:00
"TableDataOptionalWithLogical.data" ,
"FallbackTableData.data" ,
2022-09-17 17:40:28 +00:00
] ,
} ,
{
// null coalescing
2022-09-28 17:28:18 +00:00
script : "TableDataOptionalWithLogical.data ?? FallbackTableData.data" ,
2022-09-17 17:40:28 +00:00
expectedResults : [
2022-09-28 17:28:18 +00:00
"TableDataOptionalWithLogical.data" ,
"FallbackTableData.data" ,
2022-09-17 17:40:28 +00:00
] ,
} ,
{
// Basic map function
2022-09-28 17:28:18 +00:00
script : "Table5.data.map(c => ({ name: c.name }))" ,
expectedResults : [ "Table5.data.map" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Literal property search
script : "Table6['data']" ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Table6" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Deep literal property search
script : "TableDataOptionalReference['data'].details" ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "TableDataOptionalReference" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Array index search
2022-09-28 17:28:18 +00:00
script : "array[8]" ,
expectedResults : [ "array[8]" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Deep array index search
2022-09-28 17:28:18 +00:00
script : "Table7.data[4]" ,
expectedResults : [ "Table7.data[4]" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Deep array index search
2022-09-28 17:28:18 +00:00
script : "Table7.data[4].value" ,
expectedResults : [ "Table7.data[4].value" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// string literal and array index search
script : "Table['data'][9]" ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Table" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// array index and string literal search
script : "Array[9]['data']" ,
expectedResults : [ ] ,
2022-09-30 13:06:25 +00:00
invalidIdentifiers : {
Array : true ,
} ,
2022-09-17 17:40:28 +00:00
} ,
{
// Index identifier search
2022-09-28 17:28:18 +00:00
script : "Table8.data[row][name]" ,
expectedResults : [ "Table8.data" , "row" ] ,
2022-09-30 13:06:25 +00:00
// name is a global scoped variable
invalidIdentifiers : {
name : true ,
} ,
2022-09-17 17:40:28 +00:00
} ,
{
// Index identifier search with global
2022-09-28 17:28:18 +00:00
script : "Table9.data[appsmith.store.row]" ,
expectedResults : [ "Table9.data" , "appsmith.store.row" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Index literal with further nested lookups
2022-09-28 17:28:18 +00:00
script : "Table10.data[row].name" ,
expectedResults : [ "Table10.data" , "row" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// IIFE and if conditions
script :
2022-09-28 17:28:18 +00:00
"(function(){ if(Table11.isVisible) { return Api1.data } else { return Api2.data } })()" ,
expectedResults : [ "Table11.isVisible" , "Api1.data" , "Api2.data" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Functions and arguments
2022-09-28 17:28:18 +00:00
script : "JSObject1.run(Api1.data, Api2.data)" ,
expectedResults : [ "JSObject1.run" , "Api1.data" , "Api2.data" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// IIFE - without braces
script : ` function() {
const index = Input1 . text
const obj = {
"a" : 123
}
return obj [ index ]
} ( ) ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Input1.text" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// IIFE
script : ` (function() {
const index = Input2 . text
const obj = {
"a" : 123
}
return obj [ index ]
} ) ( ) ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Input2.text" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// arrow IIFE - without braces - will fail
script : ` () => {
const index = Input3 . text
const obj = {
"a" : 123
}
return obj [ index ]
} ( ) ` ,
expectedResults : [ ] ,
} ,
{
// arrow IIFE
script : ` (() => {
const index = Input4 . text
const obj = {
"a" : 123
}
return obj [ index ]
} ) ( ) ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Input4.text" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Direct object access
script : ` { "a": 123 }[Input5.text] ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Input5.text" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Function declaration and default arguments
script : ` function run(apiData = Api1.data) {
return apiData ;
} ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Api1.data" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// Function declaration with arguments
script : ` function run(data) {
return data ;
} ` ,
expectedResults : [ ] ,
} ,
{
// anonymous function with variables
script : ` () => {
let row = 0 ;
const data = { } ;
while ( row < 10 ) {
data [ "test__" + row ] = Table12 . data [ row ] ;
row = row += 1 ;
}
} ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Table12.data" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// function with variables
script : ` function myFunction() {
let row = 0 ;
const data = { } ;
while ( row < 10 ) {
data [ "test__" + row ] = Table13 . data [ row ] ;
row = row += 1 ;
}
} ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Table13.data" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// expression with arithmetic operations
script : ` Table14.data + 15 ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Table14.data" ] ,
2022-09-17 17:40:28 +00:00
} ,
{
// expression with logical operations
script : ` Table15.data || [{}] ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Table15.data" ] ,
2022-09-17 17:40:28 +00:00
} ,
// JavaScript built in classes should not be valid identifiers
{
script : ` function(){
const firstApiRun = Api1 . run ( ) ;
const secondApiRun = Api2 . run ( ) ;
const randomNumber = Math . random ( ) ;
return Promise . all ( [ firstApiRun , secondApiRun ] )
} ( ) ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Api1.run" , "Api2.run" ] ,
2022-09-30 13:06:25 +00:00
invalidIdentifiers : {
Math : true ,
Promise : true ,
} ,
2022-09-17 17:40:28 +00:00
} ,
// Global dependencies should not be valid identifiers
{
script : ` function(){
const names = [ [ "john" , "doe" ] , [ "Jane" , "dane" ] ] ;
const flattenedNames = _ . flatten ( names ) ;
return { flattenedNames , time : moment ( ) }
} ( ) ` ,
expectedResults : [ ] ,
2022-09-30 13:06:25 +00:00
invalidIdentifiers : {
_ : true ,
moment : true ,
} ,
2022-09-17 17:40:28 +00:00
} ,
// browser Apis should not be valid identifiers
{
script : ` function(){
const names = {
firstName : "John" ,
lastName : "Doe"
} ;
const joinedName = Object . values ( names ) . join ( " " ) ;
console . log ( joinedName )
return Api2 . name
} ( ) ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "Api2.name" ] ,
2022-09-30 13:06:25 +00:00
invalidIdentifiers : {
Object : true ,
console : true ,
} ,
2022-09-17 17:40:28 +00:00
} ,
// identifiers and member expressions derived from params should not be valid identifiers
{
script : ` function(a, b){
return a . name + b . name
} ( ) ` ,
expectedResults : [ ] ,
} ,
// identifiers and member expressions derived from local variables should not be valid identifiers
{
script : ` function(){
const a = "variableA" ;
const b = "variableB" ;
return a . length + b . length
} ( ) ` ,
expectedResults : [ ] ,
} ,
// "appsmith" is an internal identifier and should be a valid reference
{
script : ` function(){
return appsmith . user
} ( ) ` ,
2022-09-28 17:28:18 +00:00
expectedResults : [ "appsmith.user" ] ,
2022-09-17 17:40:28 +00:00
} ,
] ;
2022-08-23 11:09:42 +00:00
2022-09-30 13:06:25 +00:00
// commenting to trigger test shared workflow action
2022-09-17 17:40:28 +00:00
cases . forEach ( ( perCase ) = > {
2022-09-30 13:06:25 +00:00
const { references } = extractIdentifierInfoFromCode (
perCase . script ,
2 ,
2023-04-03 10:41:15 +00:00
perCase . invalidIdentifiers ,
2022-09-30 13:06:25 +00:00
) ;
2022-09-17 17:40:28 +00:00
expect ( references ) . toStrictEqual ( perCase . expectedResults ) ;
} ) ;
} ) ;
} ) ;
2022-08-23 11:09:42 +00:00
2022-09-28 17:28:18 +00:00
describe ( "parseJSObjectWithAST" , ( ) = > {
it ( "parse js object" , ( ) = > {
2023-04-03 10:41:15 +00:00
const body = ` export default{
2022-09-17 17:40:28 +00:00
myVar1 : [ ] ,
myVar2 : { } ,
myFun1 : ( ) = > {
//write code here
} ,
myFun2 : async ( ) = > {
//use async-await or promises
}
} ` ;
2023-04-03 10:41:15 +00:00
const expectedParsedObject = [
2022-09-17 17:40:28 +00:00
{
2022-09-28 17:28:18 +00:00
key : "myVar1" ,
value : "[]" ,
2023-04-03 10:41:15 +00:00
rawContent : "myVar1: []" ,
2022-09-28 17:28:18 +00:00
type : "ArrayExpression" ,
2023-04-03 10:41:15 +00:00
position : {
startLine : 2 ,
startColumn : 1 ,
endLine : 2 ,
endColumn : 11 ,
keyStartLine : 2 ,
keyEndLine : 2 ,
keyStartColumn : 1 ,
keyEndColumn : 7 ,
} ,
2022-09-17 17:40:28 +00:00
} ,
{
2022-09-28 17:28:18 +00:00
key : "myVar2" ,
value : "{}" ,
2023-04-03 10:41:15 +00:00
rawContent : "myVar2: {}" ,
2022-09-28 17:28:18 +00:00
type : "ObjectExpression" ,
2023-04-03 10:41:15 +00:00
position : {
startLine : 3 ,
startColumn : 1 ,
endLine : 3 ,
endColumn : 11 ,
keyStartLine : 3 ,
keyEndLine : 3 ,
keyStartColumn : 1 ,
keyEndColumn : 7 ,
} ,
2022-09-17 17:40:28 +00:00
} ,
{
2022-09-28 17:28:18 +00:00
key : "myFun1" ,
value : "() => {}" ,
2023-04-03 10:41:15 +00:00
rawContent : "myFun1: () => {\n\t\t//write code here\n\t}" ,
2022-09-28 17:28:18 +00:00
type : "ArrowFunctionExpression" ,
2023-04-03 10:41:15 +00:00
position : {
startLine : 4 ,
startColumn : 1 ,
endLine : 6 ,
endColumn : 2 ,
keyStartLine : 4 ,
keyEndLine : 4 ,
keyStartColumn : 1 ,
keyEndColumn : 7 ,
} ,
2022-09-17 17:40:28 +00:00
arguments : [ ] ,
2023-05-16 16:59:11 +00:00
isMarkedAsync : false ,
2022-09-17 17:40:28 +00:00
} ,
{
2022-09-28 17:28:18 +00:00
key : "myFun2" ,
value : "async () => {}" ,
2023-04-03 10:41:15 +00:00
rawContent :
"myFun2: async () => {\n\t\t//use async-await or promises\n\t}" ,
2022-09-28 17:28:18 +00:00
type : "ArrowFunctionExpression" ,
2023-04-03 10:41:15 +00:00
position : {
startLine : 7 ,
startColumn : 1 ,
endLine : 9 ,
endColumn : 2 ,
keyStartLine : 7 ,
keyEndLine : 7 ,
keyStartColumn : 1 ,
keyEndColumn : 7 ,
} ,
2022-09-17 17:40:28 +00:00
arguments : [ ] ,
2023-04-03 10:41:15 +00:00
isMarkedAsync : true ,
2022-09-17 17:40:28 +00:00
} ,
] ;
2023-04-03 10:41:15 +00:00
const { parsedObject } = parseJSObject ( body ) ;
expect ( parsedObject ) . toStrictEqual ( expectedParsedObject ) ;
2022-09-17 17:40:28 +00:00
} ) ;
2022-08-23 11:09:42 +00:00
2022-09-28 17:28:18 +00:00
it ( "parse js object with literal" , ( ) = > {
2023-04-03 10:41:15 +00:00
const body = ` export default{
2022-09-17 17:40:28 +00:00
myVar1 : [ ] ,
myVar2 : {
"a" : "app" ,
} ,
myFun1 : ( ) = > {
//write code here
} ,
myFun2 : async ( ) = > {
//use async-await or promises
}
} ` ;
2023-04-03 10:41:15 +00:00
const expectedParsedObject = [
2022-09-17 17:40:28 +00:00
{
2022-09-28 17:28:18 +00:00
key : "myVar1" ,
value : "[]" ,
2023-04-03 10:41:15 +00:00
rawContent : "myVar1: []" ,
2022-09-28 17:28:18 +00:00
type : "ArrayExpression" ,
2023-04-03 10:41:15 +00:00
position : {
startLine : 2 ,
startColumn : 1 ,
endLine : 2 ,
endColumn : 11 ,
keyStartLine : 2 ,
keyEndLine : 2 ,
keyStartColumn : 1 ,
keyEndColumn : 7 ,
} ,
2022-09-17 17:40:28 +00:00
} ,
{
2022-09-28 17:28:18 +00:00
key : "myVar2" ,
2022-09-17 17:40:28 +00:00
value : '{\n "a": "app"\n}' ,
2023-04-03 10:41:15 +00:00
rawContent : 'myVar2: {\n\t\t"a": "app",\n\t}' ,
2022-09-28 17:28:18 +00:00
type : "ObjectExpression" ,
2023-04-03 10:41:15 +00:00
position : {
startLine : 3 ,
startColumn : 1 ,
endLine : 5 ,
endColumn : 2 ,
keyStartLine : 3 ,
keyEndLine : 3 ,
keyStartColumn : 1 ,
keyEndColumn : 7 ,
} ,
2022-09-17 17:40:28 +00:00
} ,
{
2022-09-28 17:28:18 +00:00
key : "myFun1" ,
value : "() => {}" ,
2023-04-03 10:41:15 +00:00
rawContent : "myFun1: () => {\n\t\t//write code here\n\t}" ,
2022-09-28 17:28:18 +00:00
type : "ArrowFunctionExpression" ,
2023-04-03 10:41:15 +00:00
position : {
startLine : 6 ,
startColumn : 1 ,
endLine : 8 ,
endColumn : 2 ,
keyStartLine : 6 ,
keyEndLine : 6 ,
keyStartColumn : 1 ,
keyEndColumn : 7 ,
} ,
2022-09-17 17:40:28 +00:00
arguments : [ ] ,
2023-05-16 16:59:11 +00:00
isMarkedAsync : false ,
2022-09-17 17:40:28 +00:00
} ,
{
2022-09-28 17:28:18 +00:00
key : "myFun2" ,
value : "async () => {}" ,
2023-04-03 10:41:15 +00:00
rawContent :
"myFun2: async () => {\n\t\t//use async-await or promises\n\t}" ,
2022-09-28 17:28:18 +00:00
type : "ArrowFunctionExpression" ,
2023-04-03 10:41:15 +00:00
position : {
startLine : 9 ,
startColumn : 1 ,
endLine : 11 ,
endColumn : 2 ,
keyStartLine : 9 ,
keyEndLine : 9 ,
keyStartColumn : 1 ,
keyEndColumn : 7 ,
} ,
2022-09-17 17:40:28 +00:00
arguments : [ ] ,
2023-04-03 10:41:15 +00:00
isMarkedAsync : true ,
2022-09-17 17:40:28 +00:00
} ,
] ;
2023-04-03 10:41:15 +00:00
const { parsedObject } = parseJSObject ( body ) ;
expect ( parsedObject ) . toStrictEqual ( expectedParsedObject ) ;
2022-09-17 17:40:28 +00:00
} ) ;
2022-08-23 11:09:42 +00:00
2022-09-28 17:28:18 +00:00
it ( "parse js object with variable declaration inside function" , ( ) = > {
2023-04-03 10:41:15 +00:00
const body = ` export default{
2022-09-17 17:40:28 +00:00
myFun1 : ( ) = > {
const a = {
conditions : [ ] ,
requires : 1 ,
testFunc : ( ) = > { } ,
testFunc2 : function ( ) { }
} ;
} ,
myFun2 : async ( ) = > {
//use async-await or promises
}
} ` ;
2023-04-03 10:41:15 +00:00
const expectedParsedObject = [
2022-09-17 17:40:28 +00:00
{
2022-09-28 17:28:18 +00:00
key : "myFun1" ,
2023-04-03 10:41:15 +00:00
value :
"() => {\n" +
" const a = {\n" +
" conditions: [],\n" +
" requires: 1,\n" +
" testFunc: () => {},\n" +
" testFunc2: function () {}\n" +
" };\n" +
"}" ,
rawContent :
"myFun1: () => {\n" +
" const a = {\n" +
" conditions: [],\n" +
" requires: 1,\n" +
" testFunc: () => {},\n" +
" testFunc2: function(){}\n" +
" };\n" +
" }" ,
2022-09-28 17:28:18 +00:00
type : "ArrowFunctionExpression" ,
2023-04-03 10:41:15 +00:00
position : {
startLine : 2 ,
startColumn : 6 ,
endLine : 9 ,
endColumn : 7 ,
keyStartLine : 2 ,
keyEndLine : 2 ,
keyStartColumn : 6 ,
keyEndColumn : 12 ,
} ,
2022-09-17 17:40:28 +00:00
arguments : [ ] ,
2023-04-03 10:41:15 +00:00
isMarkedAsync : false ,
2022-09-17 17:40:28 +00:00
} ,
{
2022-09-28 17:28:18 +00:00
key : "myFun2" ,
value : "async () => {}" ,
2023-04-03 10:41:15 +00:00
rawContent :
"myFun2: async () => {\n //use async-await or promises\n }" ,
2022-09-28 17:28:18 +00:00
type : "ArrowFunctionExpression" ,
2023-04-03 10:41:15 +00:00
position : {
startLine : 10 ,
startColumn : 6 ,
endLine : 12 ,
endColumn : 7 ,
keyStartLine : 10 ,
keyEndLine : 10 ,
keyStartColumn : 6 ,
keyEndColumn : 12 ,
} ,
2022-09-17 17:40:28 +00:00
arguments : [ ] ,
2023-04-03 10:41:15 +00:00
isMarkedAsync : true ,
2022-09-17 17:40:28 +00:00
} ,
] ;
2023-04-03 10:41:15 +00:00
const { parsedObject } = parseJSObject ( body ) ;
expect ( parsedObject ) . toStrictEqual ( expectedParsedObject ) ;
2022-09-17 17:40:28 +00:00
} ) ;
2022-08-23 11:09:42 +00:00
2022-09-28 17:28:18 +00:00
it ( "parse js object with params of all types" , ( ) = > {
2023-04-03 10:41:15 +00:00
const body = ` export default{
2022-09-17 17:40:28 +00:00
myFun2 : async ( a , b = Array ( 1 , 2 , 3 ) , c = "" , d = [ ] , e = this . myVar1 , f = { } , g = function ( ) { } , h = Object . assign ( { } ) , i = String ( ) , j = storeValue ( ) ) = > {
//use async-await or promises
} ,
} ` ;
2022-08-23 11:09:42 +00:00
2023-04-03 10:41:15 +00:00
const expectedParsedObject = [
2022-09-17 17:40:28 +00:00
{
2022-09-28 17:28:18 +00:00
key : "myFun2" ,
2022-09-17 17:40:28 +00:00
value :
'async (a, b = Array(1, 2, 3), c = "", d = [], e = this.myVar1, f = {}, g = function () {}, h = Object.assign({}), i = String(), j = storeValue()) => {}' ,
2023-04-03 10:41:15 +00:00
rawContent :
'myFun2: async (a,b = Array(1,2,3),c = "", d = [], e = this.myVar1, f = {}, g = function(){}, h = Object.assign({}), i = String(), j = storeValue()) => {\n' +
" //use async-await or promises\n" +
" }" ,
2022-09-28 17:28:18 +00:00
type : "ArrowFunctionExpression" ,
2023-04-03 10:41:15 +00:00
position : {
startLine : 2 ,
startColumn : 6 ,
endLine : 4 ,
endColumn : 7 ,
keyStartLine : 2 ,
keyEndLine : 2 ,
keyStartColumn : 6 ,
keyEndColumn : 12 ,
} ,
2022-09-17 17:40:28 +00:00
arguments : [
2023-04-03 10:41:15 +00:00
{ paramName : "a" , defaultValue : undefined } ,
{ paramName : "b" , defaultValue : undefined } ,
{ paramName : "c" , defaultValue : undefined } ,
{ paramName : "d" , defaultValue : undefined } ,
{ paramName : "e" , defaultValue : undefined } ,
{ paramName : "f" , defaultValue : undefined } ,
{ paramName : "g" , defaultValue : undefined } ,
{ paramName : "h" , defaultValue : undefined } ,
{ paramName : "i" , defaultValue : undefined } ,
{ paramName : "j" , defaultValue : undefined } ,
2022-09-17 17:40:28 +00:00
] ,
2023-04-03 10:41:15 +00:00
isMarkedAsync : true ,
2022-09-17 17:40:28 +00:00
} ,
] ;
2023-04-03 10:41:15 +00:00
const { parsedObject } = parseJSObject ( body ) ;
expect ( parsedObject ) . toStrictEqual ( expectedParsedObject ) ;
2022-09-17 17:40:28 +00:00
} ) ;
} ) ;
2023-05-16 16:59:11 +00:00
describe ( "isFunctionPresent" , ( ) = > {
it ( "should return true if function is present" , ( ) = > {
const code = "function myFun(){}" ;
const result = isFunctionPresent ( code , 2 ) ;
expect ( result ) . toBe ( true ) ;
} ) ;
it ( "should return true if arrow function is present" , ( ) = > {
const code = "const myFun = () => {}" ;
const result = isFunctionPresent ( code , 2 ) ;
expect ( result ) . toBe ( true ) ;
} ) ;
it ( "should return false if function is absent" , ( ) = > {
const code = "const a = { key: 'value' }" ;
const result = isFunctionPresent ( code , 2 ) ;
expect ( result ) . toBe ( false ) ;
} ) ;
it ( "should return false for a string" , ( ) = > {
const code = "Hello world {{appsmith.store.name}}!!" ;
const result = isFunctionPresent ( code , 2 ) ;
expect ( result ) . toBe ( false ) ;
} ) ;
it ( "should return true for shorthand arrow function" , ( ) = > {
const code = "const myFun = () => 'value'" ;
const result = isFunctionPresent ( code , 2 ) ;
expect ( result ) . toBe ( true ) ;
} ) ;
it ( "should return true for IFFE function" , ( ) = > {
const code = "(function myFun(){ console.log('hello') })()" ;
const result = isFunctionPresent ( code , 2 ) ;
expect ( result ) . toBe ( true ) ;
} ) ;
it ( "should return true for functions with parameters" , ( ) = > {
const code = "function myFun(arg1, arg2){ console.log(arg1, arg2); }" ;
const result = isFunctionPresent ( code , 2 ) ;
expect ( result ) . toBe ( true ) ;
} ) ;
it ( "should return true for functions with parameters" , ( ) = > {
const code = "function myFun(arg1, arg2){ console.log(arg1, arg2); }" ;
const result = isFunctionPresent ( code , 2 ) ;
expect ( result ) . toBe ( true ) ;
} ) ;
it ( "should return true for higher order functions" , ( ) = > {
const code = "function myFun(cb){ const val = cb(); }" ;
const result = isFunctionPresent ( code , 2 ) ;
expect ( result ) . toBe ( true ) ;
} ) ;
it ( "should return true for functions with promises" , ( ) = > {
const code = "async function myFun(promise){ const val = await promise; }" ;
const result = isFunctionPresent ( code , 2 ) ;
expect ( result ) . toBe ( true ) ;
} ) ;
} ) ;