2022-02-03 05:52:14 +00:00
import { RenderModes } from "constants/WidgetConstants" ;
import { ValidationTypes } from "constants/WidgetValidation" ;
2025-02-14 12:56:31 +00:00
import { EvaluationSubstitutionType } from "ee/entities/DataTree/types" ;
2025-02-18 10:42:05 +00:00
import type { CanvasWidgetsReduxState } from "ee/reducers/entityReducers/canvasWidgetsReducer" ;
2023-05-11 05:26:03 +00:00
import { AutocompleteDataType } from "./autocomplete/AutocompleteDataType" ;
2022-01-18 07:52:24 +00:00
import {
flattenObject ,
getLocale ,
getSubstringBetweenTwoWords ,
2022-02-03 05:52:14 +00:00
captureInvalidDynamicBindingPath ,
2022-01-18 07:52:24 +00:00
mergeWidgetConfig ,
2022-05-04 09:45:57 +00:00
extractColorsFromString ,
2022-09-17 17:40:28 +00:00
isNameValid ,
2022-11-20 06:12:32 +00:00
pushToArray ,
concatWithArray ,
2022-01-18 07:52:24 +00:00
} from "./helpers" ;
2023-09-06 12:15:04 +00:00
import WidgetFactory from "../WidgetProvider/factory" ;
2022-02-03 05:52:14 +00:00
import * as Sentry from "@sentry/react" ;
2022-05-04 09:45:57 +00:00
import { Colors } from "constants/Colors" ;
2021-05-18 13:54:40 +00:00
describe ( "flattenObject test" , ( ) = > {
it ( "Check if non nested object is returned correctly" , ( ) = > {
const testObject = {
isVisible : true ,
isDisabled : false ,
tableData : false ,
} ;
expect ( flattenObject ( testObject ) ) . toStrictEqual ( testObject ) ;
} ) ;
it ( "Check if nested objects are returned correctly" , ( ) = > {
const tests = [
{
input : {
isVisible : true ,
isDisabled : false ,
tableData : false ,
settings : {
color : [
{
headers : {
left : true ,
} ,
} ,
] ,
} ,
} ,
output : {
isVisible : true ,
isDisabled : false ,
tableData : false ,
"settings.color[0].headers.left" : true ,
} ,
} ,
{
input : {
isVisible : true ,
isDisabled : false ,
tableData : false ,
settings : {
color : true ,
} ,
} ,
output : {
isVisible : true ,
isDisabled : false ,
tableData : false ,
"settings.color" : true ,
} ,
} ,
{
input : {
numbers : [ 1 , 2 , 3 ] ,
color : { header : "red" } ,
} ,
output : {
"numbers[0]" : 1 ,
"numbers[1]" : 2 ,
"numbers[2]" : 3 ,
"color.header" : "red" ,
} ,
} ,
{
input : {
name : null ,
color : { header : { } } ,
users : {
id : undefined ,
} ,
} ,
output : {
"color.header" : { } ,
name : null ,
"users.id" : undefined ,
} ,
} ,
] ;
tests . map ( ( test ) = >
expect ( flattenObject ( test . input ) ) . toStrictEqual ( test . output ) ,
) ;
} ) ;
} ) ;
2021-09-13 12:03:39 +00:00
describe ( "#getSubstringBetweenTwoWords" , ( ) = > {
it ( "returns substring between 2 words from a string" , ( ) = > {
const input : [ string , string , string ] [ ] = [
[ "aaa.bbb.ccc" , "aaa." , ".ccc" ] ,
[ "aaa.bbb.bbb.ccc" , "aaa." , ".ccc" ] ,
[ "aaa.aaa.aaa.aaa" , "aaa" , "aaa" ] ,
[ "aaa...aaa.aaa.aaa" , "aaa" , "aaa" ] ,
[ "aaa..bbb" , "aaa." , ".bbb" ] ,
[ "aaa.bbb" , "aaa." , ".bbb" ] ,
[ "aaabbb" , "aaab" , "abbb" ] ,
] ;
const output = [ "bbb" , "bbb.bbb" , ".aaa.aaa." , "...aaa.aaa." , "" , "" , "" ] ;
input . forEach ( ( inp , index ) = > {
expect ( getSubstringBetweenTwoWords ( . . . inp ) ) . toBe ( output [ index ] ) ;
} ) ;
} ) ;
} ) ;
2022-01-18 07:52:24 +00:00
describe ( "#mergeWidgetConfig" , ( ) = > {
it ( "should merge the widget configs" , ( ) = > {
const base = [
{
sectionName : "General" ,
children : [
{
propertyName : "someWidgetConfig" ,
} ,
] ,
} ,
{
sectionName : "icon" ,
children : [
{
propertyName : "someWidgetIconConfig" ,
} ,
] ,
} ,
] ;
const extended = [
{
sectionName : "General" ,
children : [
{
propertyName : "someOtherWidgetConfig" ,
} ,
] ,
} ,
{
sectionName : "style" ,
children : [
{
propertyName : "someWidgetStyleConfig" ,
} ,
] ,
} ,
] ;
const expected = [
{
sectionName : "General" ,
children : [
{
propertyName : "someOtherWidgetConfig" ,
} ,
{
propertyName : "someWidgetConfig" ,
} ,
] ,
} ,
{
sectionName : "style" ,
children : [
{
propertyName : "someWidgetStyleConfig" ,
} ,
] ,
} ,
{
sectionName : "icon" ,
children : [
{
propertyName : "someWidgetIconConfig" ,
} ,
] ,
} ,
] ;
expect ( mergeWidgetConfig ( extended , base ) ) . toEqual ( expected ) ;
} ) ;
} ) ;
describe ( "#getLocale" , ( ) = > {
it ( "should test that getLocale is returning navigator.languages[0]" , ( ) = > {
expect ( getLocale ( ) ) . toBe ( navigator . languages [ 0 ] ) ;
} ) ;
} ) ;
2022-02-03 05:52:14 +00:00
describe ( "#captureInvalidDynamicBindingPath" , ( ) = > {
it ( "DSL should not be altered" , ( ) = > {
const baseDSL = {
widgetName : "RadioGroup1" ,
dynamicPropertyPathList : [ ] ,
displayName : "Radio Group" ,
iconSVG : "/static/media/icon.ba2b2ee0.svg" ,
topRow : 57 ,
bottomRow : 65 ,
parentRowSpace : 10 ,
type : "RADIO_GROUP_WIDGET" ,
hideCard : false ,
defaultOptionValue : "{{1}}" ,
animateLoading : true ,
parentColumnSpace : 33.375 ,
dynamicTriggerPathList : [ ] ,
leftColumn : 42 ,
dynamicBindingPathList : [
{
key : "defaultOptionValue" ,
} ,
{
key : "options" ,
} ,
] ,
options :
'[\n {\n "label": "Yes",\n "value": {{1 > 0 ? 1 : 0}}\n },\n {\n "label": "No",\n "value": 2\n }\n]' ,
isDisabled : false ,
key : "opzs6suotf" ,
isRequired : false ,
rightColumn : 62 ,
widgetId : "s195otz2jm" ,
isVisible : true ,
label : "" ,
version : 1 ,
parentId : "0" ,
renderMode : RenderModes.CANVAS ,
isLoading : false ,
} ;
const getPropertyConfig = jest . spyOn (
WidgetFactory ,
"getWidgetPropertyPaneConfig" ,
) ;
2024-09-18 16:35:28 +00:00
2022-02-03 05:52:14 +00:00
getPropertyConfig . mockReturnValueOnce ( [
{
sectionName : "General" ,
children : [
{
helpText : "Displays a list of unique options" ,
propertyName : "options" ,
label : "Options" ,
controlType : "OPTION_INPUT" ,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . FUNCTION ,
params : {
expected : {
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
type : 'Array<{ "label": "string", "value": "string" | number}>' ,
2022-02-03 05:52:14 +00:00
example : '[{"label": "abc", "value": "abc" | 1}]' ,
autocompleteDataType : AutocompleteDataType.STRING ,
} ,
fnString :
'function optionsCustomValidation(options, props, _) {\n var validationUtil = (options, _) => {\n var _isValid = true;\n var message = "";\n var valueType = "";\n var uniqueLabels = {};\n\n for (var i = 0; i < options.length; i++) {\n var _options$i = options[i],\n label = _options$i.label,\n value = _options$i.value;\n\n if (!valueType) {\n valueType = typeof value;\n } //Checks the uniqueness all the values in the options\n\n\n if (!uniqueLabels.hasOwnProperty(value)) {\n uniqueLabels[value] = "";\n } else {\n _isValid = false;\n message = "path:value must be unique. Duplicate values found";\n break;\n } //Check if the required field "label" is present:\n\n\n if (!label) {\n _isValid = false;\n message = "Invalid entry at index: " + i + ". Missing required key: label";\n break;\n } //Validation checks for the the label.\n\n\n if (_.isNil(label) || label === "" || typeof label !== "string" && typeof label !== "number") {\n _isValid = false;\n message = "Invalid entry at index: " + i + ". Value of key: label is invalid: This value does not evaluate to type string";\n break;\n } //Check if all the data types for the value prop is the same.\n\n\n if (typeof value !== valueType) {\n _isValid = false;\n message = "All value properties in options must have the same type";\n break;\n } //Check if the each object has value property.\n\n\n if (_.isNil(value)) {\n _isValid = false;\n message = \'This value does not evaluate to type Array<{ "label": "string", "value": "string" | number }>\';\n break;\n }\n }\n\n return {\n isValid: _isValid,\n parsed: _isValid ? options : [],\n messages: [message]\n };\n };\n\n var invalidResponse = {\n isValid: false,\n parsed: [],\n messages: [\'This value does not evaluate to type Array<{ "label": "string", "value": "string" | number }>\']\n };\n\n try {\n if (_.isString(options)) {\n options = JSON.parse(options);\n }\n\n if (Array.isArray(options)) {\n return validationUtil(options, _);\n } else {\n return invalidResponse;\n }\n } catch (e) {\n return invalidResponse;\n }\n}' ,
} ,
} ,
evaluationSubstitutionType :
EvaluationSubstitutionType . SMART_SUBSTITUTE ,
id : "6su4u0bwoe" ,
} ,
{
helpText : "Sets a default selected option" ,
propertyName : "defaultOptionValue" ,
2023-05-19 18:37:06 +00:00
label : "Default selected value" ,
2022-02-03 05:52:14 +00:00
// placeholderText: "Y",
controlType : "INPUT_TEXT" ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . FUNCTION ,
params : {
expected : {
type : "string |\nnumber (only works in mustache syntax)" ,
example : "abc | {{1}}" ,
autocompleteDataType : AutocompleteDataType.STRING ,
} ,
fnString :
'function defaultOptionValidation(value, props, _) {\n //Checks if the value is not of object type in {{}}\n if (_.isObject(value)) {\n return {\n isValid: false,\n parsed: JSON.stringify(value, null, 2),\n messages: ["This value does not evaluate to type: string or number"]\n };\n } //Checks if the value is not of boolean type in {{}}\n\n\n if (_.isBoolean(value)) {\n return {\n isValid: false,\n parsed: value,\n messages: ["This value does not evaluate to type: string or number"]\n };\n }\n\n return {\n isValid: true,\n parsed: value\n };\n}' ,
} ,
} ,
id : "8wpzo6szbl" ,
} ,
{
propertyName : "isRequired" ,
label : "Required" ,
helpText : "Makes input to the widget mandatory" ,
controlType : "SWITCH" ,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . BOOLEAN ,
} ,
id : "60kc73ivwp" ,
} ,
{
helpText : "Controls the visibility of the widget" ,
propertyName : "isVisible" ,
label : "Visible" ,
controlType : "SWITCH" ,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . BOOLEAN ,
} ,
id : "w4591dtf5l" ,
} ,
{
propertyName : "isDisabled" ,
label : "Disabled" ,
helpText : "Disables input to this widget" ,
controlType : "SWITCH" ,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . BOOLEAN ,
} ,
id : "6p7181ccec" ,
} ,
{
propertyName : "animateLoading" ,
2023-05-19 18:37:06 +00:00
label : "Animate loading" ,
2022-02-03 05:52:14 +00:00
controlType : "SWITCH" ,
helpText : "Controls the loading of the widget" ,
// defaultValue: true,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . BOOLEAN ,
} ,
id : "y2gw796t2z" ,
} ,
] ,
id : "jfh7ud39r4" ,
} ,
{
sectionName : "Events" ,
children : [
{
2023-04-06 16:49:12 +00:00
helpText : "when a user changes the selected option" ,
2022-02-03 05:52:14 +00:00
propertyName : "onSelectionChange" ,
label : "onSelectionChange" ,
controlType : "ACTION_SELECTOR" ,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : true ,
id : "wqgudnqu5n" ,
} ,
] ,
id : "b2k5a0t632" ,
} ,
] ) ;
const newDsl = captureInvalidDynamicBindingPath ( baseDSL ) ;
2024-09-18 16:35:28 +00:00
2022-02-03 05:52:14 +00:00
expect ( baseDSL ) . toEqual ( newDsl ) ;
} ) ;
it ( "Checks if dynamicBindingPathList contains a property path that doesn't have a binding" , ( ) = > {
const baseDSL = {
widgetName : "RadioGroup1" ,
dynamicPropertyPathList : [ ] ,
displayName : "Radio Group" ,
iconSVG : "/static/media/icon.ba2b2ee0.svg" ,
topRow : 57 ,
bottomRow : 65 ,
parentRowSpace : 10 ,
type : "RADIO_GROUP_WIDGET" ,
hideCard : false ,
defaultOptionValue : "{{1}}" ,
animateLoading : true ,
parentColumnSpace : 33.375 ,
dynamicTriggerPathList : [ ] ,
leftColumn : 42 ,
dynamicBindingPathList : [
{
key : "defaultOptionValue" ,
} ,
{
key : "options" ,
} ,
] ,
options : [ ] ,
isDisabled : false ,
key : "opzs6suotf" ,
isRequired : false ,
rightColumn : 62 ,
widgetId : "s195otz2jm" ,
isVisible : true ,
label : "" ,
version : 1 ,
parentId : "0" ,
renderMode : RenderModes.CANVAS ,
isLoading : false ,
} ;
const getPropertyConfig = jest . spyOn (
WidgetFactory ,
"getWidgetPropertyPaneConfig" ,
) ;
2024-09-18 16:35:28 +00:00
2022-02-03 05:52:14 +00:00
getPropertyConfig . mockReturnValueOnce ( [
{
sectionName : "General" ,
children : [
{
helpText : "Displays a list of unique options" ,
propertyName : "options" ,
label : "Options" ,
controlType : "OPTION_INPUT" ,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . FUNCTION ,
params : {
expected : {
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
type : 'Array<{ "label": "string", "value": "string" | number}>' ,
2022-02-03 05:52:14 +00:00
example : '[{"label": "abc", "value": "abc" | 1}]' ,
autocompleteDataType : AutocompleteDataType.STRING ,
} ,
fnString :
'function optionsCustomValidation(options, props, _) {\n var validationUtil = (options, _) => {\n var _isValid = true;\n var message = "";\n var valueType = "";\n var uniqueLabels = {};\n\n for (var i = 0; i < options.length; i++) {\n var _options$i = options[i],\n label = _options$i.label,\n value = _options$i.value;\n\n if (!valueType) {\n valueType = typeof value;\n } //Checks the uniqueness all the values in the options\n\n\n if (!uniqueLabels.hasOwnProperty(value)) {\n uniqueLabels[value] = "";\n } else {\n _isValid = false;\n message = "path:value must be unique. Duplicate values found";\n break;\n } //Check if the required field "label" is present:\n\n\n if (!label) {\n _isValid = false;\n message = "Invalid entry at index: " + i + ". Missing required key: label";\n break;\n } //Validation checks for the the label.\n\n\n if (_.isNil(label) || label === "" || typeof label !== "string" && typeof label !== "number") {\n _isValid = false;\n message = "Invalid entry at index: " + i + ". Value of key: label is invalid: This value does not evaluate to type string";\n break;\n } //Check if all the data types for the value prop is the same.\n\n\n if (typeof value !== valueType) {\n _isValid = false;\n message = "All value properties in options must have the same type";\n break;\n } //Check if the each object has value property.\n\n\n if (_.isNil(value)) {\n _isValid = false;\n message = \'This value does not evaluate to type Array<{ "label": "string", "value": "string" | number }>\';\n break;\n }\n }\n\n return {\n isValid: _isValid,\n parsed: _isValid ? options : [],\n messages: [message]\n };\n };\n\n var invalidResponse = {\n isValid: false,\n parsed: [],\n messages: [\'This value does not evaluate to type Array<{ "label": "string", "value": "string" | number }>\']\n };\n\n try {\n if (_.isString(options)) {\n options = JSON.parse(options);\n }\n\n if (Array.isArray(options)) {\n return validationUtil(options, _);\n } else {\n return invalidResponse;\n }\n } catch (e) {\n return invalidResponse;\n }\n}' ,
} ,
} ,
evaluationSubstitutionType :
EvaluationSubstitutionType . SMART_SUBSTITUTE ,
id : "6su4u0bwoe" ,
} ,
{
helpText : "Sets a default selected option" ,
propertyName : "defaultOptionValue" ,
2023-05-19 18:37:06 +00:00
label : "Default selected value" ,
2022-02-03 05:52:14 +00:00
// placeholderText: "Y",
controlType : "INPUT_TEXT" ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . FUNCTION ,
params : {
expected : {
type : "string |\nnumber (only works in mustache syntax)" ,
example : "abc | {{1}}" ,
autocompleteDataType : AutocompleteDataType.STRING ,
} ,
fnString :
'function defaultOptionValidation(value, props, _) {\n //Checks if the value is not of object type in {{}}\n if (_.isObject(value)) {\n return {\n isValid: false,\n parsed: JSON.stringify(value, null, 2),\n messages: ["This value does not evaluate to type: string or number"]\n };\n } //Checks if the value is not of boolean type in {{}}\n\n\n if (_.isBoolean(value)) {\n return {\n isValid: false,\n parsed: value,\n messages: ["This value does not evaluate to type: string or number"]\n };\n }\n\n return {\n isValid: true,\n parsed: value\n };\n}' ,
} ,
} ,
id : "8wpzo6szbl" ,
} ,
{
propertyName : "isRequired" ,
label : "Required" ,
helpText : "Makes input to the widget mandatory" ,
controlType : "SWITCH" ,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . BOOLEAN ,
} ,
id : "60kc73ivwp" ,
} ,
{
helpText : "Controls the visibility of the widget" ,
propertyName : "isVisible" ,
label : "Visible" ,
controlType : "SWITCH" ,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . BOOLEAN ,
} ,
id : "w4591dtf5l" ,
} ,
{
propertyName : "isDisabled" ,
label : "Disabled" ,
helpText : "Disables input to this widget" ,
controlType : "SWITCH" ,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . BOOLEAN ,
} ,
id : "6p7181ccec" ,
} ,
{
propertyName : "animateLoading" ,
2023-05-19 18:37:06 +00:00
label : "Animate loading" ,
2022-02-03 05:52:14 +00:00
controlType : "SWITCH" ,
helpText : "Controls the loading of the widget" ,
// defaultValue: true,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : false ,
validation : {
type : ValidationTypes . BOOLEAN ,
} ,
id : "y2gw796t2z" ,
} ,
] ,
id : "jfh7ud39r4" ,
} ,
{
sectionName : "Events" ,
children : [
{
2023-04-06 16:49:12 +00:00
helpText : "when a user changes the selected option" ,
2022-02-03 05:52:14 +00:00
propertyName : "onSelectionChange" ,
label : "onSelectionChange" ,
controlType : "ACTION_SELECTOR" ,
isJSConvertible : true ,
isBindProperty : true ,
isTriggerProperty : true ,
id : "wqgudnqu5n" ,
} ,
] ,
id : "b2k5a0t632" ,
} ,
] ) ;
const sentrySpy = jest . spyOn ( Sentry , "captureException" ) ;
captureInvalidDynamicBindingPath ( baseDSL ) ;
expect ( sentrySpy ) . toHaveBeenCalledWith (
new Error (
` INVALID_DynamicPathBinding_CLIENT_ERROR: Invalid dynamic path binding list: RadioGroup1.options ` ,
) ,
) ;
} ) ;
} ) ;
2022-05-04 09:45:57 +00:00
describe ( "#extractColorsFromString" , ( ) = > {
it ( "Check if the extractColorsFromString returns rgb, rgb, hex color strings" , ( ) = > {
2023-08-07 08:55:10 +00:00
const widgets = {
0 : { color : ` ${ Colors . GREEN } ` } ,
1 : { color : "rgb(0,0,0)" } ,
2 : { color : ` ${ Colors . BOX_SHADOW_DEFAULT_VARIANT1 } ` } ,
3 : { color : ` LightGoldenrodYellow ` } ,
4 : { color : ` lch(54.292% 106.839 40.853) ` } ,
} as unknown as CanvasWidgetsReduxState ;
2022-05-04 09:45:57 +00:00
//Check Hex value
2023-08-07 08:55:10 +00:00
expect ( extractColorsFromString ( widgets ) [ 0 ] ) . toEqual ( "#03B365" ) ;
//Check rgb
expect ( extractColorsFromString ( widgets ) [ 1 ] ) . toEqual ( "rgb(0,0,0)" ) ;
2022-05-04 09:45:57 +00:00
//Check rgba value
2023-08-07 08:55:10 +00:00
expect ( extractColorsFromString ( widgets ) [ 2 ] ) . toEqual ( "rgba(0, 0, 0, 0.25)" ) ;
2022-05-04 09:45:57 +00:00
2023-08-07 08:55:10 +00:00
//Check name value
expect ( extractColorsFromString ( widgets ) [ 3 ] ) . toEqual ( "LightGoldenrodYellow" ) ;
//Check lch value
expect ( extractColorsFromString ( widgets ) [ 4 ] ) . toEqual (
"lch(54.292% 106.839 40.853)" ,
) ;
2022-05-04 09:45:57 +00:00
} ) ;
} ) ;
2022-09-17 17:40:28 +00:00
describe ( "isNameValid()" , ( ) = > {
it ( "works properly" , ( ) = > {
const invalidEntityNames = [
"console" ,
"Promise" ,
"appsmith" ,
"Math" ,
"yield" ,
"Boolean" ,
"ReferenceError" ,
"clearTimeout" ,
"parseInt" ,
"eval" ,
2023-05-22 06:22:25 +00:00
"performance" ,
2022-09-17 17:40:28 +00:00
] ;
// Some window object methods and properties names should be valid entity names since evaluation is done
// in the worker thread, and some of the window methods and properties are not available there.
const validEntityNames = [ "history" , "parent" , "screen" ] ;
2024-09-18 16:35:28 +00:00
2022-09-17 17:40:28 +00:00
for ( const invalidName of invalidEntityNames ) {
expect ( isNameValid ( invalidName , { } ) ) . toBe ( false ) ;
}
2024-09-18 16:35:28 +00:00
2022-09-17 17:40:28 +00:00
for ( const validName of validEntityNames ) {
expect ( isNameValid ( validName , { } ) ) . toBe ( true ) ;
}
} ) ;
} ) ;
2022-11-20 06:12:32 +00:00
describe ( "pushToArray" , ( ) = > {
it ( "adds to an undefined array" , ( ) = > {
const item = "something" ;
const expected = [ "something" ] ;
const result = pushToArray ( item ) ;
2024-09-18 16:35:28 +00:00
2022-11-20 06:12:32 +00:00
expect ( result ) . toStrictEqual ( expected ) ;
} ) ;
it ( "adds to an existing array" , ( ) = > {
const item = "something" ;
const arr1 = [ "another" ] ;
const expected = [ "another" , "something" ] ;
const result = pushToArray ( item , arr1 ) ;
2024-09-18 16:35:28 +00:00
2022-11-20 06:12:32 +00:00
expect ( result ) . toStrictEqual ( expected ) ;
} ) ;
it ( "adds to an existing array and make unique" , ( ) = > {
const item = "something" ;
const arr1 = [ "another" , "another" ] ;
const expected = [ "another" , "something" ] ;
const result = pushToArray ( item , arr1 , true ) ;
2024-09-18 16:35:28 +00:00
2022-11-20 06:12:32 +00:00
expect ( result ) . toStrictEqual ( expected ) ;
} ) ;
} ) ;
describe ( "concatWithArray" , ( ) = > {
it ( "adds to an undefined array" , ( ) = > {
const items = [ "something" ] ;
const expected = [ "something" ] ;
const result = concatWithArray ( items ) ;
2024-09-18 16:35:28 +00:00
2022-11-20 06:12:32 +00:00
expect ( result ) . toStrictEqual ( expected ) ;
} ) ;
it ( "adds to an existing array" , ( ) = > {
const items = [ "something" ] ;
const arr1 = [ "another" ] ;
const expected = [ "another" , "something" ] ;
const result = concatWithArray ( items , arr1 ) ;
2024-09-18 16:35:28 +00:00
2022-11-20 06:12:32 +00:00
expect ( result ) . toStrictEqual ( expected ) ;
} ) ;
it ( "adds to an existing array and make unique" , ( ) = > {
const items = [ "something" ] ;
const arr1 = [ "another" , "another" ] ;
const expected = [ "another" , "something" ] ;
const result = concatWithArray ( items , arr1 , true ) ;
2024-09-18 16:35:28 +00:00
2022-11-20 06:12:32 +00:00
expect ( result ) . toStrictEqual ( expected ) ;
} ) ;
} ) ;