2024-08-08 12:55:00 +00:00
|
|
|
import { ReduxActionTypes } from "ee/constants/ReduxActionConstants";
|
2024-07-31 02:54:51 +00:00
|
|
|
import { initEditorAction } from "actions/initActions";
|
2022-08-26 13:13:48 +00:00
|
|
|
import { setAppMode, updateCurrentPage } from "actions/pageActions";
|
2021-08-06 09:17:56 +00:00
|
|
|
import { APP_MODE } from "entities/App";
|
2022-08-26 13:13:48 +00:00
|
|
|
import { useDispatch } from "react-redux";
|
2025-02-18 10:42:05 +00:00
|
|
|
import type { CanvasWidgetsReduxState } from "ee/reducers/entityReducers/canvasWidgetsReducer";
|
2024-08-08 12:55:00 +00:00
|
|
|
import { getCanvasWidgetsPayload } from "ee/sagas/PageSagas";
|
2023-03-04 07:25:54 +00:00
|
|
|
import { editorInitializer } from "utils/editor/EditorUtils";
|
2022-08-26 13:13:48 +00:00
|
|
|
import { extractCurrentDSL } from "utils/WidgetPropsUtils";
|
2024-08-08 12:55:00 +00:00
|
|
|
import type { AppState } from "ee/reducers";
|
|
|
|
|
import type { WidgetEntity } from "ee/entities/DataTree/types";
|
|
|
|
|
import urlBuilder from "ee/entities/URLRedirect/URLAssembly";
|
2025-02-18 10:42:05 +00:00
|
|
|
import type { FlattenedWidgetProps } from "ee/reducers/entityReducers/canvasWidgetsStructureReducer";
|
2024-08-22 04:19:30 +00:00
|
|
|
import type { Page } from "entities/Page";
|
2024-10-17 15:18:39 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
import type { FetchPageResponse } from "api/PageApi";
|
2021-05-18 18:29:39 +00:00
|
|
|
|
2024-06-11 09:44:54 +00:00
|
|
|
const pageId = "0123456789abcdef00000000";
|
|
|
|
|
|
2024-10-17 15:18:39 +00:00
|
|
|
export const useMockDsl = (dsl: unknown, mode?: APP_MODE) => {
|
2021-05-18 18:29:39 +00:00
|
|
|
const dispatch = useDispatch();
|
2024-10-17 15:18:39 +00:00
|
|
|
const [hasLoaded, setHasLoaded] = useState(false);
|
|
|
|
|
|
|
|
|
|
const mockResp = {
|
2021-05-18 18:29:39 +00:00
|
|
|
data: {
|
2024-06-11 09:44:54 +00:00
|
|
|
id: pageId,
|
|
|
|
|
pageId: pageId,
|
2021-05-18 18:29:39 +00:00
|
|
|
name: "Page1",
|
|
|
|
|
applicationId: "app_id",
|
2021-06-17 13:26:54 +00:00
|
|
|
isDefault: true,
|
|
|
|
|
isHidden: false,
|
2022-07-11 04:06:29 +00:00
|
|
|
slug: "page-1",
|
2021-05-18 18:29:39 +00:00
|
|
|
layouts: [
|
|
|
|
|
{
|
|
|
|
|
id: "layout_id",
|
|
|
|
|
dsl,
|
|
|
|
|
layoutOnLoadActions: [],
|
|
|
|
|
layoutActions: [],
|
|
|
|
|
},
|
|
|
|
|
],
|
2022-12-01 06:30:50 +00:00
|
|
|
userPermissions: [
|
|
|
|
|
"read:pages",
|
|
|
|
|
"manage:pages",
|
|
|
|
|
"create:pageActions",
|
|
|
|
|
"delete:pages",
|
|
|
|
|
],
|
2021-05-18 18:29:39 +00:00
|
|
|
},
|
2024-10-17 15:18:39 +00:00
|
|
|
} as unknown as FetchPageResponse;
|
2021-05-18 18:29:39 +00:00
|
|
|
|
2024-10-17 15:18:39 +00:00
|
|
|
useEffect(function loadScripts() {
|
|
|
|
|
const loadMigrationScripts = async () => {
|
|
|
|
|
const canvasWidgetsPayloadD = await getCanvasWidgetsPayload(mockResp);
|
|
|
|
|
const currentDSL = await extractCurrentDSL({
|
|
|
|
|
response: mockResp,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const currentDsl = currentDSL.dsl;
|
|
|
|
|
const canvasWidgetsPayload = canvasWidgetsPayloadD.widgets;
|
|
|
|
|
|
|
|
|
|
dispatch(setAppMode(mode || APP_MODE.EDIT));
|
|
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.FETCH_PAGE_DSLS_SUCCESS,
|
|
|
|
|
payload: [
|
|
|
|
|
{
|
|
|
|
|
pageId: mockResp.data.id,
|
|
|
|
|
dsl: currentDsl,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
const pages = [
|
|
|
|
|
{
|
|
|
|
|
pageName: mockResp.data.name,
|
|
|
|
|
pageId: mockResp.data.id,
|
|
|
|
|
basePageId: mockResp.data.id,
|
|
|
|
|
isDefault: mockResp.data.isDefault,
|
|
|
|
|
isHidden: !!mockResp.data.isHidden,
|
|
|
|
|
slug: mockResp.data.slug,
|
|
|
|
|
userPermissions: [
|
|
|
|
|
"read:pages",
|
|
|
|
|
"manage:pages",
|
|
|
|
|
"create:pageActions",
|
|
|
|
|
"delete:pages",
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
] as unknown as Page[];
|
|
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.FETCH_PAGE_LIST_SUCCESS,
|
|
|
|
|
payload: {
|
|
|
|
|
pages,
|
|
|
|
|
applicationId: mockResp.data.applicationId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
dispatch({
|
|
|
|
|
type: "UPDATE_LAYOUT",
|
|
|
|
|
payload: { widgets: canvasWidgetsPayload },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
dispatch(updateCurrentPage(mockResp.data.id));
|
|
|
|
|
setHasLoaded(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
loadMigrationScripts();
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return hasLoaded;
|
2021-05-18 18:29:39 +00:00
|
|
|
};
|
2023-01-28 02:17:06 +00:00
|
|
|
|
2024-10-17 15:18:39 +00:00
|
|
|
export function MockPageDSL({
|
|
|
|
|
children,
|
|
|
|
|
dsl,
|
|
|
|
|
}: {
|
|
|
|
|
children: JSX.Element;
|
|
|
|
|
dsl?: unknown;
|
|
|
|
|
}) {
|
2021-05-18 18:29:39 +00:00
|
|
|
editorInitializer();
|
2024-10-17 15:18:39 +00:00
|
|
|
|
|
|
|
|
const hasLoaded = useMockDsl(dsl);
|
|
|
|
|
|
|
|
|
|
return hasLoaded ? children : null;
|
2021-05-18 18:29:39 +00:00
|
|
|
}
|
2021-06-17 13:26:54 +00:00
|
|
|
|
2022-08-19 10:10:36 +00:00
|
|
|
const getChildWidgets = (
|
|
|
|
|
canvasWidgets: CanvasWidgetsReduxState,
|
|
|
|
|
widgetId: string,
|
|
|
|
|
) => {
|
|
|
|
|
const parentWidget = canvasWidgets[widgetId];
|
|
|
|
|
|
|
|
|
|
if (parentWidget.children) {
|
|
|
|
|
return parentWidget.children.map((childWidgetId) => {
|
2023-03-20 11:04:02 +00:00
|
|
|
const childWidget = { ...canvasWidgets[childWidgetId] } as WidgetEntity;
|
2022-08-19 10:10:36 +00:00
|
|
|
|
|
|
|
|
if (childWidget?.children?.length > 0) {
|
|
|
|
|
childWidget.children = getChildWidgets(canvasWidgets, childWidgetId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return childWidget;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const mockGetChildWidgets = (state: AppState, widgetId: string) => {
|
|
|
|
|
return getChildWidgets(state.entities.canvasWidgets, widgetId);
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-01 06:30:50 +00:00
|
|
|
export const mockGetPagePermissions = () => {
|
|
|
|
|
return ["read:pages", "manage:pages", "create:pageActions", "delete:pages"];
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-19 10:10:36 +00:00
|
|
|
export const mockCreateCanvasWidget = (
|
|
|
|
|
canvasWidget: FlattenedWidgetProps,
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2023-03-20 11:04:02 +00:00
|
|
|
evaluatedWidget: WidgetEntity,
|
2024-10-17 15:18:39 +00:00
|
|
|
) => {
|
2022-08-19 10:10:36 +00:00
|
|
|
return { ...canvasWidget };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const mockGetWidgetEvalValues = (
|
|
|
|
|
state: AppState,
|
|
|
|
|
widgetName: string,
|
|
|
|
|
) => {
|
|
|
|
|
return Object.values(state.entities.canvasWidgets).find(
|
|
|
|
|
(widget) => widget.widgetName === widgetName,
|
2023-03-20 11:04:02 +00:00
|
|
|
) as WidgetEntity;
|
2022-08-19 10:10:36 +00:00
|
|
|
};
|
|
|
|
|
|
2021-06-17 13:26:54 +00:00
|
|
|
export const syntheticTestMouseEvent = (
|
|
|
|
|
event: MouseEvent,
|
|
|
|
|
optionsToAdd = {},
|
|
|
|
|
) => {
|
|
|
|
|
const options = Object.entries(optionsToAdd);
|
2024-10-17 15:18:39 +00:00
|
|
|
|
2021-06-17 13:26:54 +00:00
|
|
|
options.forEach(([key, value]) => {
|
|
|
|
|
Object.defineProperty(event, key, { get: () => value });
|
|
|
|
|
});
|
2024-10-17 15:18:39 +00:00
|
|
|
|
2021-06-17 13:26:54 +00:00
|
|
|
return event;
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-17 15:18:39 +00:00
|
|
|
export function MockApplication({ children }: { children: JSX.Element }) {
|
2021-05-18 18:29:39 +00:00
|
|
|
editorInitializer();
|
|
|
|
|
const dispatch = useDispatch();
|
2024-10-17 15:18:39 +00:00
|
|
|
|
2024-07-31 02:54:51 +00:00
|
|
|
dispatch(initEditorAction({ basePageId: pageId, mode: APP_MODE.EDIT }));
|
2024-10-17 15:18:39 +00:00
|
|
|
|
|
|
|
|
const mockResp = {
|
2022-06-15 15:37:41 +00:00
|
|
|
workspaceId: "workspace_id",
|
2022-08-26 13:13:48 +00:00
|
|
|
pages: [
|
|
|
|
|
{
|
2024-06-11 09:44:54 +00:00
|
|
|
id: pageId,
|
2024-07-31 02:54:51 +00:00
|
|
|
baseId: pageId,
|
2024-06-11 09:44:54 +00:00
|
|
|
pageId: pageId,
|
2022-08-26 13:13:48 +00:00
|
|
|
name: "Page1",
|
|
|
|
|
isDefault: true,
|
|
|
|
|
slug: "page-1",
|
2022-12-01 06:30:50 +00:00
|
|
|
userPermissions: [
|
|
|
|
|
"read:pages",
|
|
|
|
|
"manage:pages",
|
|
|
|
|
"create:pageActions",
|
|
|
|
|
"delete:pages",
|
|
|
|
|
],
|
2022-08-26 13:13:48 +00:00
|
|
|
},
|
|
|
|
|
],
|
2021-05-18 18:29:39 +00:00
|
|
|
id: "app_id",
|
2024-07-31 02:54:51 +00:00
|
|
|
baseId: "app_id",
|
2021-05-18 18:29:39 +00:00
|
|
|
isDefault: true,
|
2022-07-11 04:06:29 +00:00
|
|
|
name: "appName",
|
|
|
|
|
slug: "app-name",
|
|
|
|
|
applicationVersion: 2,
|
2021-05-18 18:29:39 +00:00
|
|
|
};
|
2024-10-17 15:18:39 +00:00
|
|
|
|
2022-07-11 04:06:29 +00:00
|
|
|
urlBuilder.updateURLParams(
|
|
|
|
|
{
|
2024-07-31 02:54:51 +00:00
|
|
|
baseApplicationId: mockResp.baseId,
|
2022-07-11 04:06:29 +00:00
|
|
|
applicationSlug: mockResp.slug,
|
|
|
|
|
applicationVersion: mockResp.applicationVersion,
|
|
|
|
|
},
|
|
|
|
|
[
|
|
|
|
|
{
|
2024-07-31 02:54:51 +00:00
|
|
|
basePageId: mockResp.pages[0].baseId,
|
2022-07-11 04:06:29 +00:00
|
|
|
pageSlug: mockResp.pages[0].slug,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
);
|
2021-05-18 18:29:39 +00:00
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.FETCH_APPLICATION_SUCCESS,
|
|
|
|
|
payload: mockResp,
|
|
|
|
|
});
|
2022-07-11 04:06:29 +00:00
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.FETCH_PAGE_LIST_SUCCESS,
|
|
|
|
|
payload: {
|
|
|
|
|
pages: mockResp.pages,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.SWITCH_CURRENT_PAGE_ID,
|
2022-12-01 06:30:50 +00:00
|
|
|
payload: {
|
2024-06-11 09:44:54 +00:00
|
|
|
id: pageId,
|
2022-12-01 06:30:50 +00:00
|
|
|
slug: "page-1",
|
|
|
|
|
permissions: [
|
|
|
|
|
"read:pages",
|
|
|
|
|
"manage:pages",
|
|
|
|
|
"create:pageActions",
|
|
|
|
|
"delete:pages",
|
|
|
|
|
],
|
|
|
|
|
},
|
2022-07-11 04:06:29 +00:00
|
|
|
});
|
2024-10-17 15:18:39 +00:00
|
|
|
|
2021-05-18 18:29:39 +00:00
|
|
|
return children;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//got it from @blueprintjs/test-commons to dispatch hotkeys events
|
|
|
|
|
export function dispatchTestKeyboardEventWithCode(
|
|
|
|
|
target: EventTarget,
|
|
|
|
|
eventType: string,
|
|
|
|
|
key: string,
|
|
|
|
|
keyCode: number,
|
|
|
|
|
shift = false,
|
|
|
|
|
meta = false,
|
|
|
|
|
) {
|
|
|
|
|
const event = document.createEvent("KeyboardEvent");
|
2024-10-17 15:18:39 +00:00
|
|
|
|
|
|
|
|
event.initKeyboardEvent(
|
2021-05-18 18:29:39 +00:00
|
|
|
eventType,
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
window,
|
|
|
|
|
key,
|
|
|
|
|
0,
|
|
|
|
|
meta,
|
|
|
|
|
false,
|
|
|
|
|
shift,
|
|
|
|
|
);
|
|
|
|
|
Object.defineProperty(event, "key", { get: () => key });
|
|
|
|
|
Object.defineProperty(event, "which", { get: () => keyCode });
|
|
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
|
}
|