PromucFlow_constructor/app/client/src/ce/entities/DataTree/dataTreeJSAction.test.ts
ashit-rath c281600ea7
chore: refactor create jsobject under modules (#29555)
## Description
Refactor to create js objects for modules

#### PR fixes following issue(s)
PR for https://github.com/appsmithorg/appsmith-ee/pull/3095

#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change
- Chore (housekeeping or task changes that don't impact user perception)

## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [ ] Manual
- [ ] JUnit
- [ ] Jest
- [ ] Cypress
>
>
#### 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
- [ ] 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
- [ ] 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:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Refactor**
- Updated import paths for `JSCollectionData` and related types across
various files to reflect a change in the file structure or module
resolution strategy.
- Exported `initialState` and `handlers` from `jsActionsReducer` to
align with updated code organization.

- **New Features**
- Added a new entry `JSModules` to the `entitySections` object in
`editorContextReducer.ts` to enhance editor context management.

- **Bug Fixes**
- Adjusted the `createDummyJSCollectionActions` function to include
`additionalParams` and `variables` for improved action creation and
initialization.

- **Documentation**
  - No visible changes to end-user documentation in this pull request.

- **Style**
  - No style-related changes affecting end-users in this pull request.

- **Tests**
  - No test-related changes affecting end-users in this pull request.

- **Chores**
  - No chore-related changes affecting end-users in this pull request.

- **Revert**
  - No reverts affecting end-users in this pull request.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2023-12-13 16:16:27 +05:30

398 lines
11 KiB
TypeScript

import { PluginType } from "entities/Action";
import { generateDataTreeJSAction } from "./dataTreeJSAction";
import type { JSCollectionData } from "@appsmith/reducers/entityReducers/jsActionsReducer";
describe("generateDataTreeJSAction", () => {
it("generate js collection in data tree", () => {
const jsCollection: JSCollectionData = {
isLoading: false,
config: {
id: "1234",
applicationId: "app123",
workspaceId: "workspace123",
name: "JSObject2",
pageId: "page123",
pluginId: "plugin123",
pluginType: PluginType.JS,
actionIds: [],
archivedActionIds: [],
actions: [
{
id: "abcd",
applicationId: "app123",
workspaceId: "workspace123",
pluginType: PluginType.JS,
pluginId: "plugin123",
name: "myFun2",
fullyQualifiedName: "JSObject2.myFun2",
datasource: {
userPermissions: [],
name: "UNUSED_DATASOURCE",
pluginId: "plugin123",
workspaceId: "workspace123",
messages: [],
isValid: true,
new: true,
},
pageId: "page123",
collectionId: "1234",
actionConfiguration: {
timeoutInMillisecond: 10000,
// @ts-expect-error: paginationType does not exists on JSAction
paginationType: "NONE",
encodeParamsToggle: true,
body: "async () => {\n\t\t//use async-await or promises\n\t}",
jsArguments: [],
},
executeOnLoad: false,
dynamicBindingPathList: [
{
key: "body",
},
],
isValid: true,
invalids: [],
messages: [],
jsonPathKeys: [
"async () => {\n\t\t//use async-await or promises\n\t}",
],
confirmBeforeExecute: false,
userPermissions: [
"read:actions",
"execute:actions",
"manage:actions",
],
validName: "JSObject2.myFun2",
},
{
id: "623973054d9aea1b062af87b",
applicationId: "app123",
workspaceId: "workspace123",
pluginType: "JS",
pluginId: "plugin123",
name: "myFun1",
fullyQualifiedName: "JSObject2.myFun1",
datasource: {
userPermissions: [],
name: "UNUSED_DATASOURCE",
pluginId: "plugin123",
workspaceId: "workspace123",
messages: [],
isValid: true,
new: true,
},
pageId: "page123",
collectionId: "1234",
actionConfiguration: {
timeoutInMillisecond: 10000,
// @ts-expect-error: paginationType does not exists on JSAction
paginationType: "NONE",
encodeParamsToggle: true,
body: "() => {\n\t\t//write code here\n\t}",
jsArguments: [],
},
executeOnLoad: false,
clientSideExecution: true,
dynamicBindingPathList: [
{
key: "body",
},
],
isValid: true,
invalids: [],
messages: [],
jsonPathKeys: ["() => {\n\t\t//write code here\n\t}"],
confirmBeforeExecute: false,
userPermissions: [
"read:actions",
"execute:actions",
"manage:actions",
],
validName: "JSObject2.myFun1",
},
],
archivedActions: [],
body: "export default {\n\tmyVar1: [],\n\tmyVar2: {},\n\tmyFun1: () => {\n\t\t//write code here\n\t},\n\tmyFun2: async () => {\n\t\t//use async-await or promises\n\t}\n}",
variables: [
{
name: "myVar1",
value: [],
},
{
name: "myVar2",
value: {},
},
],
},
data: {},
};
const expectedData = {
myVar1: [],
myVar2: {},
ENTITY_TYPE: "JSACTION",
body: "export default {\n\tmyVar1: [],\n\tmyVar2: {},\n\tmyFun1: () => {\n\t\t//write code here\n\t},\n\tmyFun2: async () => {\n\t\t//use async-await or promises\n\t}\n}",
myFun2: {
data: {},
},
myFun1: {
data: {},
},
actionId: "1234",
};
const expectedConfig = {
name: "JSObject2",
actionId: "1234",
pluginType: "JS",
ENTITY_TYPE: "JSACTION",
meta: {
myFun2: {
arguments: [],
confirmBeforeExecute: false,
},
myFun1: {
arguments: [],
confirmBeforeExecute: false,
},
},
bindingPaths: {
body: "SMART_SUBSTITUTE",
myFun2: "SMART_SUBSTITUTE",
myFun1: "SMART_SUBSTITUTE",
myVar1: "SMART_SUBSTITUTE",
myVar2: "SMART_SUBSTITUTE",
},
dynamicBindingPathList: [
{
key: "body",
},
{
key: "myVar1",
},
{
key: "myVar2",
},
{
key: "myFun2",
},
{
key: "myFun1",
},
],
variables: ["myVar1", "myVar2"],
dependencyMap: {
body: ["myFun2", "myFun1"],
},
reactivePaths: {
body: "SMART_SUBSTITUTE",
myFun1: "SMART_SUBSTITUTE",
myFun2: "SMART_SUBSTITUTE",
myVar1: "SMART_SUBSTITUTE",
myVar2: "SMART_SUBSTITUTE",
},
};
const resultData = generateDataTreeJSAction(jsCollection);
expect(resultData.unEvalEntity).toStrictEqual(expectedData);
expect(resultData.configEntity).toStrictEqual(expectedConfig);
});
it("replaces 'this' in body with js object name", () => {
const jsCollection: JSCollectionData = {
isLoading: false,
config: {
id: "1234",
applicationId: "app123",
workspaceId: "workspace123",
name: "JSObject2",
pageId: "page123",
pluginId: "plugin123",
pluginType: PluginType.JS,
actionIds: [],
archivedActionIds: [],
actions: [
{
id: "abcd",
applicationId: "app123",
workspaceId: "workspace123",
pluginType: PluginType.JS,
pluginId: "plugin123",
name: "myFun2",
fullyQualifiedName: "JSObject2.myFun2",
datasource: {
userPermissions: [],
name: "UNUSED_DATASOURCE",
pluginId: "plugin123",
workspaceId: "workspace123",
messages: [],
isValid: true,
new: true,
},
pageId: "page123",
collectionId: "1234",
actionConfiguration: {
timeoutInMillisecond: 10000,
// @ts-expect-error: paginationType does not exists on JSAction
paginationType: "NONE",
encodeParamsToggle: true,
body: "async () => {\n\t\t//use async-await or promises\n\t}",
jsArguments: [],
},
executeOnLoad: false,
dynamicBindingPathList: [
{
key: "body",
},
],
isValid: true,
invalids: [],
messages: [],
jsonPathKeys: [
"async () => {\n\t\t//use async-await or promises\n\t}",
],
confirmBeforeExecute: false,
userPermissions: [
"read:actions",
"execute:actions",
"manage:actions",
],
validName: "JSObject2.myFun2",
},
{
id: "623973054d9aea1b062af87b",
applicationId: "app123",
workspaceId: "workspace123",
pluginType: "JS",
pluginId: "plugin123",
name: "myFun1",
fullyQualifiedName: "JSObject2.myFun1",
datasource: {
userPermissions: [],
name: "UNUSED_DATASOURCE",
pluginId: "plugin123",
workspaceId: "workspace123",
messages: [],
isValid: true,
new: true,
},
pageId: "page123",
collectionId: "1234",
actionConfiguration: {
timeoutInMillisecond: 10000,
// @ts-expect-error: paginationType does not exists on JSAction
paginationType: "NONE",
encodeParamsToggle: true,
body: "() => {\n\t\t//write code here\n\t}",
jsArguments: [],
},
executeOnLoad: false,
clientSideExecution: true,
dynamicBindingPathList: [
{
key: "body",
},
],
isValid: true,
invalids: [],
messages: [],
jsonPathKeys: ["() => {\n\t\t//write code here\n\t}"],
confirmBeforeExecute: false,
userPermissions: [
"read:actions",
"execute:actions",
"manage:actions",
],
validName: "JSObject2.myFun1",
},
],
archivedActions: [],
body: "export default {\n\tmyVar1: [],\n\tmyVar2: {},\n\tmyFun1: () => {\n\t\t//write code here\n\t return this.myFun2},\n\tmyFun2: async () => {\n\t\t//use async-await or promises\n\t}\n}",
variables: [
{
name: "myVar1",
value: [],
},
{
name: "myVar2",
value: {},
},
],
},
data: {
abcd: {
users: [{ id: 1, name: "John" }],
},
},
};
const expectedData = {
myVar1: [],
myVar2: {},
body: "export default {\n\tmyVar1: [],\n\tmyVar2: {},\n\tmyFun1: () => {\n\t\t//write code here\n\t return JSObject2.myFun2},\n\tmyFun2: async () => {\n\t\t//use async-await or promises\n\t}\n}",
ENTITY_TYPE: "JSACTION",
myFun2: {
data: {},
},
myFun1: {
data: {},
},
actionId: "1234",
};
const expectedConfig = {
ENTITY_TYPE: "JSACTION",
actionId: "1234",
meta: {
myFun2: {
arguments: [],
confirmBeforeExecute: false,
},
myFun1: {
arguments: [],
confirmBeforeExecute: false,
},
},
bindingPaths: {
body: "SMART_SUBSTITUTE",
myFun2: "SMART_SUBSTITUTE",
myFun1: "SMART_SUBSTITUTE",
myVar1: "SMART_SUBSTITUTE",
myVar2: "SMART_SUBSTITUTE",
},
dynamicBindingPathList: [
{
key: "body",
},
{
key: "myVar1",
},
{
key: "myVar2",
},
{
key: "myFun2",
},
{
key: "myFun1",
},
],
variables: ["myVar1", "myVar2"],
dependencyMap: {
body: ["myFun2", "myFun1"],
},
name: "JSObject2",
pluginType: "JS",
reactivePaths: {
body: "SMART_SUBSTITUTE",
myFun1: "SMART_SUBSTITUTE",
myFun2: "SMART_SUBSTITUTE",
myVar1: "SMART_SUBSTITUTE",
myVar2: "SMART_SUBSTITUTE",
},
};
const result = generateDataTreeJSAction(jsCollection);
expect(result.unEvalEntity).toStrictEqual(expectedData);
expect(result.configEntity).toStrictEqual(expectedConfig);
});
});