## Description Ad unit tests for adding and moving building blocks saga. Fixes #34146 ## Automation /ok-to-test tags="@tag.Sanity" ### 🔍 Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/9494392536> > Commit: c6b9b1b679f284458cc6500729408ddb015adb62 > Cypress dashboard url: <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=9494392536&attempt=1" target="_blank">Click here!</a> <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Tests** - Added comprehensive tests for adding and moving building blocks on the canvas. - Updated import paths for consistency in test files. - Introduced new test suites for sagas handling building block operations. - **New Fixtures** - Added mock data and constants for testing building block functionalities. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
112 lines
3.5 KiB
TypeScript
112 lines
3.5 KiB
TypeScript
import type { CallEffect, PutEffect, SelectEffect } from "redux-saga/effects";
|
|
import { call, select } from "redux-saga/effects";
|
|
import {
|
|
addBuildingBlockToCanvasSaga,
|
|
loadBuildingBlocksIntoApplication,
|
|
} from "../BuildingBlockAdditionSagas";
|
|
import type { ReduxAction } from "@appsmith/constants/ReduxActionConstants";
|
|
import { getCurrentWorkspaceId } from "@appsmith/selectors/selectedWorkspaceSelectors";
|
|
import type { WidgetAddChild } from "actions/pageActions";
|
|
import { addChildSaga } from "sagas/WidgetAdditionSagas";
|
|
import { getDragDetails, getWidgetByName } from "sagas/selectors";
|
|
import { getCurrentApplicationId } from "selectors/editorSelectors";
|
|
import { initiateBuildingBlockDropEvent } from "utils/buildingBlockUtils";
|
|
import { addEntityAction, skeletonWidget } from "./fixtures";
|
|
|
|
type GeneratorType = Generator<
|
|
CallEffect | SelectEffect | PutEffect,
|
|
void,
|
|
unknown
|
|
>;
|
|
|
|
describe("addBuildingBlockToCanvasSaga", () => {
|
|
it("1. should add a skeleton widget and initiate a building block drop", () => {
|
|
const generator: GeneratorType =
|
|
addBuildingBlockToCanvasSaga(addEntityAction);
|
|
|
|
// Step 1: select getCurrentApplicationId
|
|
let result = generator.next();
|
|
expect(result.value).toEqual(select(getCurrentApplicationId));
|
|
|
|
// Mock return value of getCurrentApplicationId
|
|
const applicationId = "app1";
|
|
result = generator.next(applicationId);
|
|
expect(result.value).toEqual(select(getCurrentWorkspaceId));
|
|
|
|
// Step 2: select getCurrentWorkspaceId
|
|
const workspaceId = "workspace1";
|
|
result = generator.next(workspaceId);
|
|
expect(result.value).toEqual(select(getDragDetails));
|
|
|
|
// Step 3: select getDragDetails
|
|
const dragDetails = {
|
|
newWidget: {
|
|
displayName: "TestWidget",
|
|
},
|
|
};
|
|
result = generator.next(dragDetails);
|
|
|
|
// Generating the skeletonWidgetName
|
|
const buildingblockName = dragDetails.newWidget.displayName;
|
|
const skeletonWidgetName = `loading_${buildingblockName.toLowerCase().replace(/ /g, "_")}`;
|
|
|
|
const addSkeletonWidgetAction: ReduxAction<
|
|
WidgetAddChild & { shouldReplay: boolean }
|
|
> = {
|
|
...addEntityAction,
|
|
payload: {
|
|
...addEntityAction.payload,
|
|
type: "SKELETON_WIDGET",
|
|
widgetName: skeletonWidgetName,
|
|
shouldReplay: false,
|
|
},
|
|
};
|
|
|
|
// Step 4: call initiateBuildingBlockDropEvent
|
|
expect(result.value).toEqual(
|
|
call(initiateBuildingBlockDropEvent, {
|
|
applicationId,
|
|
workspaceId,
|
|
buildingblockName,
|
|
}),
|
|
);
|
|
|
|
// Step 5: call addChildSaga
|
|
result = generator.next();
|
|
expect(result.value).toEqual(call(addChildSaga, addSkeletonWidgetAction));
|
|
|
|
// Step 6: select getWidgetByName
|
|
result = generator.next();
|
|
expect(result.value).toEqual(select(getWidgetByName, skeletonWidgetName));
|
|
|
|
result = generator.next(skeletonWidget);
|
|
|
|
// Step 7: call loadBuildingBlocksIntoApplication
|
|
expect(result.value).toEqual(
|
|
call(
|
|
loadBuildingBlocksIntoApplication,
|
|
addEntityAction.payload,
|
|
skeletonWidget.widgetId,
|
|
),
|
|
);
|
|
|
|
// Complete the generator
|
|
result = generator.next();
|
|
expect(result.done).toBe(true);
|
|
});
|
|
|
|
it("2. should handle errors gracefully", () => {
|
|
const generator: GeneratorType =
|
|
addBuildingBlockToCanvasSaga(addEntityAction);
|
|
|
|
generator.next();
|
|
// Introduce an error by throwing one manually
|
|
const error = new Error("Something went wrong");
|
|
try {
|
|
generator.throw(error);
|
|
} catch (err) {
|
|
expect(err).toBe(error);
|
|
}
|
|
});
|
|
});
|