PromucFlow_constructor/app/client/src/sagas/helper.test.ts
Ilia d6f249b42d
chore: add blank line eslint rule (#36369)
## Description
Added ESLint rule to force blank lines between statements. 


Fixes #`Issue Number`  
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags="@tag.All"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!CAUTION]
> 🔴 🔴 🔴 Some tests have failed.
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/10924926728>
> Commit: 34f57714a1575ee04e94e03cbcaf95e57a96c86c
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=10924926728&attempt=1&selectiontype=test&testsstatus=failed&specsstatus=fail"
target="_blank">Cypress dashboard</a>.
> Tags: @tag.All
> Spec: 
> The following are new failures, please fix them before merging the PR:
<ol>
> <li>cypress/e2e/Regression/ClientSide/Anvil/AnvilModal_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilCheckboxGroupWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilCurrencyInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilIconButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilInlineButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilParagraphWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilPhoneInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilStatsWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilSwitchGroupWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilSwitchWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilTableWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilToolbarButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilZoneSectionWidgetSnapshot_spec.ts</ol>
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/identified-flaky-tests-65890b3c81d7400d08fa9ee3?branch=master"
target="_blank">List of identified flaky tests</a>.
> <hr>Wed, 18 Sep 2024 16:33:36 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No

---------

Co-authored-by: Valera Melnikov <valera@appsmith.com>
2024-09-18 19:35:28 +03:00

170 lines
7.8 KiB
TypeScript

/**
* @jest-environment jsdom
*/
import { TextEncoder, TextDecoder } from "util";
Object.assign(global, { TextDecoder, TextEncoder });
import {
enhanceRequestPayloadWithEventData,
cleanValuesInObjectForHashing,
generateHashFromString,
} from "sagas/helper";
describe("tests the redux saga helper functions", () => {
it("tests the enhanceRequestPayloadWithEventData function", () => {
const inputs = [
{ payload: { id: "xyz" }, type: "COPY_ACTION_INIT" },
{ payload: { id: "xyz" }, type: "DUMMY_ACTION" },
{
payload: {
id: "xyz",
eventData: { analyticsData: { originalActionId: "abc" } },
},
type: "COPY_ACTION_INIT",
},
{ payload: {}, type: "COPY_ACTION_INIT" },
{ payload: {}, type: "" },
{ payload: undefined, type: "" },
];
const outputs = [
{ id: "xyz", eventData: { analyticsData: { originalActionId: "xyz" } } },
{ id: "xyz" },
{ id: "xyz", eventData: { analyticsData: { originalActionId: "abc" } } },
{},
{},
undefined,
];
inputs.forEach((input, index) => {
expect(
enhanceRequestPayloadWithEventData(input.payload, input.type),
).toStrictEqual(outputs[index]);
});
});
it("tests the cleanValuesInObjectForHashing function", () => {
const inputs = [
{
body: 'UPDATE public.users SET "id"= \'{{Table2.updatedRow.id}}\', "gender"= \'{{Table2.updatedRow.gender}}\', "latitude"= \'{{Table2.updatedRow.latitude}}\', "longitude"= \'{{Table2.updatedRow.longitude}}\', "dob"= \'{{Table2.updatedRow.dob}}\', "phone"= \'{{Table2.updatedRow.phone}}\', "email"= \'{{Table2.updatedRow.email}}\', "image"= \'{{Table2.updatedRow.image}}\', "country"= \'{{Table2.updatedRow.country}}\', "name"= \'{{Table2.updatedRow.name}}\' WHERE "id"= {{Table2.updatedRow.id}};',
},
{
paginationType: "NONE",
encodeParamsToggle: true,
body: 'UPDATE public.users SET "id"= \'{{Table2.updatedRow.id}}\', "gender"= \'{{Table2.updatedRow.gender}}\', "latitude"= \'{{Table2.updatedRow.latitude}}\', "longitude"= \'{{Table2.updatedRow.longitude}}\', "dob"= \'{{Table2.updatedRow.dob}}\', "phone"= \'{{Table2.updatedRow.phone}}\', "email"= \'{{Table2.updatedRow.email}}\', "image"= \'{{Table2.updatedRow.image}}\', "country"= \'{{Table2.updatedRow.country}}\', "name"= \'{{Table2.updatedRow.name}}\' WHERE "id"= {{Table2.updatedRow.id}};',
selfReferencingDataPaths: [],
pluginSpecifiedTemplates: [{ value: false }],
},
{
paginationType: "NONE",
encodeParamsToggle: true,
selfReferencingDataPaths: [],
formData: {
aggregate: { limit: { data: "10" } },
smartSubstitution: { data: true },
insert: { documents: { data: "{{(Table3.newRow || {})}}" } },
command: { data: "INSERT" },
collection: { data: "movies" },
},
},
{
timeoutInMillisecond: 10000.0,
paginationType: "NONE",
encodeParamsToggle: true,
selfReferencingDataPaths: [],
formData: { pageSize: "100", command: "", sort: "" },
},
{
body: 'UPDATE public.users SET "id"= \'{{Table2.updatedRow.id}}\', "gender"= \'{{Table2.updatedRow.gender}}\', "latitude"= \'{{Table2.updatedRow.latitude}}\', "longitude"= \'{{Table2.updatedRow.longitude}}\', "dob"= \'{{Table2.updatedRow.dob}}\', "phone"= \'{{Table2.updatedRow.phone}}\', "email"= \'{{Table2.updatedRow.email}}\', "image"= \'{{Table2.updatedRow.image}}\', "country"= \'{{Table2.updatedRow.country}}\', "name"= \'{{Table2.updatedRow.name}}\' WHERE "id"= {{Table2.updatedRow.id}}; \n -- I am an SQL comment',
},
];
const outputs = [
{
body: `updatepublic.usersset"id"='',"gender"='',"latitude"='',"longitude"='',"dob"='',"phone"='',"email"='',"image"='',"country"='',"name"=''where"id"=;`,
},
{
paginationType: "none",
encodeParamsToggle: true,
body: `updatepublic.usersset"id"='',"gender"='',"latitude"='',"longitude"='',"dob"='',"phone"='',"email"='',"image"='',"country"='',"name"=''where"id"=;`,
selfReferencingDataPaths: [],
pluginSpecifiedTemplates: [{ value: false }],
},
{
paginationType: "none",
encodeParamsToggle: true,
selfReferencingDataPaths: [],
formData: {
aggregate: { limit: { data: "10" } },
smartSubstitution: { data: true },
insert: { documents: { data: "" } },
command: { data: "insert" },
collection: { data: "movies" },
},
},
{
timeoutInMillisecond: 10000.0,
paginationType: "none",
encodeParamsToggle: true,
selfReferencingDataPaths: [],
formData: { pageSize: "100", command: "", sort: "" },
},
{
body: `updatepublic.usersset"id"='',"gender"='',"latitude"='',"longitude"='',"dob"='',"phone"='',"email"='',"image"='',"country"='',"name"=''where"id"=;`,
},
];
inputs.forEach((input, index) => {
expect(cleanValuesInObjectForHashing(input)).toStrictEqual(
outputs[index],
);
});
});
it("tests the generateHashFromString function", async () => {
// verify that the same strings generate the same SHA1
const sameInputs = ["testString1", "testString1"];
const output1 = await generateHashFromString(sameInputs[0]);
const output2 = await generateHashFromString(sameInputs[1]);
expect(output1).toEqual(output2);
// verify that different strings generate different SHA1
const differentInputs = ["testString1", "testString2"];
const output3 = await generateHashFromString(differentInputs[0]);
const output4 = await generateHashFromString(differentInputs[1]);
expect(output3).not.toEqual(output4);
// verify that the SHA1 is the same in the future
// Note: There is something up with the algorithm, due to the it using
// ByteArrays and converting to hex strings
// If you google, the SHA1 for "testString", you get "956265657d0b637ef65b9b59f9f858eecf55ed6a"
// Nevertheless, for our purposes, this is fine
const testString = "testString";
const sha1fortestString = "21b695cef037e9a8e0b3edccac3e5e58271edf22";
const output = await generateHashFromString(testString);
expect(output).toEqual(sha1fortestString);
});
it("tests the generateHashFromString function with a complex object", async () => {
const input1 = {
body: 'UPDATE public.users SET "id"= \'{{Table2.updatedRow.id}}\', "gender"= \'{{Table2.updatedRow.gender}}\', "latitude"= \'{{Table2.updatedRow.latitude}}\', "longitude"= \'{{Table2.updatedRow.longitude}}\', "dob"= \'{{Table2.updatedRow.dob}}\', "phone"= \'{{Table2.updatedRow.phone}}\', "email"= \'{{Table2.updatedRow.email}}\', "image"= \'{{Table2.updatedRow.image}}\', "country"= \'{{Table2.updatedRow.country}}\', "name"= \'{{Table2.updatedRow.name}}\' WHERE "id"= {{Table2.updatedRow.id}}; \n -- I am an SQL comment',
};
const input2 = {
body: `UPDATE public.users SET "id"= \'{{Table2.updatedRow.id}}\', "gender"= \'{{Table2.updatedRow.gender}}\', "latitude"= \'{{Table2.updatedRow.latitude}}\', "longitude"= \'{{Table2.updatedRow.longitude}}\', "dob"= \'{{Table2.updatedRow.dob}}\', "phone"= \'{{Table2.updatedRow.phone}}\', "email"= \'{{Table2.updatedRow.email}}\', "image"= \'{{(() => { return "myvalue" })}}\', "country"= \'{{Table2.updatedRow.country}}\', "name"= \'{{GetQuery.updatedRow.name}}\' WHERE "id"= {{Table5.updatedRow.id}};`,
};
const cleanObjectInput1 = cleanValuesInObjectForHashing(input1);
const cleanObjectInput2 = cleanValuesInObjectForHashing(input2);
const hash1 = await generateHashFromString(
JSON.stringify(cleanObjectInput1),
);
const hash2 = await generateHashFromString(
JSON.stringify(cleanObjectInput2),
);
expect(hash1).toEqual(hash2);
});
});